Exemple #1
0
    def test_ulaw2lin(self):
        encoded = b"\x00\x0e\x28\x3f\x57\x6a\x76\x7c\x7e\x7f" b"\x80\x8e\xa8\xbf\xd7\xea\xf6\xfc\xfe\xff"
        src = [-8031, -4447, -1471, -495, -163, -53, -18, -6, -2, 0, 8031, 4447, 1471, 495, 163, 53, 18, 6, 2, 0]
        for w in 1, 2, 4:
            self.assertEqual(audioop.ulaw2lin(encoded, w), packs[w](*(x << (w * 8) >> 14 for x in src)))

        # Current u-law implementation has two codes fo 0: 0x7f and 0xff.
        encoded = bytes(range(127)) + bytes(range(128, 256))
        for w in 2, 4:
            decoded = audioop.ulaw2lin(encoded, w)
            self.assertEqual(audioop.lin2ulaw(decoded, w), encoded)
Exemple #2
0
 def decode(self, bytes):
     if len(bytes) != 160:
         log.msg("mulaw: short read on decode, %d != 160" % len(bytes),
                 system="codec")
     if 0:
         bytes = audioop.ulaw2lin(bytes, 2)
         self.buf += bytes
         if len(self.buf) > 159:
             out, self.buf = self.buf[:160], self.buf[160:]
             return out
     else:
         return audioop.ulaw2lin(bytes, 2)
Exemple #3
0
 def decode(self, bytes):
     if len(bytes) != 160:
         log.msg("mulaw: short read on decode, %d != 160"%len(bytes),
                                                         system="codec")
     if 0:
         bytes = audioop.ulaw2lin(bytes, 2)
         self.buf += bytes
         if len(self.buf) > 159:
             out, self.buf = self.buf[:160], self.buf[160:]
             return out
     else:
         return audioop.ulaw2lin(bytes, 2)
Exemple #4
0
    def test_ulaw2lin(self):
        encoded = b'\x00\x0e\x28\x3f\x57\x6a\x76\x7c\x7e\x7f'\
                  b'\x80\x8e\xa8\xbf\xd7\xea\xf6\xfc\xfe\xff'
        src = [-8031, -4447, -1471, -495, -163, -53, -18, -6, -2, 0,
               8031, 4447, 1471, 495, 163, 53, 18, 6, 2, 0]
        for w in 1, 2, 4:
            self.assertEqual(audioop.ulaw2lin(encoded, w),
                             packs[w](*(x << (w * 8) >> 14 for x in src)))

        # Current u-law implementation has two codes fo 0: 0x7f and 0xff.
        encoded = ''.join(chr(x) for x in range(127) + range(128, 256))
        for w in 2, 4:
            decoded = audioop.ulaw2lin(encoded, w)
            self.assertEqual(audioop.lin2ulaw(decoded, w), encoded)
Exemple #5
0
 def test_ulaw2lin(self):
     # Cursory
     d = audioop.lin2ulaw(data[0], 1)
     self.assertEqual(audioop.ulaw2lin(d, 1), data[0])
     if endian == 'big':
         self.assertEqual(audioop.ulaw2lin(d, 2),
                          b'\x00\x00\x01\x04\x02\x0c')
         self.assertEqual(audioop.ulaw2lin(d, 4),
                          b'\x00\x00\x00\x00\x01\x04\x00\x00\x02\x0c\x00\x00')
     else:
         self.assertEqual(audioop.ulaw2lin(d, 2),
                          b'\x00\x00\x04\x01\x0c\x02')
         self.assertEqual(audioop.ulaw2lin(d, 4),
                          b'\x00\x00\x00\x00\x00\x00\x04\x01\x00\x00\x0c\x02')
Exemple #6
0
        def bytes_to_channels(fmtChunk: FmtChunk, raw_samples: bytes,
                              size) -> list:
            if int(fmtChunk.data.bits_per_sample / 8) > 0:
                sample_len = int(fmtChunk.data.bits_per_sample / 8)
            else:
                sample_len = 0

            samples = []

            if sample_len > 0:
                for i in range(int(size / sample_len)):
                    sample = raw_samples[i * sample_len:i * sample_len +
                                         sample_len]
                    if fmtChunk.data.audio_format == 1:
                        if sample_len == 1:
                            converted_sample = int.from_bytes(
                                sample, byteorder="little", signed=False)
                        else:
                            converted_sample = int.from_bytes(
                                sample, byteorder="little", signed=True)
                    elif fmtChunk.data.audio_format == 3:
                        if sample_len == 4:
                            converted_sample = struct.unpack("f", sample)[0]
                        else:
                            converted_sample = struct.unpack("d", sample)[0]
                    elif fmtChunk.data.audio_format == 6:
                        converted_sample = int.from_bytes(audioop.alaw2lin(
                            sample, sample_len),
                                                          byteorder="little",
                                                          signed=True)
                    elif fmtChunk.data.audio_format == 7:
                        converted_sample = int.from_bytes(audioop.ulaw2lin(
                            sample, sample_len),
                                                          byteorder="little",
                                                          signed=True)
                    else:
                        print(
                            "Format zapisu danych w pliku nie jest wspierany")
                        raise Exception
                    samples.append(converted_sample)
            else:
                if fmtChunk.data.audio_format == 2:
                    ret = audioop.adpcm2lin(raw_samples,
                                            fmtChunk.data.bits_per_sample,
                                            None)
                    samples_lin = ret[0]
                    for i in range(int(len(samples_lin) / 8)):
                        sample = samples_lin[i * 8:i * 8 + 8]
                        converted_sample = int.from_bytes(sample,
                                                          byteorder="little",
                                                          signed=True)
                        samples.append(converted_sample)
                else:
                    print("Format zapisu danych w pliku nie jest wspierany")
                    raise Exception

            channels = []
            for c in range(fmtChunk.data.num_channels):
                channels.append(samples[c::fmtChunk.data.num_channels])
            return channels
