0
$\begingroup$

I saved "wav" file with below code.

However, I can't hear any sound from "wav" file.

Is there any solution?

import pyaudio 
import wave
from datetime import datetime

print('Pyaudio version: ', pyaudio.__version__)

########################################################################################################

p = pyaudio.PyAudio()

for i in range(p.get_device_count()):
    print(p.get_device_info_by_index(i).get('name'))
    
########################################################################################################
    
form_1 = pyaudio.paInt16 
chans = 1 
samp_rate = 44100 
chunk = 1024 
record_secs = 5 

num = 1

while True:
    
    audio = pyaudio.PyAudio() # create pyaudio instantiation

    # create pyaudio stream
    stream = audio.open(format = form_1, rate = samp_rate, channels = chans, \
                        input = True, input_device_index = 1, frames_per_buffer=chunk)
    
    frames = []
    
    start_time = datetime.now()

    # loop through stream and append audio chunks to frame array
    for j in range(0, int((samp_rate/chunk)*record_secs)): # 53 | (samp_rate/chunk)*record_secs)*chunk = samp_rate*record_secs 
        data = stream.read(chunk)
        frames.append(data)
        
    end_time = datetime.now()

    # stop the stream, close it, and terminate the pyaudio instantiation
    stream.stop_stream()
    stream.close()
    audio.terminate()
    
    # name of .wav file
    wav_output_filename = './Record/%d_%s_%s_%s.wav' % (num, start_time.strftime('%Y%m%d'), start_time.strftime('%H%M%S'), end_time.strftime('%H%M%S'))
    
    num+=1

    # save the audio frames as .wav file
    wavefile = wave.open(wav_output_filename,'wb')
    wavefile.setnchannels(chans)
    wavefile.setsampwidth(audio.get_sample_size(form_1))
    wavefile.setframerate(samp_rate)
    wavefile.writeframes(b''.join(frames))
    wavefile.close()

My id is blocked from Stackoverflow due to low quality questions.

If someone can ask instead of me for this question to Stackoverflow, then it will be so much appreciated.

I totally tested same code of below code but my wav file doesn't have any sound.

https://www.youtube.com/watch?v=wE-122yZaCE

$\endgroup$
8
  • 1
    $\begingroup$ Why are you using such a complicated way to write a .wav file? $\endgroup$
    – Jdip
    Commented Aug 31 at 8:32
  • 1
    $\begingroup$ The code is fine. How do you know there is "no sound"? What input and output devices are you using and how are they configured? $\endgroup$
    – Hilmar
    Commented Aug 31 at 12:07
  • $\begingroup$ I believe there’s quite some information missing here. We don’t really know setup here. Is there any sound actually in the stream at all? Have you tried using other devices? Is your hardware working properly? I believe you should provide some more information for people to be able to help. $\endgroup$
    – ZaellixA
    Commented Aug 31 at 12:39
  • $\begingroup$ The problem is probably simple but of course that doesn't help. I tend to attack these problems head on. If I were you, I would add some debug to do things like get the min/max values of each captured frame, plus maybe the energy. Based on the audio source, you should expect some non-zero values, and perhaps some variation. You might also, for practice and debugging, write a simple program to read your wav file and again print some statistics per frame. With a little more work, you could use matplotlib to display your waveforms - it is good practice. $\endgroup$
    – gschro
    Commented Aug 31 at 14:09
  • $\begingroup$ @Jdip I am not a good coder so I have to just refer some famous code. $\endgroup$
    – MCPMH
    Commented Sep 1 at 0:39

1 Answer 1

1
$\begingroup$

Information from comments

So, in the comments, you say that the exact same code works when run on your lab computer, but it fails when you run it on your personal computer. To second gschro’s comment, the problem must be quite simple and I suspect it could be the device you use to record.

For example, my laptop has three recording devices available; some built-in multi-microphone setup, the same setup with all channels mixed to mono and a "Microsoft Teams Audio” device.

Now, if I were to use your code and pick the “Microsoft Teams Audio”, I would most probably get nothing out of it, as (if I understand it correctly) it is meant to get audio from and to the Microsoft Teams software and is not to be used as a generic audio device.

Suggestion

An easy way to “debug” this issue is to print all available devices on each machine and then pick the one you want to record. I am not good with Python, so I won’t try to provide a complete solution. However, adapting some code found online, I provide a “simple” code snippet below (please be aware that it is not tested and should be treated with care). The sources are presented after the snippet with some brief descriptions.

# Import packages/libraries
import pyaudio

# Make a PyAudio instant
audio = pyaudio.PyAudio()

# Enumerate the available devices (you may have issues with ASIO,
# see description below
for i in range(audio.get_device_count()):
    print(audio.get_device_info_by_index(i))

# Pick a device ID and convert to integer
devID = input(“Please pick a device ID: )
devID = int(devID)

# ---------------------------------------------
# Continue with your code but use the chosen
# device ID to record, like below
# ---------------------------------------------

# Create PyAudio stream
stream = audio.open(format = form_1, rate = samp_rate, channels = chans, \
                        input = True, input_device_index = devID, frames_per_buffer=chunk)

Some more info about the code

As already mentioned, the code is not tested and should be treated with caution. Hopefully, if you run it, you will get the appropriate error messages from the interpreter.

Now, below I provide the sources I got the code from and some information from the sources.

[1] Device enumeration: I got it from this StackOverflow question. The question is regarding an issue with the ASIO devices not being enumerated. There are some suggestions in the answers if you end up having the same issue.

[2] Keyboard input: I got some info about the input() function from here. I am sure there may be better and more elegant ways to do it, but as I said, I am not a Python programmer so I provided a simple solution to start with.

[3] Input checks: Please keep in mind that there are zero input checks performed (unless the int() function does some), and you may end up with errors if an incorrect value is provided. Or worse, if the function converts the input to some “garbage” value without producing errors you may end up with non-sensical problems. I believe that if you stick to integers for debugging purposes you’ll be fine. (you can find a quite simple solution in this StackOverflow answer)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.