Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wav file is empty after recording is saved to file #157

Open
MarkoKurtovic opened this issue Oct 2, 2022 · 5 comments
Open

Wav file is empty after recording is saved to file #157

MarkoKurtovic opened this issue Oct 2, 2022 · 5 comments

Comments

@MarkoKurtovic
Copy link

import soundcard as sc
import numpy
import soundfile

# get a list of all speakers:
speakers = sc.all_speakers()

# get the current default speaker on your system:
default_speaker = sc.default_speaker()

# get a list of all microphones:
mics = sc.all_microphones()

# get the current default microphone on your system:
default_mic = sc.default_microphone()
one_speaker = sc.get_speaker('IEC958')



# record and play back one second of audio:
data = default_mic.record(samplerate=48000, numframes=48000)
default_speaker.play(data/numpy.max(data), samplerate=48000)

# alternatively, get a `Recorder` and `Player` object
# and play or record continuously:
with default_mic.recorder(samplerate=48000) as mic, \
      default_speaker.player(samplerate=48000) as sp:
    for _ in range(400):
        data = mic.record(numframes=1024)
        sp.play(data)
        soundfile.write("x.wav", data, 1024)
        soundfile.read("x.wav")
        
@MarkoKurtovic
Copy link
Author

I got few seconds of wav file but can't hear anything.
I tried to change numframes, no luck

@bastibe
Copy link
Owner

bastibe commented Oct 3, 2022

What sound card are you using? Is it the correct one?

@MarkoKurtovic
Copy link
Author

Not sure which one, but recording and playing is working

@MarkoKurtovic
Copy link
Author

@bastibe

This is what I get when I print all_speakers() and all_microfons()

<Speaker Logi USB Headset Analog Stereo (2 channels)>
[<Microphone Logi USB Headset Analog Mono (1 channels)>, <Microphone HD-Audio Generic Analog Stereo (2 channels)>]

@bastibe
Copy link
Owner

bastibe commented Oct 4, 2022

It might be that the sample rates of your sound cards are out-of-sync. Try reversing the order of play and record.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants