Python Forum
Using pyaudio to stop recording under certain sound threshold
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using pyaudio to stop recording under certain sound threshold
#1
Heads up, this is my first real programming project, but I'm really dedicated to making it work and would love some input.

I've written a program to attempt to record sound using Pyaudio and then halt the recording once the sound intensity has dropped under a certain threshold. To do this, I took the audio data, turned it into integer data, averaged the chunks of data collected by the program, and then set the program to halt after it dropped below a threshold that I picked after some trial and error. The issue is that, the averages of the data clusters don't seem to actually correlate to the intensity of the audio input, and it sometimes drops below the threshold and halts recording even when there is significant input (eg, constant music playing). Below is the code:

import pyaudio
import struct
import wave
    
def record(outputFile):
#defining audio variables
    chunk = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    RATE = 44100
    Y = 100
    
#Calling pyadio module and starting recording 
    p = pyaudio.PyAudio()

    stream = p.open(format=FORMAT,
                channels=CHANNELS, 
                rate=RATE, 
                input=True,
                frames_per_buffer=chunk)

    stream.start_stream()
    print("Starting!")

#Recording data until under threshold
    frames=[]
    
    while True:
        #Converting chunk data into integers
        data = stream.read(chunk)
        data_int = struct.unpack(str(2*chunk) +'B', data)
        #Finding average intensity per chunk
        avg_data=sum(data_int)/len(data_int)
        print(str(avg_data))
        #Recording chunk data
        frames.append(data)
        if avg_data < Y:
            break
        
#Stopping recording   
    stream.stop_stream()
    stream.close()
    p.terminate()
    print("Ending recording!")
   
    
#Saving file with wave module    
    wf = wave.open(outputFile, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(b''.join(frames))
    wf.close()
    
record('outputFile1.wav')
Reply


Messages In This Thread
Using pyaudio to stop recording under certain sound threshold - by Twanski94 - Jun-13-2020, 01:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyaudio seems to randomly halt input. elpidiovaldez5 2 465 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,352 Feb-24-2023, 06:14 PM
Last Post: deanhystad
Smile How we can prevent screen recording murad_ali 3 1,930 Jul-29-2022, 10:29 AM
Last Post: DeaD_EyE
  How to decrease latency while recording streaming video. unicorn2019 0 1,306 Nov-15-2021, 02:12 PM
Last Post: unicorn2019
  Help with storing temp data for each day then recording min/max in app. trthskr4 3 2,501 Sep-10-2021, 10:51 PM
Last Post: trthskr4
  Not getting response from pyaudio OceansBlue 1 2,730 Jul-03-2021, 06:22 AM
Last Post: OceansBlue
  Split recording with Picamera EvanS1 0 1,995 Jun-19-2021, 12:26 PM
Last Post: EvanS1
  help with PyAudio Leo12143 1 2,013 Jan-18-2021, 09:56 AM
Last Post: DT2000
  pyAudio playing variable Talking2442 3 3,107 Dec-01-2020, 06:20 PM
Last Post: Talking2442
  Pyaudio Souls99 7 3,704 Oct-05-2020, 04:06 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020