123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- # -*- coding: utf-8 -*-
- from __future__ import division
- from ..__load__ import *
- from .base import Base
- class Wav(Base):
- def __init__(self, **kwargs):
- super(self.__class__, self).__init__(**kwargs)
- self.playing = False
- self.stop = False
- def run(self):
- # play a voice
- CHUNK = 1024
- self.logger.info(self.mouth.log('播放语音 %s', self.src))
- f = wave.open(self.src, "rb")
- with no_alsa_error():
- audio = pyaudio.PyAudio()
- stream = audio.open(
- format=audio.get_format_from_width(f.getsampwidth()),
- channels=f.getnchannels(),
- rate=f.getframerate(),
- input=False,
- output=True)
-
- self.playing = True
- stream.start_stream()
- data = f.readframes(CHUNK)
- while data != '' and not self.stop:
- stream.write(data)
- data = f.readframes(CHUNK)
- print('data=="": {}, self.stop: {}'.format(data == '', self.stop))
- self.playing = False
- stream.stop_stream()
- stream.close()
- audio.terminate()
- if self.onCompleteds:
- for onCompleted in self.onCompleteds:
- if onCompleted:
- onCompleted()
- def play(self, src, onCompleted=None):
- self.src = src
- self.onCompleted = onCompleted
- self.start()
- def play_block(self):
- self.run()
- def stop(self):
- self.stop = True
- File.remove(self.src)
- def is_playing(self):
- return self.playing
|