Streaming from Microphone using python
1
import pyaudio
2
import socketio
3
import json
4
import requests
5
from six.moves import queue
6
sio = socketio.Client()
7
​
8
@sio.event
9
def connect():
10
print("I'm connected!")
11
@sio.event
12
def connect_error(data):
13
print("The connection failed!")
14
​
15
@sio.event
16
def disconnect():
17
print("I'm disconnected!")
18
​
19
#@sio.event
20
#def messages(sid, data):
21
# print(data)
22
​
23
@sio.event
24
def output(data):
25
data = json.loads(data)
26
print('Trascript : ', data.get('sentence'))
27
print('Sentiment : ', data.get('sentiment'))
28
print('Intent : ', data.get('intent'))
29
print('intent Phrase', data.get('intentPhrase'))
30
print('Tone : ', data.get('tone'))
31
print('Emotion : ', data.get('emotion'))
32
print('Statement-Question-Command : ', data.get('sqc'))
33
print('Preset Statement Tag : ', data.get('presetStatementTag'))
34
print('Custom Statement Tag : ', data.get('customStatementTag'))
35
​
36
@sio.event
37
def valid_token(data):
38
print('Token is valid')
39
send_binary_data()
40
@sio.event
41
def invalid_token(data):
42
print('Token is Invalid')
43
​
44
​
45
#This class helps us simulate a 16bit audio stream
46
class MicrophoneStream(object):
47
"""Opens a recording stream as a generator yielding the audio chunks."""
48
​
49
def __init__(self, rate, chunk):
50
self._rate = rate
51
self._chunk = chunk
52
​
53
# Create a thread-safe buffer of audio data
54
self._buff = queue.Queue()
55
self.closed = True
56
​
57
def __enter__(self):
58
self._audio_interface = pyaudio.PyAudio()
59
self._audio_stream = self._audio_interface.open(
60
format=pyaudio.paInt16,
61
# The API currently only supports 1-channel (mono) audio
62
# https://goo.gl/z757pE
63
channels=1,
64
rate=self._rate,
65
input=True,
66
frames_per_buffer=self._chunk,
67
# Run the audio stream asynchronously to fill the buffer object.
68
# This is necessary so that the input device's buffer doesn't
69
# overflow while the calling thread makes network requests, etc.
70
stream_callback=self._fill_buffer,
71
)
72
​
73
self.closed = False
74
​
75
return self
76
​
77
def __exit__(self, type, value, traceback):
78
self._audio_stream.stop_stream()
79
self._audio_stream.close()
80
self.closed = True
81
# Signal the generator to terminate so that the client's
82
# streaming_recognize method will not block the process termination.
83
self._buff.put(None)
84
self._audio_interface.terminate()
85
​
86
def _fill_buffer(self, in_data, frame_count, time_info, status_flags):
87
"""Continuously collect data from the audio stream, into the buffer."""
88
self._buff.put(in_data)
89
return None, pyaudio.paContinue
90
​
91
def generator(self):
92
while not self.closed:
93
# Use a blocking get() to ensure there's at least one chunk of
94
# data, and stop iteration if the chunk is None, indicating the
95
# end of the audio stream.
96
chunk = self._buff.get()
97
if chunk is None:
98
return
99
data = [chunk]
100
​
101
# Now consume whatever other data's still buffered.
102
while True:
103
try:
104
chunk = self._buff.get(block=False)
105
if chunk is None:
106
return
107
data.append(chunk)
108
except queue.Empty:
109
break
110
​
111
yield b"".join(data)
112
​
113
def get_token(apiKey, apiSecret, userId):
114
API_ENDPOINT = "https://api.marsview.ai/cb/v1/auth/create_access_token"
115
payload = {
116
"apiKey": apiKey,
117
"apiSecret": apiSecret,
118
"userId": userId
119
}
120
​
121
r = requests.post(url = API_ENDPOINT, data = payload)
122
token = json.loads(r.text)['data']['accessToken']
123
return token
124
​
125
def initiate_transaction(token, model_configs):
126
# importing the requests library
127
128
# defining the api-endpoint
129
API_ENDPOINT = "https://streams.marsview.ai/rb/v1/streams/setup_realtime_stream"
130
​
131
data = {
132
"channels":2,
133
"modelConfigs" : model_configs}
134
​
135
headers = {'Authorization': f'Bearer {token}'}
136
# sending post request and saving response as response object
137
r = requests.post(url = API_ENDPOINT, json = data, headers = headers)
138
139
pastebin_url = json.loads(r.text)['data']
140
txnId = pastebin_url['txnId']
141
channelId = pastebin_url['channels'][0]['channelId']
142
​
143
print("txnId : ", txnId)
144
print("channelId : ", channelId)
145
​
146
sio.connect('https://streams.marsview.ai', auth={'txnId': txnId, 'channelId': channelId, 'token':token})
147
sio.emit('startStream', '')
148
149
def send_binary_data():
150
151
152
RATE = 16000
153
CHUNK = int(RATE / 10)
154
​
155
print("* recording")
156
with MicrophoneStream(RATE, CHUNK) as stream:
157
audio_generator = stream.generator()
158
for content in audio_generator:
159
sio.emit('binaryData', content)
160
161
​
162
if __name__ == '__main__':
163
api_secret = <APISECRET>
164
api_key = <APIKEY>
165
user_id = <USERID>
166
token = get_token(api_key, api_secret, user_id)
167
model_configs = {
168
"intent_analysis":{
169
"intents":[]},
170
"custom_statement_tag_analysis":{
171
"statement_tag_ids":[],
172
"use_tag_level_threshold":True
173
},
174
}
175
initiate_transaction(token, model_configs)
Copied!
Copy link