Exemple #7
0
 def _inout(self, linear, stream_time, userdata):
     #        logger.debug('audio capture %d', len(linear))
     self._ts += 160
     if self.media and (self.media.hasYourFormat(self._pcmu) or self.media.hasYourFormat(self._pcma)):
         linear8, self._resample1 = audiospeex.resample(
             linear, input_rate=44100, output_rate=8000, state=self._resample1
         )
         if self.media.hasYourFormat(self._pcmu):
             fmt, payload = self._pcmu, audioop.lin2ulaw(linear8, 2)
         elif self.media.hasYourFormat(self._pcma):
             fmt, payload = self._pcma, audioop.lin2alaw(linear8, 2)
         self.media.send(payload=payload, ts=self._ts, marker=False, fmt=fmt)
     if self._queue:
         fmt, packet = self._queue.pop(0)
         linear8 = None
         if str(fmt.name).lower() == "pcmu" and fmt.rate == 8000 or fmt.pt == 0:
             linear8 = audioop.ulaw2lin(packet.payload, 2)
         elif str(fmt.name).lower() == "pcma" and fmt.rate == 8000 or fmt.pt == 8:
             linear8 = audioop.alaw2lin(packet.payload, 2)
         if linear8:
             linear, self._resample2 = audiospeex.resample(
                 linear8, input_rate=8000, output_rate=44100, state=self._resample2
             )
             #                logger.debug('audio play %d', len(linear))
             return linear
     return ""
Exemple #8
0
  def write(self, audio):

    if self._user_resample:
      # The user can also specify to have ULAW encoded source audio
      # converted to linear encoding upon being written.
      if self._user_resample.ulaw2lin:
        # Possibly skip downsampling if this was triggered, as
        # while ULAW encoded audio can be sampled at rates other
        # than 8KHz, since this is telephony related, it's unlikely.
        audio = audioop.ulaw2lin(audio, 2)

      # If the audio isn't already sampled at 8KHz,
      # then it needs to be downsampled first
      if self._user_resample.rate != 8000:
        audio, self._user_resample.ratecv_state = audioop.ratecv(
          audio,
          2,
          self._user_resample.channels,
          self._user_resample.rate,
          8000,
          self._user_resample.ratecv_state,
        )

      # If the audio isn't already in mono, then
      # it needs to be downmixed as well
      if self._user_resample.channels == 2:
        audio = audioop.tomono(audio, 2, 1, 1)

    self._tx_q.put(audio)
Exemple #9
0
    def compress(self):
        read = wave.open(self.create_wave(), 'rb')
        string_wav = np.fromstring(read.readframes(-1), 'Int16')

        if (self.compression_mod == "a-LAW"):
            compressed_string = audioop.lin2alaw(string_wav,
                                                 read.getsampwidth())
            compressed_string = audioop.alaw2lin(compressed_string,
                                                 read.getsampwidth())
            self.working_signal = np.frombuffer(compressed_string,
                                                dtype='Int16')
        if (self.compression_mod == "u-LAW"):
            compressed_string = audioop.lin2ulaw(string_wav,
                                                 read.getsampwidth())
            compressed_string = audioop.ulaw2lin(compressed_string,
                                                 read.getsampwidth())
            self.working_signal = np.frombuffer(compressed_string,
                                                dtype='Int16')
        if (self.compression_mod == "ADPCM"):
            compressed_string = audioop.lin2adpcm(string_wav,
                                                  read.getsampwidth(), None)
            compressed_string = audioop.adpcm2lin(compressed_string[0],
                                                  read.getsampwidth(), None)
            self.working_signal = np.frombuffer(compressed_string[0],
                                                dtype='Int16')
Exemple #10
0
 def decode(self, encoded_frame):
     return [
         AudioFrame(channels=1,
                    data=audioop.ulaw2lin(encoded_frame.data, 2),
                    sample_rate=8000,
                    timestamp=encoded_frame.timestamp)
     ]
