Skip to content

Instantly share code, notes, and snippets.

@Mostafa-Hamdy-Elgiar
Created April 10, 2016 18:51
Show Gist options
  • Save Mostafa-Hamdy-Elgiar/c7b3984a204630b3ca106b0fcef46dc6 to your computer and use it in GitHub Desktop.
Save Mostafa-Hamdy-Elgiar/c7b3984a204630b3ca106b0fcef46dc6 to your computer and use it in GitHub Desktop.
import pymedia.audio.sound as sound
import pymedia.audio.acodec as acodec
import time
import sys
import wx
from threading import Thread
app = wx.App()
class frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self , parent=None, title="Recorder" )
self.text1 = wx.TextCtrl(self , -1 , pos=(120,20) , size=(150,20))
self.text2 = wx.TextCtrl(self , -1 , pos=(120,50) , size=(150,20))
self.text3 = wx.TextCtrl(self , -1 , pos=(120,80) , size=(150,20))
self.label1 = wx.StaticText(self , -1 , pos = (10,20),label= " File name : ")
self.label2 = wx.StaticText(self , -1 , pos = (0,50),label= " Number Of seconds: ")
self.label3 = wx.StaticText(self , -1 , pos = (10,80),label= " Place IN : ")
self.label4 = wx.StaticText(self , -1 , pos = (150,170),label= " ")
button1 = wx.Button(self , -1 , pos = (300,80) , label = 'Browse')
button2 = wx.Button(self , -1 , pos = (200,120) , label = 'Recorde')
button3 = wx.Button(self , -1 , pos = (200,150) , label = 'Stop')
self.Bind(wx.EVT_BUTTON , self.startrecorde , button2)
self.Bind(wx.EVT_BUTTON , self.Browse , button1)
self.Bind(wx.EVT_BUTTON , self.stoprecorde , button3)
def recorder (self ) :
self.label4.SetLabel('Speeking Now')
path1 = self.text3.GetValue()
name = str(self.text1.GetValue())
filename = path1 + '\\' + name
f = open(filename , 'wb')
params={'id' : acodec.getCodecID('mp3') ,
'bitrate' : 128000 ,
'sample_rate': 44100 ,
'channels' : 2 }
ac=acodec.Encoder(params)
self.snd = sound.Input(44100 , 2 ,sound.AFMT_S16_LE)
self.snd.start()
while self.snd :
s=self.snd.getData()
if s and len(s) :
frames=ac.encode(s)
for fr in frames :
f.write(fr)
else :
time.sleep(0.003)
def Browse (self , event) :
dlg=wx.DirDialog(self , 'select your directory')
if dlg.ShowModal() == wx.ID_OK :
path=dlg.GetPath()
self.text3.SetValue(path)
def startrecorde (self , event) :
Thread(target=self.recorder).start()
def stoprecorde (self , event) :
time.sleep(0.003)
self.snd.stop()
self.label4.SetLabel('Recording Finshed')
frame().Show()
app.MainLoop()
@Mostafa-Hamdy-Elgiar
Copy link
Author

This is a part of my graduation project which is called multimedia programming using Python. This code is responsible for recording the sound into files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment