def on_recording_done(self, tempFile): """ Triggered when the microphone stream has finished recording. Enables / Disables appropriate buttons and receives tempFile from the microphone stream. Also processes an FFT for display :param tempFile: (file handle: int, file path: str) temporary audio file generated by the microphone stream to store the audio while the user decides whether or not to save it""" self.state = 'saving' self.handle_chk_state() self.deleteBtn.setEnabled(True) self.recordBtn.setEnabled(False) self.recordShortCut.setEnabled(False) self.recordBtn.setIcon(QIcon(r'.\assets\record.png')) self.inputDropDown.setEnabled(True) self.tempFile = tempFile # Process FFT tChopped, vChopped, fVals, \ powerFFT, peakFreqs, peakAmps = Utils.AnalyzeFFT(tempFile[1], tChop=None) # Get these fields ready for a possible insertion into DB self.processed_fields['PCM'] = str(list(vChopped)) self.processed_fields['Date'] = str(datetime.datetime.now()) self.processed_fields['FFT_Processed'] = str(list(powerFFT)) self.processed_fields['Sample_Rate'] = str(self.stream.sampleRate) self.processed_fields['Hash'] = hashlib.sha256( str(list(powerFFT)).encode('utf-8')).hexdigest() self.processed_fields['Peaks_Processed'] = [] for freq, amp in zip(peakFreqs, peakAmps): self.processed_fields['Peaks_Processed'].append({ "Frequency": freq, "Amplitude": amp }) self.processed_fields['Peaks_Processed'].sort( reverse=True, key=lambda peak: peak['Amplitude']) self.processed_fields['Peaks_Processed'] = str( self.processed_fields['Peaks_Processed']) os.close(self.tempFile[0]) os.remove(self.tempFile[1]) # Make a new .wav file from the processed data self.tempFile = tempfile.mkstemp(prefix='temp_processed_', suffix='.wav', dir='') fileStream = soundfile.SoundFile(self.tempFile[1], mode='w', samplerate=self.stream.sampleRate, channels=max(self.stream.channels), subtype=self.stream.subtype) fileStream.write(vChopped) fileStream.close() self.plotCanvas.plot(tChopped, vChopped, fVals, powerFFT, peakFreqs, peakAmps, 'Impulse Recording') self.canvasStack.setCurrentWidget(self.plotCanvas)
def single_analyze_wav(self, filePath): """ Do an FFT and find peaks on a single wav file :param filePath: file path to .wav file """ tChopped, vChopped, fVals,\ powerFFT, peakFreqs, peakAmps = Utils.AnalyzeFFT(filePath, tChop=self.settings['processing']['tChop'], detail=self.settings['processing']['detail']) self.analyzeDone.emit(tChopped, vChopped, fVals, powerFFT, peakFreqs, peakAmps, filePath) self.update_table(peakFreqs, peakAmps)