Exemple #11
0
 def _inout(self, linear, stream_time, userdata):
     # logger.debug('audio capture %d', len(linear))
     self._ts += 160
     if self.media and (self.media.hasYourFormat(self._pcmu) or self.media.hasYourFormat(self._pcma)):
         if self._outqueue:
             # logger.debug('sending packet from out queue, not mic')
             linear8 = self._outqueue.pop(0)
         else:
             linear8, self._resample1 = audiospeex.resample(linear, input_rate=self.sample_rate, output_rate=8000, state=self._resample1)
         if self.media.hasYourFormat(self._pcmu):
             fmt, payload = self._pcmu, audioop.lin2ulaw(linear8, 2)
         elif self.media.hasYourFormat(self._pcma):
             fmt, payload = self._pcma, audioop.lin2alaw(linear8, 2)
         self.media.send(payload=payload, ts=self._ts, marker=False, fmt=fmt)
     if self._queue:
         fmt, packet = self._queue.pop(0)
         linear8 = None
         if str(fmt.name).lower() == 'pcmu' and fmt.rate == 8000 or fmt.pt == 0:
             linear8 = audioop.ulaw2lin(packet.payload, 2)
         elif str(fmt.name).lower() == 'pcma' and fmt.rate == 8000 or fmt.pt == 8:
             linear8 = audioop.alaw2lin(packet.payload, 2)
         if linear8:
             self._record.write(linear8)
             if self._thqueue is not None:
                 self._thqueue.put(linear8)
             linear, self._resample2 = audiospeex.resample(linear8, input_rate=8000, output_rate=self.sample_rate, state=self._resample2)
             # logger.debug('audio play %d', len(linear))
             return linear
     return ''
Exemple #12
0
 def test_ulaw2lin(self):
     encoded = (
         b'\x00\x0e(?Wjv|~\x7f\x80\x8e\xa8\xbf\xd7\xea\xf6\xfc\xfe\xff')
     src = [
         -8031, -4447, -1471, -495, -163, -53, -18, -6, -2, 0, 8031, 4447,
         1471, 495, 163, 53, 18, 6, 2, 0
     ]
     for w in (1, 2, 3, 4):
         decoded = packs[w](*(x << w * 8 >> 14 for x in src))
         self.assertEqual(audioop.ulaw2lin(encoded, w), decoded)
         self.assertEqual(audioop.ulaw2lin(bytearray(encoded), w), decoded)
         self.assertEqual(audioop.ulaw2lin(memoryview(encoded), w), decoded)
     encoded = bytes(range(127)) + bytes(range(128, 256))
     for w in (2, 3, 4):
         decoded = audioop.ulaw2lin(encoded, w)
         self.assertEqual(audioop.lin2ulaw(decoded, w), encoded)
Exemple #13
0
 def _inout(self, linear, stream_time, userdata):
     # logger.debug('audio capture %d', len(linear))
     self._ts += 160
     if self.media and (self.media.hasYourFormat(self._pcmu) or self.media.hasYourFormat(self._pcma)):
         if self._outqueue:
             # logger.debug('sending packet from out queue, not mic')
             linear8 = self._outqueue.pop(0)
         else:
             linear8, self._resample1 = audiospeex.resample(linear, input_rate=self.sample_rate, output_rate=8000, state=self._resample1)
         if self.media.hasYourFormat(self._pcmu):
             fmt, payload = self._pcmu, audioop.lin2ulaw(linear8, 2)
         elif self.media.hasYourFormat(self._pcma):
             fmt, payload = self._pcma, audioop.lin2alaw(linear8, 2)
         self.media.send(payload=payload, ts=self._ts, marker=False, fmt=fmt)
     if self._queue:
         fmt, packet = self._queue.pop(0)
         linear8 = None
         if str(fmt.name).lower() == 'pcmu' and fmt.rate == 8000 or fmt.pt == 0:
             linear8 = audioop.ulaw2lin(packet.payload, 2)
         elif str(fmt.name).lower() == 'pcma' and fmt.rate == 8000 or fmt.pt == 8:
             linear8 = audioop.alaw2lin(packet.payload, 2)
         if linear8:
             self._record.write(linear8)
             if self._thqueue is not None:
                 self._thqueue.put(linear8)
             linear, self._resample2 = audiospeex.resample(linear8, input_rate=8000, output_rate=self.sample_rate, state=self._resample2)
             # logger.debug('audio play %d', len(linear))
             return linear
     return ''
Exemple #14
0
        def recv_msg_symm(sock):

            while True:
                data, addr = sock.recvfrom(1024)
                if addr == self.master:
                    # sys.stdout.write(data)
                    data = audioop.ulaw2lin(data, 2)
                    stream.write(data)
Exemple #15
0
def testulaw2lin(data):
    if verbose:
        print 'ulaw2lin'
    # Cursory
    d = audioop.lin2ulaw(data[0], 1)
    if audioop.ulaw2lin(d, 1) != data[0]:
        return 0
    return 1
Exemple #16
0
 def decode(self, bytes):
     if not bytes:
         return
     elif len(bytes) != 160:
         log.msg("mulaw: short read on decode, %d != 160"%len(bytes),
                                                         system="codec")
         # Pad with silence.
         extra = (160 - len(bytes)) * bytes[-1]
         bytes += extra
     if 0:
         bytes = audioop.ulaw2lin(bytes, 2)
         self.buf += bytes
         if len(self.buf) > 159:
             out, self.buf = self.buf[:160], self.buf[160:]
             return out
     else:
         return audioop.ulaw2lin(bytes, 2)
