Exemplo n.º 1
0
def test_16khz_file():
    audio_file = AudioFile(io.BytesIO(b'\x52\x49\x46\x46\x22\xe2\x01\x00\x57\x41\x56\x45\x66\x6d\x74\x20\x10\x00'
                                      b'\x00\x00\x01\x00\x01\x00\x80\x3e\x00\x00\x00\x7d\x00\x00\x02\x00\x10\x00'
                                      b'\x64\x61\x74\x61\xfe\xe1\x01\x00\x74\x65\x73\x74'))
    assert audio_file.header.encoding == PCM_SIGNED_INT
    assert audio_file.header.bits_per_sample == 16
    assert audio_file.header.sample_rate == 16000
    assert audio_file.header.channels == 1
    assert audio_file.header.big_endian is False
    assert audio_file.read(4) == b'test'
Exemplo n.º 2
0
def test_48khz_big_endian_file():
    audio_file = AudioFile(io.BytesIO(b'\x52\x49\x46\x58\x00\x0a\x00\x32\x57\x41\x56\x45\x66\x6d\x74\x20\x00\x00'
                                      b'\x00\x12\x00\x03\x00\x01\x00\x00\xbb\x80\x00\x02\xee\x00\x00\x04\x00\x20'
                                      b'\x00\x00\x66\x61\x63\x74\x00\x00\x00\x04\x00\x02\x62\x01\x64\x61\x74\x61'
                                      b'\x00\x09\x88\x04\x74\x65\x73\x74'))
    assert audio_file.header.encoding == PCM_FLOAT
    assert audio_file.header.bits_per_sample == 32
    assert audio_file.header.sample_rate == 48000
    assert audio_file.header.channels == 1
    assert audio_file.header.big_endian is True
    assert audio_file.read(4) == b'test'
Exemplo n.º 3
0
def test_16khz_file():
    audio_file = AudioFile(
        io.BytesIO(
            b'\x52\x49\x46\x46\x22\xe2\x01\x00\x57\x41\x56\x45\x66\x6d\x74\x20\x10\x00'
            b'\x00\x00\x01\x00\x01\x00\x80\x3e\x00\x00\x00\x7d\x00\x00\x02\x00\x10\x00'
            b'\x64\x61\x74\x61\xfe\xe1\x01\x00'))
    assert audio_file.header.bits_per_sample == 16
    assert audio_file.header.sample_rate == 16000
Exemplo n.º 4
0
def test_raw_audio():
    audio_file = AudioFile(
        io.BytesIO(
            b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
            b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
            b'\x00\x00\x00\x00\x00\x00\x00\x00'))
    assert audio_file.header.bits_per_sample == 16
    assert audio_file.header.sample_rate == 16000
Exemplo n.º 5
0
def test_48khz_file():
    audio_file = AudioFile(
        io.BytesIO(
            b'\x52\x49\x46\x46\x32\x00\x0a\x00\x57\x41\x56\x45\x66\x6d\x74\x20\x12\x00'
            b'\x00\x00\x03\x00\x01\x00\x80\xbb\x00\x00\x00\xee\x02\x00\x04\x00\x20\x00'
            b'\x00\x00\x66\x61\x63\x74\x04\x00'))
    assert audio_file.header.bits_per_sample == 32
    assert audio_file.header.sample_rate == 48000
Exemplo n.º 6
0
 def __init__(self, audio_file=None, **kwargs):
     Device.__init__(self, **kwargs)
     self.time_between_chunks = kwargs.get('time_between_chunks', 0.08)
     self._queue = queue.Queue()
     self._last_chunk_time = datetime.datetime.utcfromtimestamp(0)
     self.audio_file = AudioFile(audio_file)
     if self.audio_file.header is not None:
         self.encoding = self.audio_file.header.encoding
         self.sample_rate = self.audio_file.header.sample_rate
         self.bits_per_sample = self.audio_file.header.bits_per_sample
         self.channels = self.audio_file.header.channels
         self.big_endian = self.audio_file.header.big_endian
     else:
         self.encoding = None
         self.sample_rate = None
         self.bits_per_sample = None
         self.channels = None
         self.big_endian = None
