Git Product home page Git Product logo

Comments (12)

mgeier avatar mgeier commented on July 28, 2024

Hmmm, I don't know what's going on there ...

Can you please print the return value of sd.wait()?

Can you please try some values for blocksize and see how that relates to the number of zeros you get?

from python-sounddevice.

mathiassmichno avatar mathiassmichno commented on July 28, 2024

I modified the snippet slightly it is now:

import sounddevice as sd
from scipy.io import wavfile
import numpy as np
from pprint import pprint
import begin

@begin.start(auto_convert=True)
def main(sound_device: 'Sound Device Name' = 'DMX 6Fire ASIO', 
         channel_map: 'Channel Mapping' = [1,2], 
         duration : 'Duration of recording' = 1., 
         out_file: 'Wavfile to save recording in' = 'out.wav',
         blocksize: 'Block size (0 is optimal but may vary)' = 0,
         loops: 'Number of recordings in loop' = 1,
         format: 'dtype of recording' = 'float32'):
    dev = sd.query_devices(sound_device)
    pprint(dev)
    sd.default.device = sound_device
    sd.default.dtype = format
    sd.default.blocksize = blocksize
    print('Starting Recording')
    channel_map = [int(c) for c in channel_map]
    for i in range(loops):
        print('Loop #', i)
        rec = sd.rec(int(duration * dev['default_samplerate']), samplerate=int(dev['default_samplerate']), mapping=channel_map)
        print('  sd.wait() ->', sd.wait())
        for channel in range(rec.shape[1]):
            non_silence = np.where(rec[:,channel] > float(0))[0]
            print('  Ch. {}, initial silence {} samples'.format(channel, non_silence[0]))
        wavfile.write(str(i) + out_file, int(dev['default_samplerate']), rec)  

I tried to use a WASAPI device and recorded audio ten times in a loop.
It gave the following result (I also included the return of sd.wait(), but it is always None)

#: python audiorec.py -s "In 1 WASAPI" -b 1024 -l 10

{'default_high_input_latency': 0.01,
 'default_high_output_latency': 0.0,
 'default_low_input_latency': 0.003,
 'default_low_output_latency': 0.0,
 'default_samplerate': 48000.0,
 'hostapi': 3,
 'max_input_channels': 2,
 'max_output_channels': 0,
 'name': 'USBPAL In1 L/R (Terratec DMX 6fire WDM Audio)'}
Starting Recording
Loop # 0
  sd.wait() -> None
  Ch. 0, initial silence 256 samples
  Ch. 1, initial silence 256 samples
Loop # 1
  sd.wait() -> None
  Ch. 0, initial silence 447 samples
  Ch. 1, initial silence 477 samples
Loop # 2
  sd.wait() -> None
  Ch. 0, initial silence 256 samples
  Ch. 1, initial silence 256 samples
Loop # 3
  sd.wait() -> None
  Ch. 0, initial silence 0 samples
  Ch. 1, initial silence 1 samples
Loop # 4
  sd.wait() -> None
  Ch. 0, initial silence 256 samples
  Ch. 1, initial silence 256 samples
Loop # 5
  sd.wait() -> None
  Ch. 0, initial silence 256 samples
  Ch. 1, initial silence 256 samples
Loop # 6
  sd.wait() -> None
  Ch. 0, initial silence 265 samples
  Ch. 1, initial silence 256 samples
Loop # 7
  sd.wait() -> None
  Ch. 0, initial silence 256 samples
  Ch. 1, initial silence 256 samples
Loop # 8
  sd.wait() -> None
  Ch. 0, initial silence 256 samples
  Ch. 1, initial silence 256 samples
Loop # 9
  sd.wait() -> None
  Ch. 0, initial silence 257 samples
  Ch. 1, initial silence 268 samples

Changing the blocksize did not see to affect the Asio device; unless I set the blocksize to the same as the samples displayed under "Asio latency" in the driver software, which yields about half the initial silence as any other value.

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

I think your code np.where(rec[:,channel] > float(0))[0][0] doesn't yield the number of zeros but rather the number of non-positive values before the first positive value. Shouldn't it be rather something like rec[:, channel].nonzero()[0][0]?

I also included the return of sd.wait(), but it is always None

OK, that's good, that means that there are no over/underflows.
You should probably only print this when its not None.

from python-sounddevice.

mathiassmichno avatar mathiassmichno commented on July 28, 2024

I'm getting the same results, moreover I can confirm the silence when importing the out file in audacity and manually finding the first non zero sample.

However when I record om a MME device I am not getting any initial silence. Could it be an issue with PortAudio?

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

OK, if it doesn't happen in MME, it sure looks like a PortAudio issue.

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

You should probably ask there: http://www.portaudio.com/contacts.html

from python-sounddevice.

hiccup7 avatar hiccup7 commented on July 28, 2024

@mathiassmichno - You could try libportaudio64bit.dll from the portaudio-19.6.0-win branch at https://github.com/spatialaudio/portaudio-binaries
portaudio has fixed a lot of bugs since revision 1899 bundled in the sounddevice 0.3.5 Windows wheel.

from python-sounddevice.

hiccup7 avatar hiccup7 commented on July 28, 2024

Another option is to install the wheel with the updated PortAudio library from http://www.lfd.uci.edu/~gohlke/pythonlibs/#sounddevice

from python-sounddevice.

mathiassmichno avatar mathiassmichno commented on July 28, 2024

I downloaded the wheel from http://www.lfd.uci.edu/~gohlke/pythonlibs/#sounddevice
And it fixed the problem, at least according to my small test script.

Thanks @hiccup7!

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

I've just made a new release.
Can you please check if the problem is fixed now?

from python-sounddevice.

mathiassmichno avatar mathiassmichno commented on July 28, 2024

According to my small test, the problem is fixed, and the initial silence is gone.

from python-sounddevice.

mgeier avatar mgeier commented on July 28, 2024

OK, thanks!

from python-sounddevice.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.