Python Forum
Using pyaudio to stop recording under certain sound threshold - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Using pyaudio to stop recording under certain sound threshold (/thread-27601.html)



Using pyaudio to stop recording under certain sound threshold - Twanski94 - Jun-13-2020

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')



RE: Using pyaudio to stop recording under certain sound threshold - Larz60+ - Jun-13-2020

google this verbatim: 'use pyaudio to detect motion near microphone'
it should bring you to a place where you can read the section of "Security with Intelligent Computing and Big-Data Services 2019: Proceedings of the 3rd International Conference on Security with Intelligent Computing ... in Intelligent Systems and Computing (1145))"
where something very similar was done to create a motion detector, using pyaudio.
The book costs $220.00, so I expect you want to purchase a copy. But you should be allowed to read the section of interest. (starts on page 296) and shows references.


RE: Using pyaudio to stop recording under certain sound threshold - Twanski94 - Jun-13-2020

Quote: google this verbatim: 'use pyaudio to detect motion near microphone'
it should bring you to a place where you can read the section of "Security with Intelligent Computing and Big-Data Services 2019: Proceedings of the 3rd International Conference on Security with Intelligent Computing ... in Intelligent Systems and Computing (1145))"
where something very similar was done to create a motion detector, using pyaudio.
The book costs $220.00, so I expect you want to purchase a copy. But you should be allowed to read the section of interest. (starts on page 296) and shows references.
Find Rep
* Quote Multi-Quote Report

Thanks for the reply! I'll check it out