prerequisites: the libraries i need for this small project are tkinter(for making gui app),threading(to use the threading option to access one member function's variable),pyaudio(to access the default microphone and default speakers), wave(for creation and operation of .wav format files)
first lets have a look on the code:
import tkinter as tk
chunk = 1024
sample_format = pyaudio.paInt16
channels = 2
fs = 44100
frames = 
def __init__(self, master):
self.isrecording = False
self.button1 = tk.Button(main, text='rec',command=self.startrecording)
self.button2 = tk.Button(main, text='stop',command=self.stoprecording)
self.p = pyaudio.PyAudio()
self.stream = self.p.open(format=self.sample_format,channels=self.channels,rate=self.fs,frames_per_buffer=self.chunk,input=True)
self.isrecording = True
t = threading.Thread(target=self.record)
self.isrecording = False
self.filename = self.filename+".wav"
wf = wave.open(self.filename, 'wb')
data = self.stream.read(self.chunk)
main = tk.Tk()
app = App(main)
to get the understanding of tkinter please go here
, i made a gui app to count the number of characters.
another gui project that i made was attendance manager using tkinter ,go here
So lets talk about the recording then
The chunk is like a buffer, so therefore each buffer will contain 1024 samples, which you can then either keep or throw away. We use chunks of data, instead of a continuous amount of audio because of processing power
Waveform Audio File Format (WAVE, or more commonly known as WAV due to its filename extension; pronounced "wave" is an audio file format standard, developed by Microsoft and IBM, for storing an audio bitstream on PCs. It is an application of the Resource Interchange File Format (RIFF) bitstream format method for storing data in "chunks".
For CD recordings, the industry standard is to store each audio sample as a 16-bit value, at 44100 samples per second.
Sound Channel refers to the independent audio signal which is collected or playback when the sound is recording or playback in different spatial position. Therefore, the number of channel is the amount of sound source when the sound is recording or the relevant speaker number when it is playback.
The pyaudio.Pyaudio helps to create the interface for recording the sound,and creates an object for microphones and speakers. And well the open function opens the required stream for recording.
To execute the application follow the screen shots:
1) first run the script , here name of the python file is recgui.py
2) then press the rec button for recording
3)after recording voice press stop
4)input the file name , hit enter. The application will close automatically.
5)and you will find your recording in the directory where you have kept your recgui.py