bastibe/SoundCard

Record audio unlimited

zioalex opened this issue · 6 comments

Hi, I was trying to setup my RP4 to record the audio without specifying a fixed number of frames, allowing it to record unlimited but with the possibility to stop it on a specific condition.

Can you help with it?
I didn't find anything useful so far.

Thanks,
Alessandro

With record(numframes=None), soundcard will record whatever is available at this very moment, without blocking. Usually a very short amount of data.

If you want to record unlimited audio, run record repeatedly.

HI @bastibe,
I had the chance to try it but it doesn't work as expected.
When I run
data = default_mic.record(samplerate=44100, numframes=None, channels=2)
it exit immediately and data is empty while if works correctly if I define a fixed number of frames to record.

What I need is way to start/stop the recording on demand and it can go on for a unknown time.

default_mic.record will start/stop the recording for each call, which is not what you want.

Use the recorder context manager instead, which will keep the recorder going between records:

with default_mic.recorder(...) as rec:
    while True:
        rec.record(...)

Also, please read the documentation. This is not a help forum, but an issue board.

Hi @bastibe,
thanks.
I didn't find it in the doc and implemented my own way:

    while condition:
        if condition != condition_pre:
            print("Start recording")
        else:
            print(".", end = '')
        # Read data from device
        # Record the data from the MIC - together while used by Chromium
        data = default_mic.record(samplerate=44100, numframes=44100, channels=2)
        time.sleep(.001)
        print(type(data),data.size, data.ndim, data.dtype, type(data_full), data_full.ndim, data_full.size, data_full.dtype)
        data_full = np.concatenate((data_full, data))
        print(data)
        #print(data_full)
        condition=os.path.isfile(recording_toggle)
    
        if condition_pre != "first_run" and condition != condition_pre:
            print("\n################################ Stop Recording ##################################")
            # Play 
            #import numpy
            default_speaker.play(data/np.max(data_full), samplerate=44100,channels=2)

            # Write to the file in WAV
            import soundfile
            #f = open("./soundcard.dat", 'wb')
            #f.write(data)
            print("Wrting the file")
            soundfile.write("./soundcard.wav", data_full, 44100)
            print(soundfile.read("./soundcard.wav"))
            
        condition_pre = condition

But the way you suggested looks better.
Is there a support forum about this project?

You're doing it wrong and will get incorrect results. As I said before, use a recorder context manager.

Hi @bastibe, surely it is better with the context manager, but it works correctly also as I did.