Exemple #17
0
 def decode(self, bytes):
     if not bytes:
         return
     elif len(bytes) != 160:
         log.msg("mulaw: short read on decode, %d != 160" % len(bytes),
                 system="codec")
         # Pad with silence.
         extra = (160 - len(bytes)) * bytes[-1]
         bytes += extra
     if 0:
         bytes = audioop.ulaw2lin(bytes, 2)
         self.buf += bytes
         if len(self.buf) > 159:
             out, self.buf = self.buf[:160], self.buf[160:]
             return out
     else:
         return audioop.ulaw2lin(bytes, 2)
def testulaw2lin(data):
    if verbose:
        print 'ulaw2lin'
    # Cursory
    d = audioop.lin2ulaw(data[0], 1)
    if audioop.ulaw2lin(d, 1) <> data[0]:
        return 0
    return 1
Exemple #19
0
 def decode(self, encoded_frame):
     frame = AudioFrame(
         channels=1,
         data=audioop.ulaw2lin(encoded_frame.data, 2),
         sample_rate=SAMPLE_RATE)
     frame.pts = encoded_frame.timestamp
     frame.time_base = TIME_BASE
     return [frame]
Exemple #20
0
def messageHandler(conn):
    # this is to deal with a single incoming TCP or UDP message
    global fifo, previous, missed, state

    # the actual packet length will not be 2048 bytes, but depends on the format and number of audio samples
    buf = sock.recv(2048)
    if len(buf) < 12:
        return

    # see https://en.wikipedia.org/wiki/RTP_payload_formats
    (version, type, counter, timestamp, id) = struct.unpack('BBHII', buf[0:12])

    if version != 128:
        raise RuntimeError('unsupported packet version')

    fragment = bytearray(buf[12:])

    with fifolock:
        if not id in fifo:
            fifo[id] = RingBuffer(
                id, rate)  # make a buffer that can hold one second of audio
            previous[id] = None
            state[id] = None
            missed[id] = 0

        if type == 0:
            # type=0  PCMU  audio 1 8000  any 20  ITU-T G.711 PCM μ-Law audio 64 kbit/s               RFC 3551
            fragment = audioop.ulaw2lin(fragment, 2)
            fragment, state[id] = audioop.ratecv(fragment, 2, 1, 8000, 44100,
                                                 state[id])
            dat = np.frombuffer(fragment, np.int16)
        elif type == 1:
            # type=8  PCMA  audio 1 8000  any 20  ITU-T G.711 PCM A-Law audio 64 kbit/s               RFC 3551
            fragment = audioop.alaw2lin(fragment, 2)
            fragment, state[id] = audioop.ratecv(fragment, 2, 1, 8000, 44100,
                                                 state[id])
            dat = np.frombuffer(fragment, np.int16)
        elif type == 11:
            # type=11 L16   audio 1 44100 any 20  Linear PCM 16-bit audio 705.6 kbit/s, uncompressed  RFC 3551, Page 27
            dat = np.frombuffer(fragment, np.int16)
        else:
            raise RuntimeError('unsupported RTP packet type')

        if not previous[id] == None:
            for missing in range(previous[id] + 1 - counter, 0):
                logger.debug('missed packet from %d' % (id))
                # See https://en.wikipedia.org/wiki/Comfort_noise
                missing_dat = np.random.random(
                    len(dat))  # FIXME these are only positive
                missing_dat *= np.linalg.norm(dat) / np.linalg.norm(
                    missing_dat)
                missing_timestamp = timestamp + missing * len(
                    dat) * 1000 / 44100
                missed[id] += 1
                fifo[id].push(missing_dat.astype(np.int16), missing_timestamp)

        previous[id] = counter
        fifo[id].push(dat, timestamp)
Exemple #21
0
    def stream_to_lex(self, base_64_encoded_data):
        if self.stop_data_processing.is_set():
            self.logger.warn(
                "discarding the passed in data, as underlying lex stream has been stopped"
            )
            return

        data = self.__decode_data(base_64_encoded_data)

        raw_audio_data = audioop.ulaw2lin(data, self.width)
        #raw_audio_data, state = audioop.lin2adpcm(raw_audio_data, self.width, None)

        # raw_audio_data, state = audioop.ratecv(raw_audio_data,
        #                                        self.width,
        #                                        self.channels,
        #                                        self.twilio_rate,
        #                                        self.lex_rate,
        #                                        None)

        rms = audioop.rms(raw_audio_data, self.width)

        #self.logger.info("RMS value is {0}".format(rms))
        self.rms_values.append(rms)
        if rms > self.voice_threshold:
            #self.logger.debug("voice detected in input data")
            self.rms_graph.append("^")

            if self.last_detected_voice_time is None:
                # voice detected for first time
                self.logger.debug("voice detected for first time")
                self.voice_detected()
            self.last_detected_voice_time = datetime.now()
            self.lex_client.add_to_stream(raw_audio_data)
        else:
            self.rms_graph.append(".")
            #self.logger.debug("silence detected in input data")

            if self.last_detected_voice_time:
                # check if elapsed time is greater than configured time for silence
                self.lex_client.add_to_stream(raw_audio_data)

                silence_time = (datetime.now() -
                                self.last_detected_voice_time).total_seconds()
                if silence_time >= self.silence_duration_time:
                    self.logger.debug(
                        "elapsed time {0} seconds since last detected voice time {1} is higher than configured time for silence {2} seconds. closing connection to lex."
                        .format(silence_time, self.last_detected_voice_time,
                                self.silence_duration_time))

                    # stop lex client now
                    self.lex_client.stop()
                    self.stop_data_processing.set()
                    self.logger.info("Voice activity graph {0}".format("".join(
                        self.rms_graph)))
                    self.logger.info("RMS values {0}".format(self.rms_values))
                    self.silence_detected()
 def readframes(self, nframes):
     if self._encoding in _simple_encodings:
         if nframes == AUDIO_UNKNOWN_SIZE:
             data = self._file.read()
         else:
             data = self._file.read(nframes*self._framesize)
         if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
             import audioop
             data = audioop.ulaw2lin(data, self._sampwidth)
         return data
