# -*- coding: utf-8 -*- import time from ctypes import windll, c_buffer GlobalPVersion='1.1' class mci: def __init__(self): self.w32mci = windll.winmm.mciSendStringA self.w32mcierror = windll.winmm.mciGetErrorStringA def send(self,commande): """Sends a command to mciSendStringA and returns (errorcode,buffer). If there is an error, then it will return (errorcode,description). """ buffer = c_buffer(255) errorcode = self.w32mci(str(commande),buffer,254,0) if errorcode: # There was an error return errorcode, self.get_error(errorcode) else: # commande returned 0 return errorcode,buffer.value def get_error(self,error): """Given an error code, will return a description of the error.""" error = int(error) buffer = c_buffer(255) self.w32mcierror(error,buffer,254) return buffer.value def directsend(self, txt): (err,buf)=self.send(txt) if err != 0: print'Erreur',str(err),'sur',txt,':',buf return (err,buf) def record(self): print"Record" self.directsend('open new type waveaudio alias toto') self.directsend('Set toto time format milliseconds') self.directsend('set toto bitspersample 8') # 8 bits ou 16 bits self.directsend('set toto samplespersec 11025') # 11025 low quality 22050 medium quality 44100 high quality (CD) self.directsend('set toto channels 1') # 1 mono 2 stereo self.directsend('record toto') time.sleep(0.1) def finrecord(self, nomfichier): self.directsend('stop toto') self.directsend('save toto '+nomfichier) self.directsend('close toto') #self.directsend('delete toto') def play(self, nomfichier): self.directsend('open "'+nomfichier+'" alias toto') self.directsend('set toto time format milliseconds') err,duree=self.directsend('status toto length ') self.directsend('play toto from 0 to '+str(duree)) time.sleep(float(duree)/1000.0) self.directsend('stop toto') self.directsend('close toto') if __name__=='__main__': """ message=mci() message.record() for i in xrange(5): time.sleep(1) print 5-i print "Fin" message.finrecord("C:\\test.wav") """ canal=mci() canal.play("C:\\test.wav") #canal.play("C:\\coq.wav") #canal.play("C:\\etoile.mp3") print "Fini"