Exemplo n.º 7
0
class FileDevice(Device):
    def __init__(self, audio_file=None, **kwargs):
        Device.__init__(self)
        self.time_between_chunks = kwargs.get('time_between_chunks', 0.08)
        self._queue = queue.Queue()
        self._last_chunk_time = datetime.datetime.utcfromtimestamp(0)
        self.audio_file = AudioFile(audio_file)

    def stream(self, client, recording_stopper):
        self.start_recording()
        recording_stopper.started()
        query = client.stream_audio(self.generate_frames(), notification_handler=recording_stopper.stop_recording,
                                    audio_type=self.audio_type())
        recording_stopper.stop_recording(None)
        return query

    def start_recording(self):
        self._queue.queue.clear()
        self.audio_to_frames()

    def stop_recording(self):
        self._queue.queue.clear()

    def is_recording(self):
        return not (self._queue.empty())

    def generate_frames(self):
        while not self._queue.empty():
            data = self._queue.get_nowait()
            if data:
                now = datetime.datetime.utcnow()
                seconds_since_last = (now - self._last_chunk_time).total_seconds()
                if seconds_since_last < self.time_between_chunks:
                    time.sleep(self.time_between_chunks - seconds_since_last)
                self._last_chunk_time = now
                yield data

    def audio_to_frames(self):
        while True:
            data = self.audio_file.read(self.chunk_size)
            if not data:
                break
            self._queue.put(data)

    def audio_type(self):
        return 'audio/pcm;bits={};rate={}'.format(self.audio_file.header.bits_per_sample,
                                                  self.audio_file.header.sample_rate)
Exemplo n.º 8
0
def test_raw_audio():
    audio_file = AudioFile(io.BytesIO(b'\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00\x00\x00'
                                      b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
                                      b'\x00\x00\x00\x00\x00\x00\x00\x00'))
    assert audio_file.header is None
    assert audio_file.read(16) == b'\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00'
Exemplo n.º 9
0
class FileDevice(Device):
    def __init__(self, audio_file=None, **kwargs):
        Device.__init__(self, **kwargs)
        self.time_between_chunks = kwargs.get('time_between_chunks', 0.08)
        self._queue = queue.Queue()
        self._last_chunk_time = datetime.datetime.utcfromtimestamp(0)
        self.wakeword_detected = False
        self.audio_file = AudioFile(audio_file)
        if self.audio_file.header is not None:
            self.encoding = self.audio_file.header.encoding
            self.sample_rate = self.audio_file.header.sample_rate
            self.bits_per_sample = self.audio_file.header.bits_per_sample
            self.channels = self.audio_file.header.channels
            self.big_endian = self.audio_file.header.big_endian
        else:
            self.encoding = None
            self.sample_rate = None
            self.bits_per_sample = None
            self.channels = None
            self.big_endian = None

    def stream(self, client, recording_stopper):
        self.start_recording()
        recording_stopper.started()
        query = client.stream_audio(
            self.generate_frames(),
            notification_handler=recording_stopper.stop_recording,
            audio_type=self.audio_type())
        recording_stopper.stop_recording(None)
        return query

    def stream_with_wakeword(self, client, recording_stopper,
                             wakeword_detector):
        self.start_recording()
        recording_stopper.started()
        self.wakeword_detected = wakeword_detector.stream_audio(
            self.generate_frames())
        if self.wakeword_detected:
            print("Wakeword detected.")
        else:
            print("No wakeword detected.")
        query = client.stream_audio(
            self.generate_frames(),
            notification_handler=recording_stopper.stop_recording,
            audio_type=self.audio_type())
        recording_stopper.stop_recording(None)
        return query

    def test_wakeword(self, recording_stopper, wakeword_detector):
        self.start_recording()
        recording_stopper.started()
        wakeword_indices = wakeword_detector.test_wakeword(
            self.generate_frames())
        recording_stopper.stop_recording(None)
        return wakeword_indices

    def start_recording(self):
        log.info(
            'Sending %s channels at %sHz, %s bits per sample using encoding %s',
            self.channels, self.sample_rate, self.bits_per_sample,
            self.encoding)
        self._queue.queue.clear()
        self.audio_to_frames()

    def stop_recording(self):
        self._queue.queue.clear()

    def is_recording(self):
        return not (self._queue.empty())

    def generate_frames(self):
        while not self._queue.empty():
            data = self._queue.get_nowait()
            if data:
                now = datetime.datetime.utcnow()
                seconds_since_last = (now -
                                      self._last_chunk_time).total_seconds()
                if seconds_since_last < self.time_between_chunks:
                    time.sleep(self.time_between_chunks - seconds_since_last)
                self._last_chunk_time = now
                yield data

    def audio_to_frames(self):
        while True:
            data = self.audio_file.read(self.chunk_size)
            if not data:
                break
            self._queue.put(data)

    @abc.abstractmethod
    def audio_type(self):
        pass
Exemplo n.º 10
0
 def __init__(self, audio_file=None, **kwargs):
     Device.__init__(self)
     self.time_between_chunks = kwargs.get('time_between_chunks', 0.08)
     self._queue = queue.Queue()
     self._last_chunk_time = datetime.datetime.utcfromtimestamp(0)
     self.audio_file = AudioFile(audio_file)