Exemple #23
0
 def decode(self, payload):
     assert isinstance(payload,
                       bytes), "payload is not an instance of bytes"
     if not payload:
         return
     elif len(payload) != 160:
         log.msg("mulaw: short read on decode, %d != 160" % len(payload),
                 system="codec")
         # Pad with silence.
         extra = (160 - len(payload)) * payload[-1]
         payload += extra
     if 0:
         payload = audioop.ulaw2lin(payload, 2)
         self.buf += payload
         if len(self.buf) > 159:
             out, self.buf = self.buf[:160], self.buf[160:]
             return out
     else:
         return audioop.ulaw2lin(payload, 2)
Exemple #24
0
def sendPacket(packetsize):
    previousend = 0;
    singleByte = b'\x00'
    replacement = singleByte
    newEnd = 0
    for i in range (1, packetsize):
        replacement+=singleByte
    for i in range (0, nframes, packetsize):
        randnum = random.random()
        for i in range(0,6):
            if (randnum<lossratio[i]):
                J[i].writeframes(audioop.ulaw2lin(encodedaudio[previousend-packetsize:newEnd],1))
                K[i].writeframes(replacement)
                previousend = previousend+packetsize
            else:
                newEnd = previousend+packetsize
                J[i].writeframes(audioop.ulaw2lin(encodedaudio[previousend:newEnd],1))
                K[i].writeframes(audioop.ulaw2lin(encodedaudio[previousend:newEnd],1))
                previousend = previousend+packetsize
Exemple #25
0
 def readframes(self, nframes):
     if self._encoding in sunau._simple_encodings:
         if nframes == sunau.AUDIO_UNKNOWN_SIZE:
             data = self._file.read()
         else:
             data = self._file.read(nframes * self._framesize * self._nchannels)
         if self._encoding == sunau.AUDIO_FILE_ENCODING_MULAW_8:
             import audioop
             data = audioop.ulaw2lin(data, self._sampwidth)
         return data
     return None             # XXX--not implemented yet
    def speech_to_text(self):
        stt_config = CONFIG['stt_config']
        models_folder = Path(stt_config['folder'])
        model_path = str(models_folder / stt_config['model'])
        scorer_path = str(models_folder / stt_config['scorer'])

        self.deep_speech = Model(model_path)
        self.deep_speech.enableExternalScorer(scorer_path)

        stream = self.deep_speech.createStream()

        while True:
            speech = self.audio_in_queue.get()

            while not self.audio_in_queue.empty():
                speech += self.audio_in_queue.get()

            lin_speech = audioop.ulaw2lin(speech, 2)
            ds_speech, _ = audioop.ratecv(lin_speech, 2, 1, self.IN_AUDIO_RATE,
                                          self.DS_AUDIO_RATE, None)

            lin_speech_arr = np.frombuffer(lin_speech, np.int16)
            ds_speech_arr = np.frombuffer(ds_speech, np.int16)

            stream.feedAudioContent(ds_speech_arr)

            self.caller_audio_chunk = np.concatenate(
                (self.caller_audio_chunk, lin_speech_arr))

            chunk_idx = max(0,
                            len(self.caller_audio_chunk) - self.QUIET_LENGTH)
            quiet_chunk = self.caller_audio_chunk[chunk_idx:]
            if (quiet_chunk < self.QUIET_THRESH).all() and (
                    self.caller_audio_chunk > self.QUIET_THRESH).any():
                text = stream.intermediateDecode()

                if text.strip():
                    self.stt_to_chatbot_queue.put(text)

                    idx = len(self.transcript
                              )  # insert to avoid race conditions with indexes
                    self.transcript.insert(
                        idx, {
                            "speaker": "caller",
                            "text": text,
                            "datetime": dt.datetime.now().isoformat()
                        })
                    self.stt_to_classification_queue.put(idx)

                    stream.finishStream()
                    stream = self.deep_speech.createStream()

                self.caller_audio_chunk = np.array([], dtype='int16')
