0

I am a noob in audio processing using python. I have got the following python code from internet to perform live audio recording. Whenever, I record using the following code, there is a block of zeros in the beginning of the recorded audio file. The recording doesn't start immediately and it adds zeros until the recording starts.

I want the recording to happen immediately i run code without any zeros in the beginning. Any help is appreciated.

import argparse
import tempfile
import queue
import sys


def int_or_str(text):
    """Helper function for argument parsing."""
    try:
        return int(text)
    except ValueError:
        return text

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
    '-l', '--list-devices', action='store_true',
    help='show list of audio devices and exit')
parser.add_argument(
    '-d', '--device', type=int_or_str,
    help='input device (numeric ID or substring)')
parser.add_argument(
    '-r', '--samplerate', type=int, help='sampling rate')
parser.add_argument(
    '-c', '--channels', type=int, default=1, help='number of input channels')
parser.add_argument(
    'filename', nargs='?', metavar='FILENAME',
    help='audio file to store recording to')
parser.add_argument(
    '-t', '--subtype', type=str, help='sound file subtype (e.g. "PCM_24")')
args = parser.parse_args()

try:
    import sounddevice as sd
    import soundfile as sf
    import numpy  # Make sure NumPy is loaded before it is used in the callback
    assert numpy  # avoid "imported but unused" message (W0611)

    if args.list_devices:
        print(sd.query_devices())
        parser.exit(0)
    if args.samplerate is None:
        device_info = sd.query_devices(args.device, 'input')
        # soundfile expects an int, sounddevice provides a float:
        args.samplerate = int(device_info['default_samplerate'])
    if args.filename is None:
        args.filename = tempfile.mktemp(prefix='delme_rec_unlimited_',
                                        suffix='.wav', dir='')
    q = queue.Queue()

    def callback(indata, frames, time, status):
        """This is called (from a separate thread) for each audio block."""
        if status:
            print(status, file=sys.stderr)
        q.put(indata.copy())

    # Make sure the file is opened before recording anything:
    with sf.SoundFile(args.filename, mode='x', samplerate=args.samplerate,
                      channels=args.channels, subtype=args.subtype) as file:
        with sd.InputStream(samplerate=args.samplerate, device=args.device,
                            channels=args.channels, callback=callback):
            print('#' * 80)
            print('press Ctrl+C to stop the recording')
            print('#' * 80)
            while True:
                file.write(q.get())

except KeyboardInterrupt:
    print('\nRecording finished: ' + repr(args.filename))
    parser.exit(0)
except Exception as e:
    parser.exit(type(e).__name__ + ': ' + str(e))
alpereira7
  • 1,522
  • 3
  • 17
  • 30
  • What OS and host API are you using? It works for me without zeros on Linux/ALSA. – Matthias Sep 03 '18 at 11:19
  • @Matthias I am using windows 10/WASAPI. But I am not sure about the host API. How can I find that? –  Sep 03 '18 at 11:31
  • WASAPI is the host API! You find the host API at the end of each line in the list of devices. I think the zeros you are observing are caused by the underlying PortAudio library. You should ask your question on the PortAudio mailing list: http://www.portaudio.com/contacts.html. – Matthias Sep 03 '18 at 11:59
  • thanks for your suggestions. @Matthias –  Sep 03 '18 at 12:21

0 Answers0