swharden/Python-GUI-examples

Register audio

giorgionanfa opened this issue · 0 comments

Hi, your work is very interesting! I would like to register the audio in a file.wav after stopping the real time stream. Hence, i add a frames list in SWHear.py and i update it in this way:

def stream_readchunk(self):
"""reads some audio and re-launches itself"""

    try:
        self.data = np.fromstring(self.stream.read(self.chunk, exception_on_overflow=False),dtype=np.int16)
        self.fftx, self.fft = getFFT(self.data,self.rate)
        for i in range(0, int(self.rate / self.chunk*5)):
            frames.append(self.stream.read(self.chunk, exception_on_overflow=False))

Then, i pass frames to go.py and save the audio in this way:
waveFile = wave.open("test_file.wav", 'wb')
waveFile.setnchannels(1)
waveFile.setsampwidth(pyaudio.PyAudio().get_sample_size(pyaudio.paInt16))
waveFile.setframerate(44100)
waveFile.writeframes(b''.join(SWHear.frames))
waveFile.close()

Now, my problem is that the for loop that i added in SWHear.py creates a great delay in the graphic interface. If i remove it, the saved test_file.wav is sampled uncorrectly. Do you have some idea to solve this problem? Or a simpler way to implement this thing?

Moreover, sometimes i observe the following error:

ALSA lib pcm_dsnoop.c:638:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dsnoop.c:638:(snd_pcm_dsnoop_open) unable to open slave
Expression 'ret' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1736

Using the microphone of my pc, it seems that only sometimes the program does not recognize it. Why?
Thanks in advance,
Giorgio