def read_sound_file(path):
    with open(path, 'rb') as fp:
        au = sunau.open(fp)
        rate = au.getframerate()
        nchannels = au.getnchannels()
        encoding = au._encoding
        fp.seek(0)
        data = fp.read()
    if encoding != sunau.AUDIO_FILE_ENCODING_MULAW_8:
        raise RuntimeError('Expect .au file with 8-bit mu-law samples')
    data = audioop.ulaw2lin(data, 2)
    return data, rate, 16, nchannels
Exemple #28
0
 def readframes(self, nframes):
     if self._encoding in _simple_encodings:
         if nframes == AUDIO_UNKNOWN_SIZE:
             data = self._file.read()
         else:
             data = self._file.read(nframes * self._framesize)
         self._soundpos += len(data) // self._framesize
         if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
             import audioop
             data = audioop.ulaw2lin(data, self._sampwidth)
         return data
     return None             # XXX--not implemented yet
Exemple #29
0
 def RTP_Rx_Thread(self, threadName):
     #print(threadName, "RTP_Rx_Thread started")
     while True:
         data, addr = self.RTP_Sock.recvfrom(1024)
         #print(threadName, "RTP_Rx_Thread: received message:", data)
         if data[0:2] == bytes.fromhex('9000'):
             buffer = audioop.ulaw2lin(data[28:], 2)
             if RepeaterVolume != 1:
                 buffer = audioop.mul(buffer, 2, RepeaterVolume)
             buffer, newstate = audioop.ratecv(buffer, 2, 1,
                                               self.PCMSAMPLERATE, 48000,
                                               None)
             mumble.sound_output.add_sound(buffer)
def read_sound_file(path):
    fp = open(path, 'rb')
    size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
    data = fp.read()
    fp.close()

    if enc != SND_FORMAT_MULAW_8:
        print "Expect .au file with 8-bit mu-law samples"
        return

    # Convert the data to 16-bit signed.
    data = audioop.ulaw2lin(data, 2)
    return (data, rate, 16, nchannels)
Exemple #31
0
def read_sound_file(path):
    fp = open(path, 'rb')
    size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
    data = fp.read()
    fp.close()

    if enc != SND_FORMAT_MULAW_8:
        print "Expect .au file with 8-bit mu-law samples"
        return

    # Convert the data to 16-bit signed.
    data = audioop.ulaw2lin(data, 2)
    return (data, rate, 16, nchannels)
Exemple #32
0
def save_output(data, filename):
    # data is the u-law quantized sample
    u_law = data
    u_law = [chr(x) for x in u_law]
    u_law = ''.join(u_law)

    original = audioop.ulaw2lin(u_law, input_sample_width)
    print "output data size: " + str(len(original))

    output = wave.open(filename, 'w')
    output.setparams((input_num_channels, input_sample_width, SAMPLE_RATE, 0,
                      'NONE', 'not compressed'))
    output.writeframes(original)
    output.close()
Exemple #33
0
 def readframes(self, nframes):
     if self._encoding in _simple_encodings:
         if nframes == AUDIO_UNKNOWN_SIZE:
             data = self._file.read()
         else:
             data = self._file.read(nframes * self._framesize)
         self._soundpos += len(data) // self._framesize
         if self._encoding == AUDIO_FILE_ENCODING_MULAW_8:
             with warnings.catch_warnings():
                 warnings.simplefilter('ignore', category=DeprecationWarning)
                 import audioop
             data = audioop.ulaw2lin(data, self._sampwidth)
         return data
     return None             # XXX--not implemented yet
def read_sound_file(path):
    with open(path, 'rb') as fp:
        au = sunau.open(fp)
        rate = au.getframerate()
        nchannels = au.getnchannels()
        encoding = au._encoding
        fp.seek(0)
        data = fp.read()

    if encoding != sunau.AUDIO_FILE_ENCODING_MULAW_8:
        raise RuntimeError("Expect .au file with 8-bit mu-law samples")

    # Convert the data to 16-bit signed.
    data = audioop.ulaw2lin(data, 2)
    return (data, rate, 16, nchannels)
def load_sph(file_name):
    """
    Load NIST SPH audio file
        file_name: sph full path
    """
    fsph = sphfile.SPHFile(file_name)  # u-law encoding and uint8 sample width
    assert fsph.format['sample_coding'] == 'ulaw'

    # transform to pcm16 and [-1, 1]
    buf = audioop.ulaw2lin(fsph.content, 2)
    y = np.frombuffer(buf, np.int16) / float(np.iinfo(np.int16).max)
    ych = []
    for ch in range(fsph.format['channel_count']):
        ych.append(y[ch::2])
    ych = np.vstack(ych).T
    return ych, fsph.format['sample_rate']
Exemple #36
0
	def run(self):
		while 1:
			olddata = data = self.iport.readsamps(600)
			if self.do_ulaw:
				data = audioop.lin2ulaw(data, 2)
				data = audioop.ulaw2lin(data, 2)
			if self.do_adpcm:
				data, nacstate = audioop.lin2adpcm(data, 2, \
					  self.acstate)
				data, dummy = audioop.adpcm2lin(data, 2, \
					  self.acstate)
				self.acstate = nacstate
			if self.do_diff:
				olddata = audioop.mul(olddata, 2, -1)
				data = audioop.add(olddata, data, 2)
			self.oport.writesamps(data)
			fl.check_forms()
