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