Skip to content

Commit

Permalink
Merge branch 'optimizeRecording'
Browse files Browse the repository at this point in the history
  • Loading branch information
aaclause committed Dec 28, 2023
2 parents f7a3b72 + 7009653 commit 0e0788d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 29 deletions.
10 changes: 9 additions & 1 deletion addon/globalPlugins/openai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
"useCustomPrompt": "boolean(default=False)",
"customPromptText": 'string(default="")'
},
"audio": {
"sampleRate": "integer(min=8000, max=48000, default=16000)",
"channels": "integer(min=1, max=2, default=1)",
"dtype": "string(default=int16)"
},
"renewClient": "boolean(default=False)",
"debug": "boolean(default=False)"
}
Expand Down Expand Up @@ -491,5 +496,8 @@ def script_toggleRecording(self, gesture):
self.recordtThread.stop()
self.recordtThread = None
else:
self.recordtThread = RecordThread(self.getClient())
self.recordtThread = RecordThread(
self.getClient(),
conf=conf["audio"]
)
self.recordtThread.start()
14 changes: 11 additions & 3 deletions addon/globalPlugins/openai/maindialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,11 @@ def onRecord(self, evt):
self.recordBtn.SetLabel(_("Stop &recording") + " (Ctrl+R)")
self.recordBtn.Bind(wx.EVT_BUTTON, self.onStopRecord)
self.recordBtn.Enable()
self.worker = RecordThread(self.client, self)
self.worker = RecordThread(
self.client,
self,
conf=self.conf["audio"]
)
self.worker.start()

def onRecordFromFilePath(self, evt):
Expand All @@ -1306,12 +1310,16 @@ def onRecordFromFilePath(self, evt):
)
if dlg.ShowModal() != wx.ID_OK:
return
filename = dlg.GetPath()
fileName = dlg.GetPath()
self.message(_("Processing, please wait..."))
winsound.PlaySound(f"{ADDON_DIR}/sounds/progress.wav", winsound.SND_ASYNC|winsound.SND_LOOP)
self.disableButtons()
self.historyText.SetFocus()
self.worker = RecordThread(self.client, self, filename)
self.worker = RecordThread(
self.client,
self,
fileName
)
self.worker.start()

def onTextToSpeech(self, evt):
Expand Down
55 changes: 30 additions & 25 deletions addon/globalPlugins/openai/recordthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,67 +46,73 @@ def retrieveTranscription(transcription):

class RecordThread(threading.Thread):

def __init__(self, client, notifyWindow=None, pathList=None):
def __init__(self, client, notifyWindow=None, pathList=None, conf=None):
super(RecordThread, self).__init__()
self._notifyWindow = notifyWindow
self.client = client
self.pathList = pathList
self.stop_record = False
self.conf = conf
self._stopRecord = False
self._notifyWindow = notifyWindow
self._wantAbort = 0
self._recording = False
self.audio_data = np.array([], dtype='int16')

def run(self):
if self.pathList:
self.process_transcription(self.pathList)
return
framerate = 44100
if not self.conf:
self.conf = {
"channels": 1,
"sampleRate": 16000,
"dtype": "int16",
}
self.audioData = np.array([], dtype=self.conf["dtype"])
filename = self.get_filename()
tones.beep(200, 100)
self.record_audio(framerate)
self.record_audio(self.conf["sampleRate"])
tones.beep(200, 200)
winsound.PlaySound(f"{ADDON_DIR}/sounds/progress.wav", winsound.SND_ASYNC|winsound.SND_LOOP)

if self._wantAbort:
return
self.save_wav(
filename,
self.audio_data,
framerate
self.audioData,
self.conf["sampleRate"]
)
if self._notifyWindow:
self._notifyWindow.message(_("Transcribing..."))
self.process_transcription(filename)

def record_audio(self, framerate):
chunk_size = 1024 # Vous pouvez ajuster la taille du bloc selon vos besoins
channels = 2
dtype = 'int16'
def record_audio(self, sampleRate):
chunk_size = 1024
self._recording = True

with sd.InputStream(samplerate=framerate, channels=channels, dtype=dtype) as stream:
while not self.stop_record and self._recording:
with sd.InputStream(
samplerate=sampleRate,
channels=self.conf["channels"],
dtype=self.conf["dtype"],
) as stream:
while not self._stopRecord and self._recording:
frame, overflowed = stream.read(chunk_size)
if overflowed:
print("Warning: audio buffer has overflowed.")
self.audio_data = np.append(self.audio_data, frame)
log.error("Audio buffer has overflowed.")
self.audioData = np.append(self.audioData, frame)
if self._wantAbort:
break

self._recording = False

def save_wav(self, filename, data, framerate):
def save_wav(self, filename, data, sampleRate):
if self._wantAbort:
return
wavefile = wave.open(filename, "wb")
wavefile.setnchannels(2)
wavefile.setsampwidth(2)
wavefile.setframerate(framerate)
wavefile.setnchannels(self.conf["channels"])
wavefile.setsampwidth(2) # 16 bits
wavefile.setframerate(sampleRate)
wavefile.writeframes(data.tobytes())
wavefile.close()

def stop(self):
self.stop_record = True
self._stopRecord = True
self._recording = False

def get_filename(self):
Expand Down Expand Up @@ -135,6 +141,5 @@ def process_transcription(self, filename):
core.callLater(200, retrieveTranscription, transcription)

def abort(self):
self.stop_record = 1
self._stopRecord = 1
self._wantAbort = 1

0 comments on commit 0e0788d

Please sign in to comment.