Exemple #37
0
	def run(self):
		while 1:
			olddata = data = self.iport.readsamps(600)
			if self.do_ulaw:
				data = audioop.lin2ulaw(data, 2)
				data = audioop.ulaw2lin(data, 2)
			if self.do_adpcm:
				data, nacstate = audioop.lin2adpcm(data, 2, \
					  self.acstate)
				data, dummy = audioop.adpcm2lin(data, 2, \
					  self.acstate)
				self.acstate = nacstate
			if self.do_diff:
				olddata = audioop.mul(olddata, 2, -1)
				data = audioop.add(olddata, data, 2)
			self.oport.writesamps(data)
			fl.check_forms()
 def on_data_received(self, data):
     self._global_data += audioop.ulaw2lin(RTP(data).load,
                                           2)  # data.decode()
     audio_regions = auditok.split(
         self._global_data,
         audio_format='bytes',
         sampling_rate=8000,
         sample_width=2,
         channels=1,
         min_dur=0.3,  # minimum duration of a valid audio event in seconds
         max_dur=6,  # maximum duration of an event
         max_silence=
         0.3,  # maximum duration of tolerated continuous silence within an event
         energy_threshold=50  # threshold of detection
     )
     if len(list(audio_regions)) > 1:
         self.queue_audio.put(self._global_data)
         self._global_data = b''
def avs_play_audio(queue_data, stream):

    # Get data from queue
    try:
        data = queue_data.get(block=True, timeout=1)
        if len(data) == 0:
            return False
    except:
        return False
    #print("queue_data.get {}".format(len(data)))

    # Decompress and play data
    if CONF_AUDIO_RECV_BITDEPTH == DEVICE_CAPABILITIES_BITDEPTH_8:
        data = audioop.ulaw2lin(data, 2)
    stream.write(data)

    queue_data.task_done()
    return True
Exemple #40
0
    def analyze(self, bytes_data):
        ## Отправка в декодер
        # Декомпрессия из формата u-LAW (PCMU)
        data = audioop.ulaw2lin(bytes_data, 2)
        samples = np.frombuffer(data, dtype=np.int16)
        for s in samples:
            self.detector.goertzel(s)
        # Результатом алгоритма декодирования является characters -- список
        # пар значений (нажатая клавиша, время нажатия в секундах от запуска скрипта)
        if (self.detector.characters):
            self.transformInput(self.detector.characters)
            self.last_sound_time = int(round(time() * 1000))

        # В случае, когда звука нет больше 100 мс, камера останавливается
        elapsed = int(round(time() * 1000)) - self.last_sound_time
        if elapsed > 100:
            self.camera.stop()
        self.detector.characters = []
Exemple #41
0
 def RTP_Rx_Thread(self, threadName):
     #print(threadName, "RTP_Rx_Thread started")
     self.flushWave()
     LastWaveSegmentChangeTime = -MAX_SEC_PER_WAVEFILE
     while True:
         data, addr = self.RTP_Sock.recvfrom(1024)
         #print(threadName, "RTP_Rx_Thread: received message:", data)
         if time.time() - LastWaveSegmentChangeTime >= MAX_SEC_PER_WAVEFILE:
             self.flushWave()
             self.WaveSegmentName = WAVE_PATH + 'rec-' + threadName + '-' + time.strftime(
                 '%Y%m%d-%H%M') + '.wav'
             print(
                 threadName, ':', 'Beginning new segment \"' +
                 self.WaveSegmentName + '\"...')
             self.wavefile = wave.open(self.WaveSegmentName + '.tmp', 'wb')
             self.wavefile.setparams(
                 (1, 2, self.PCMSAMPLERATE, 0, 'NONE', 'not compressed'))
             LastWaveSegmentChangeTime = time.time()
         if data[0:2] == bytes.fromhex('9000'):
             self.wavefile.writeframes(audioop.ulaw2lin(data[28:], 2))
             self.WaveDataWritten = True
    def test_play_sound_file(self):
        path = findfile("audiotest.au")
        fp = open(path, 'r')
        size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
        data = fp.read()
        fp.close()

        if enc != SND_FORMAT_MULAW_8:
            self.fail("Expect .au file with 8-bit mu-law samples")

        # convert the data to 16-bit signed
        data = audioop.ulaw2lin(data, 2)

        # set the data format
        if sys.byteorder == 'little':
            fmt = linuxaudiodev.AFMT_S16_LE
        else:
            fmt = linuxaudiodev.AFMT_S16_BE

        # set parameters based on .au file headers
        self.dev.setparameters(rate, 16, nchannels, fmt)
        self.dev.write(data)
        self.dev.flush()
