| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- # -*-coding:Latin-1 -*
- """lecture d'un flux audio mp3"""
- from time import sleep
- from threading import Thread
- #modules complémentaires
- import pymedia.audio.sound as sound
- import pymedia.audio.acodec as acodec
- class Lecture(Thread):
- """instance de lecture audio d'un flux mp3"""
- def __init__(self, identifiant, cparams, debug):
- """création du fil de lecture"""
- self.Terminated = False
- Thread.__init__(self)
- self.debug = debug
- self.dec = acodec.Decoder(cparams)
- self.sortie = sound.Output( cparams["sample_rate"], 1, sound.AFMT_S16_LE, 0 )
- if self.debug:
- self.recept = open( "{}.mp3".format(identifiant), 'wb' )
-
- def lire(self, recu):
- """reception des donnees a lire"""
- retour = ""
- if recu:
- son_decode = self.dec.decode(recu)
- if self.debug:
- self.recept.write(recu)
- if son_decode:
- self.sortie.play(son_decode.data)
-
- def run(self):
- """boucle vide"""
- while not self.Terminated:
- sleep(0.1)
- def stop(self):
- """Fermeture du fil de lecture"""
- print("Fin de la lecture\n")
- self.Terminated = True
- if self.debug:
- self.recept.close()
- if __name__ == "__main__":
- # si lancement direct:
- micro = sound.Input( 22050, 1, sound.AFMT_S16_LE, 0 )
- micro.start()
- cparams= { 'id': acodec.getCodecID( 'mp3' ),
- 'bitrate': 32000,
- 'sample_rate': 22050,
- 'channels': 1 }
- ac = acodec.Encoder( cparams )
- le = Lecture("test_lecture", cparams, False)
- le.start()
- i = 0
-
- while True:
- try:
- i += 1
- son = micro.getData()
- if son and len(son):
- son_encode = ac.encode(son)
- print("".format(i))
- for fr in son_encode:
- print(le.lire(fr))
- except KeyboardInterrupt:
- break
- micro.stop()
- le.stop()
-
|