Exemple #43
0
"""Stuff to parse AIFF-C and AIFF files.
Exemple #44
0
	def _ulaw2lin(self, data):
		import audioop
		return audioop.ulaw2lin(data, 2)
Exemple #45
0
"""Stuff to parse Sun and NeXT audio files.
from test_support import verbose, findfile, TestFailed, TestSkipped
import errno
import fcntl
import linuxaudiodev
import os
import sys
import select
import sunaudio
import time
import audioop
SND_FORMAT_MULAW_8 = 1
def play_sound_file(path):
    fp = open(path, 'r')
    size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
    data = fp.read()
    fp.close()
    if enc != SND_FORMAT_MULAW_8:
        print "Expect .au file with 8-bit mu-law samples"
        return
    try:
        a = linuxaudiodev.open('w')
    except linuxaudiodev.error, msg:
        if msg[0] in (errno.EACCES, errno.ENODEV):
            raise TestSkipped, msg
        raise TestFailed, msg
    # convert the data to 16-bit signed
    data = audioop.ulaw2lin(data, 2)
    # set the data format
    if sys.byteorder == 'little':
        fmt = linuxaudiodev.AFMT_S16_LE
Exemple #47
0
 def test_ulaw2lin(self):
     # Cursory
     d = audioop.lin2ulaw(data[0], 1)
     self.assertEqual(audioop.ulaw2lin(d, 1), data[0])
Exemple #48
0
# Compare different audio compression schemes.
    data = fp.read()
    fp.close()

    if enc != SND_FORMAT_MULAW_8:
        print "Expect .au file with 8-bit mu-law samples"
        return

    try:
        a = linuxaudiodev.open('w')
    except linuxaudiodev.error, msg:
        if msg[0] in (errno.EACCES, errno.ENODEV, errno.EBUSY):
            raise TestSkipped, msg
        raise TestFailed, msg

    # convert the data to 16-bit signed
    data = audioop.ulaw2lin(data, 2)

    # set the data format
    if sys.byteorder == 'little':
        fmt = linuxaudiodev.AFMT_S16_LE
    else:
        fmt = linuxaudiodev.AFMT_S16_BE

    # at least check that these methods can be invoked
    a.bufsize()
    a.obufcount()
    a.obuffree()
    a.getptr()
    a.fileno()

    # set parameters based on .au file headers
def testulaw2lin(data):
	# Cursory
	d = audioop.lin2ulaw(data[0], 1)
	if audioop.ulaw2lin(d, 1) <> data[0]:
		return 0
	return 1
Exemple #51
0
QSIZE = 100000
Exemple #52
0
K.append(sunau.open("soundfiles/whatYouFeel1g.au","w"))


k= sunau.open("soundfiles/whatYouFeel1.au","w")
currentIndex = 0;
replacement = b'\x00'
packetsize = 256
prev=0
lossratio = [.1,.2,.35,.5,.7,100]
zeroes = False;

nframes = math.floor(w.getnframes())
audiostring = w.readframes(nframes)
encodedaudio = audioop.lin2ulaw(audiostring, 1)

recodedaudio = audioop.ulaw2lin(encodedaudio, 1)

for item in J:
    item.setsampwidth(w.getsampwidth())
    item.setframerate(w.getframerate())
    item.setcomptype(w.getcomptype(),w.getcompname())
    item.setnchannels(w.getnchannels())

for item in K:
    item.setsampwidth(w.getsampwidth())
    item.setframerate(w.getframerate())
    item.setcomptype(w.getcomptype(),w.getcompname())
    item.setnchannels(w.getnchannels())

'''for i in range (0, packetsize):
    replacement+=b'\x00'
Exemple #53
0
serv.bind((socket.gethostname(),777))
print '\n'*90,"*"*80,"\n",' '*30,'VoIP Rxer by Godson','\n\n','*'*80,'\n'*17
ip=raw_input("\nEnter the IP of other machine \t")
comp=raw_input("\n Select one of the compression technique from the following \n\t1.mue law\n\t2.ADPCM\n")
#constants
rate=33000 # these things mean nothing. i forgot to remove them(used for some experimentation)
bits=8
channels=1
data=[]
def recv(bytes):    
    global channels,bits,rate #I used these constants for debugging, they are not used in present code
    sou=pySonic.Source()
    print "Received amount of data",len(bytes)           
    sou.Sound=pySonic.MemorySample(bytes,channels,16,44000) #It simply forms a chunk of audio from the received samples
    sou.Play()
    print "Playing..."
    while sou.IsPlaying():
       time.sleep(0)   #Lol do we need  this line!!!!!
while True:    #A continuous loop which keeps on receving,decompressing & playing the audio data.
    sample,client=serv.recvfrom(100000) #dont be scared by that number
    data.append(sample) #All samples were joined together to make a meaningful audio. 
    bytes=''.join(data) # usually this called buffering. This one considerably slows down the program
    data=[]
    print "decoding..."
    if comp=='1':
        ulaw=audioop.ulaw2lin(bytes,4) #recovering original samples
        recv(ulaw)
    else:
        k=audioop.adpcm2lin(bytes,4,None)        
        recv(k[0])   
Exemple #54
0
"""Classes for manipulating audio devices (currently only for Sun and SGI)"""