コード例 #1
0
ファイル: processing.py プロジェクト: bgtwoigu/wavenet-2
def preprocess(oldFileName):
    #preprocess takes in the name of a .wav file (oldFileName) and returns
    #u-law downsampled version of the file

    file = wave.open(filename, "rb")

    num_channels = file.getnchannels()
    sample_rate = file.getframerate()
    sample_width = file.getsampwidth()
    num_frames = file.getnframes()

    #Grab the bytes from our WAV file
    raw_frames = file.readframes(num_frames)
    file.close()

    total_samples = num_frames * num_channels

    if sample_rate != SAMPLE_RATE:
        u_law = audioop.ratecv(raw_frames, sample_width, num_channels,
                               sample_rate, SAMPLE_RATE, None)
        u_law = audioop.lin2ulaw(u_law[0], sample_width)

    else:
        u_law = audioop.lin2ulaw(raw_frames, sample_width)

    u_law = list(u_law)
    u_law = [ord(x) // Q_FACTOR for x in u_law]

    return np.asarray(u_law)
コード例 #2
0
ファイル: test_audioop.py プロジェクト: BrythonServer/brython
 def test_lin2ulaw(self):
     self.assertEqual(audioop.lin2ulaw(datas[1], 1),
                      b'\xff\xad\x8e\x0e\x80\x00\x67')
     self.assertEqual(audioop.lin2ulaw(datas[2], 2),
                      b'\xff\xad\x8e\x0e\x80\x00\x7e')
     self.assertEqual(audioop.lin2ulaw(datas[4], 4),
                      b'\xff\xad\x8e\x0e\x80\x00\x7e')
コード例 #3
0
def open_input(filename):
    stream = wave.open(filename, "rb")

    input_num_channels = stream.getnchannels()
    input_sample_rate = stream.getframerate()
    input_sample_width = stream.getsampwidth()
    input_num_frames = stream.getnframes()

    raw_data = stream.readframes(input_num_frames)  # Returns byte data
    stream.close()

    total_samples = input_num_frames * input_num_channels

    print "Sample Width: {} ({}-bit)".format(input_sample_width,
                                             8 * input_sample_width)
    print "Number of Channels: " + str(input_num_channels)
    print "Sample Rate " + str(input_sample_rate)

    print "Number of Samples: " + str(total_samples)
    print "Duration: {0:.2f}s".format(total_samples / float(input_sample_rate))
    print "Raw Data Size: " + str(len(raw_data))

    if input_sample_rate != SAMPLE_RATE:
        u_law = audioop.ratecv(raw_data, input_sample_width,
                               input_num_channels, input_sample_rate,
                               SAMPLE_RATE, None)
        u_law = audioop.lin2ulaw(u_law[0], input_sample_width)
    else:
        u_law = audioop.lin2ulaw(raw_data, input_sample_width)

    u_law = list(u_law)
    u_law = [ord(x) // Q_FACTOR for x in u_law]

    return np.asarray(u_law), input_sample_rate
コード例 #4
0
 def test_lin2ulaw(self):
     self.assertEqual(audioop.lin2ulaw(datas[1], 1),
                      b'\xff\xad\x8e\x0e\x80\x00\x67')
     self.assertEqual(audioop.lin2ulaw(datas[2], 2),
                      b'\xff\xad\x8e\x0e\x80\x00\x7e')
     self.assertEqual(audioop.lin2ulaw(datas[4], 4),
                      b'\xff\xad\x8e\x0e\x80\x00\x7e')
コード例 #5
0
ファイル: test_audioop.py プロジェクト: B-Rich/breve
def testlin2ulaw(data):
    if verbose:
        print 'lin2ulaw'
    if audioop.lin2ulaw(data[0], 1) != '\xff\xe7\xdb' or \
              audioop.lin2ulaw(data[1], 2) != '\xff\xff\xff' or \
              audioop.lin2ulaw(data[2], 4) != '\xff\xff\xff':
        return 0
    return 1
コード例 #6
0
def testlin2ulaw(data):
    if verbose:
        print 'lin2ulaw'
    if audioop.lin2ulaw(data[0], 1) <> '\377\347\333' or \
              audioop.lin2ulaw(data[1], 2) <> '\377\377\377' or \
              audioop.lin2ulaw(data[2], 4) <> '\377\377\377':
        return 0
    return 1
コード例 #7
0
def testlin2ulaw(data):
    if verbose:
        print 'lin2ulaw'
    if audioop.lin2ulaw(data[0], 1) != '\xff\xe7\xdb' or \
              audioop.lin2ulaw(data[1], 2) != '\xff\xff\xff' or \
              audioop.lin2ulaw(data[2], 4) != '\xff\xff\xff':
        return 0
    return 1
コード例 #8
0
def testlin2ulaw(data):
    if verbose:
        print 'lin2ulaw'
    if audioop.lin2ulaw(data[0], 1) != '\377\347\333' or \
              audioop.lin2ulaw(data[1], 2) != '\377\377\377' or \
              audioop.lin2ulaw(data[2], 4) != '\377\377\377':
        return 0
    return 1
コード例 #9
0
ファイル: test_audioop.py プロジェクト: 0jpq0/kbengine
 def test_lin2ulaw(self):
     self.assertEqual(audioop.lin2ulaw(datas[1], 1),
                      b'\xff\xad\x8e\x0e\x80\x00\x67')
     self.assertEqual(audioop.lin2ulaw(bytearray(datas[1]), 1),
                      b'\xff\xad\x8e\x0e\x80\x00\x67')
     self.assertEqual(audioop.lin2ulaw(memoryview(datas[1]), 1),
                      b'\xff\xad\x8e\x0e\x80\x00\x67')
     for w in 2, 3, 4:
         self.assertEqual(audioop.lin2ulaw(datas[w], w),
                          b'\xff\xad\x8e\x0e\x80\x00\x7e')
コード例 #10
0
 def test_lin2ulaw(self):
     self.assertEqual(audioop.lin2ulaw(datas[1], 1),
                      b'\xff\xad\x8e\x0e\x80\x00\x67')
     self.assertEqual(audioop.lin2ulaw(bytearray(datas[1]), 1),
                      b'\xff\xad\x8e\x0e\x80\x00\x67')
     self.assertEqual(audioop.lin2ulaw(memoryview(datas[1]), 1),
                      b'\xff\xad\x8e\x0e\x80\x00\x67')
     for w in 2, 3, 4:
         self.assertEqual(audioop.lin2ulaw(datas[w], w),
                          b'\xff\xad\x8e\x0e\x80\x00\x7e')
コード例 #11
0
 def test_lin2ulaw(self):
     self.assertEqual(audioop.lin2ulaw(datas[1], 1),
                      b'\xff\xad\x8e\x0e\x80\x00\x67')
     self.assertEqual(audioop.lin2ulaw(bytearray(datas[1]), 1),
                      b'\xff\xad\x8e\x0e\x80\x00\x67')
     self.assertEqual(audioop.lin2ulaw(memoryview(datas[1]), 1),
                      b'\xff\xad\x8e\x0e\x80\x00\x67')
     for w in 2, 3, 4:
         # [jart] fixed off-by-one w/ itu primary materials
         self.assertEqual(audioop.lin2ulaw(datas[w], w),
                          b'\xff\xad\x8e\x0e\x80\x00\x7f')
コード例 #12
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')
コード例 #13
0
ファイル: sipcaller.py プロジェクト: gavinljj/p2p-sip
 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 ""
コード例 #14
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 ''
コード例 #15
0
	def read(self, samples, CODING_MODE = PCMUMODE):
		#Encode data using mu-law or a-law
		data, eod = ('', False) 
		if self.isopen:

 			if self.mode == TTSMODE:
				data, eod = self.ttsdevice.read(samples)

			elif self.mode == OSMODE:
				data = self.osdevice.read(samples)
	
			elif self.mode == FILEMODE:
				data, eod = self.filedevice.read(samples)

			if CODING_MODE == PCMUMODE:
				data = audioop.lin2ulaw(data, 2)

			elif CODING_MODE == PCMAMODE:
				data = audioop.lin2alaw(data, 2)

			#For future implementation of GSM EFR
		#	elif CODING_MODE == GSMMODE:
		#		data = self.gsm_encoder.encode(data)

			#Else the data is going to be sent as RAW data			

			return data, eod

		else:
			print "Error reading data... please check if the device is opened..."
			return data, eod
コード例 #16
0
ファイル: caller.py プロジェクト: msingh05/rtclite
 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 ''
コード例 #17
0
ファイル: voicetest.py プロジェクト: do1kbl/hylink
def wavfile(filename):
    """
    Play a wave file over RTP

    :param filename:
    :return:
    """

    global _rtpseq, _rtptstamp

    # load the wav file, using librosa to convert to mono at the repeater's RTP sample rate
    data, sr = librosa.load(filename, sr=SAMPLE_RATE, mono=True)

    # create an RTP packet
    pkt = RTPPacket()
    pkt.payloadType = RTPPayloadType.HYTERA_PCMU    # ITU-T G.711 mu-law
    # NOTE: Extension must be correct or the repeater won't repeat the audio
    pkt.extension = {'type': 0x15, 'data': [0, 0, 0]}

    next_time = time.time()
    pace = RTP_FRAMESZ / SAMPLE_RATE
    log.debug("Starting WAV playback, pace=%.2f ms" % (pace * 1000.))
    for i in range(0, len(data), RTP_FRAMESZ):
        # process audio in chunks of the RTP frame size
        chunk = data[i:i+160]

        # update sequence number and timestamp
        _rtpseq += 1
        _rtptstamp += RTP_FRAMESZ
        pkt.seq = _rtpseq
        pkt.timestamp = _rtptstamp

        def _sx(x):
            """ float32-to-signed16 with saturation clipping """
            r = int(round(32767*x))
            if r > 32767:
                return 32767
            elif r < -32767:
                return -32767
            else:
                return r

        # convert from float32 to signed16
        chunk = [_sx(x) for x in chunk]

        # we now have <= 160 bytes, pad with silence if needed
        if len(chunk) < RTP_FRAMESZ:
            padding = [0] * (RTP_FRAMESZ - len(chunk))
            chunk += padding

        # convert to ITU G.711 mu-law
        pkt.payload = audioop.lin2ulaw(samp_to_signed_bin(chunk), 2)

        # send packet
        rtpPort.send(pkt)

        # wait for next interval
        next_time += pace
        time.sleep(max(0., next_time - time.time()))
コード例 #18
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
コード例 #19
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
コード例 #20
0
 def playFile(self, wavefilename, CallType, DstId):
     self.CallType = CallType
     self.DstId = DstId
     wavefile = wave.open(wavefilename, 'rb')
     # TODO: Check format and convert if necessary
     self.TxBufferULaw += audioop.lin2ulaw(
         wavefile.readframes(3 * 60 * self.PCMSAMPLERATE),
         2)  # load max 3min
     wavefile.close()
コード例 #21
0
	def writeframesraw(self, data):
		self._ensure_header_written()
		nframes = len(data) / self._framesize
		if self._comptype == 'ULAW':
			import audioop
			data = audioop.lin2ulaw(data, self._sampwidth)
		self._file.write(data)
		self._nframeswritten = self._nframeswritten + nframes
		self._datawritten = self._datawritten + len(data)
コード例 #22
0
 def writeframesraw(self, data):
     if not isinstance(data, (bytes, bytearray)):
         data = memoryview(data).cast('B')
     self._ensure_header_written()
     if self._comptype == 'ULAW':
         import audioop
         data = audioop.lin2ulaw(data, self._sampwidth)
     nframes = len(data) // self._framesize
     self._file.write(data)
     self._nframeswritten = self._nframeswritten + nframes
     self._datawritten = self._datawritten + len(data)
コード例 #23
0
ファイル: test_audioop.py プロジェクト: Qointum/pypy
    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)
コード例 #24
0
ファイル: sunau.py プロジェクト: sailfishos-mirror/cpython
 def writeframesraw(self, data):
     if not isinstance(data, (bytes, bytearray)):
         data = memoryview(data).cast('B')
     self._ensure_header_written()
     if self._comptype == 'ULAW':
         with warnings.catch_warnings():
             warnings.simplefilter('ignore', category=DeprecationWarning)
             import audioop
         data = audioop.lin2ulaw(data, self._sampwidth)
     nframes = len(data) // self._framesize
     self._file.write(data)
     self._nframeswritten = self._nframeswritten + nframes
     self._datawritten = self._datawritten + len(data)
コード例 #25
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')
コード例 #26
0
ファイル: test_audioop.py プロジェクト: rrd257r/Thruster
    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)
コード例 #27
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)
コード例 #28
0
ファイル: wav_chunks.py プロジェクト: akkuzativ/E-media
        def channels_to_bytes(fmtChunk: FmtChunk, contents) -> bytes:
            combined_channels = [None] * (len(contents.samples) *
                                          len(contents.samples[0]))
            for c in range(0, fmtChunk.data.num_channels):
                combined_channels[c::fmtChunk.data.
                                  num_channels] = contents.samples[c]

            bytes_sample = b""
            bytes_samples = []

            for sample in combined_channels:
                if fmtChunk.data.bits_per_sample >= 8:
                    sample_len = int(fmtChunk.data.bits_per_sample / 8)
                else:
                    sample_len = int(fmtChunk.data.bits_per_sample / 4)
                if fmtChunk.data.audio_format == 1:
                    if sample_len == 1:
                        bytes_sample = int.to_bytes(sample,
                                                    byteorder="little",
                                                    signed=False,
                                                    length=sample_len)
                    else:
                        bytes_sample = int.to_bytes(sample,
                                                    byteorder="little",
                                                    signed=True,
                                                    length=sample_len)
                elif fmtChunk.data.audio_format == 3:
                    if sample_len == 4:
                        bytes_sample = bytearray(struct.pack("f", sample))
                    else:
                        bytes_sample = bytearray(struct.pack("d", sample))
                elif fmtChunk.data.audio_format == 6:
                    bytes_sample = audioop.lin2alaw(
                        int.to_bytes(sample,
                                     byteorder="little",
                                     signed=True,
                                     length=sample_len), sample_len)
                elif fmtChunk.data.audio_format == 7:
                    bytes_sample = audioop.lin2ulaw(
                        int.to_bytes(sample,
                                     byteorder="little",
                                     signed=True,
                                     length=sample_len), sample_len)
                else:
                    raise Exception
                bytes_samples.append(bytes_sample)
            return b"".join(bytes_samples)
コード例 #29
0
ファイル: cmpaf.py プロジェクト: Claruarius/stblinux-2.6.37
	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()
コード例 #30
0
    def text_to_speech(self):
        tts_config = CONFIG['tts_config']
        models_folder = Path(tts_config['folder'])

        model_path = str(models_folder / tts_config['model'])
        model_config_path = str(models_folder / tts_config['model_config'])
        vocoder_path = str(models_folder / tts_config['vocoder'])
        vocoder_config_path = str(models_folder / tts_config['vocoder_config'])

        self.mozilla_tts = Synthesizer(model_path, model_config_path,
                                       vocoder_path, vocoder_config_path)

        while True:
            response = self.chatbot_to_tts_queue.get()
            print("TTS:", response)

            sound_arr = np.array(self.mozilla_tts.tts(response))

            sound_arr *= 2**15
            sound_arr = sound_arr.astype('int16')

            sound = bytes(sound_arr)
            sound, _ = audioop.ratecv(sound, 2, 1, self.MOZILLA_TTS_AUDIO_RATE,
                                      self.IN_AUDIO_RATE, None)

            ulaw_sound = audioop.lin2ulaw(sound, 2)

            chunk_len = 540
            chunks = len(ulaw_sound) // chunk_len
            extra = len(ulaw_sound) - (chunks * chunk_len)

            for c in range(chunks):
                chunk = ulaw_sound[c * chunk_len:c * chunk_len + chunk_len]
                self.audio_out_queue.put(
                    base64.b64encode(chunk).decode('utf-8'))

            if extra != 0:
                chunk = ulaw_sound[-extra:]
                self.audio_out_queue.put(
                    base64.b64encode(chunk).decode('utf-8'))

            self.transcript.append({
                "speaker": "self",
                "text": response,
                "datetime": dt.datetime.now().isoformat()
            })
コード例 #31
0
def wav2au(data):
    # Very limited! Assumes a standard 8-bit mono .wav as input
    import audioop, struct
    freq, = struct.unpack("<i", data[24:28])
    data = data[44:]
    data = audioop.bias(data, 1, -128)
    data, ignored = audioop.ratecv(data, 1, 1, freq, 8000, None)
    data = audioop.lin2ulaw(data, 1)
    data = struct.pack('>4siiiii8s',
                       '.snd',                         # header
                       struct.calcsize('>4siiiii8s'),  # header size
                       len(data),                      # data size
                       1,                              # encoding
                       8000,                           # sample rate
                       1,                              # channels
                       'magic.au') + data
    return data
コード例 #32
0
ファイル: cmpaf.py プロジェクト: nagayev/old-python
	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()
コード例 #33
0
 def next(self):
     if self._framecount == 0:
         self._wvau.close()
         raise StopIteration
     lindata = self._wvau.readframes(self._framesperquantum)
     # Convert this data to ulaw
     data = audioop.lin2ulaw(lindata, 2)
     # Transcribe using Google Speech API
     result = gspeech.transcribe_audio(data, self._wvau.getframerate(),
                                       self.language, None)
     if self._framecount < self._framesperquantum:
         self._framecount = 0
         timestring = "From " + convert_secs(
             self._elapsed) + " to " + convert_secs(self._duration) + "\n"
         self._elapsed = self._duration
     else:
         self._framecount -= self._framesperquantum
         timestring = "From " + convert_secs(
             self._elapsed) + " to " + convert_secs(
                 self._elapsed + self._quantumsize) + "\n"
         self._elapsed += self._quantumsize
     return timestring + result
コード例 #34
0
def avs_send_audio(file_name):

    global g_socket
    
    # Get file size
    file_size = 0
    if os.path.isfile(file_name):
        file_info = os.stat(file_name)
        file_size = file_info.st_size

    # Get file bytes
    file = open(file_name, "rb")
    file_bytes = file.read(file_size)
    file.close()

    # Compress file bytes from 16-bit to 8-bit
    if CONF_AUDIO_SEND_BITDEPTH == DEVICE_CAPABILITIES_BITDEPTH_8:
        file_bytes = audioop.lin2ulaw(file_bytes, 2)
    file_size = len(file_bytes)

    # Send size of Alexa request
    timeval = struct.pack('ll', CONF_TIMEOUT_SEND, 0)
    g_socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDTIMEO, timeval)
    val = struct.pack('i', file_size)
    g_socket.sendall(val)

    # Send Alexa request
    # send in chunks instead of sending all to simulate RS485 slowness
    # g_socket.sendall(bytes(file_bytes))
    sent_data = 0
    send_size = CONF_CHUNK_SIZE
    file_bytes = bytes(file_bytes)
    #print("{}".format(file_size))
    while sent_data < file_size:
        if file_size - sent_data < send_size:
            send_size = file_size - sent_data
        g_socket.sendall(file_bytes[sent_data:sent_data+send_size])
        sent_data += send_size
コード例 #35
0
def RtpSendThread(clsRtpThread):
    clsRtpThread.bSendThreadRun = True

    if (clsRtpThread.bUseTwoAudio):
        clsAudio = pyaudio.PyAudio()
        clsStream = clsAudio.open(format=8,
                                  channels=1,
                                  rate=8000,
                                  input=True,
                                  frames_per_buffer=160)

    iFlags = 0x80
    iPayload = 0
    iSeq = 0
    iTimeStamp = 0
    iSsrc = 200

    while (clsRtpThread.bStopEvent == False):
        iSeq += 1
        iTimeStamp += 160

        if (clsRtpThread.bUseTwoAudio):
            arrPcm = clsStream.read(160)
        else:
            arrPcm = clsRtpThread.clsStream.read(160)

        arrPayload = audioop.lin2ulaw(arrPcm, 2)
        szPacket = struct.pack('!BBHII', iFlags, iPayload, iSeq, iTimeStamp,
                               iSsrc) + arrPayload
        clsRtpThread.hUdpSocket.sendto(
            szPacket, (clsRtpThread.strDestIp, clsRtpThread.iDestPort))

    if (clsRtpThread.bUseTwoAudio):
        clsStream.close()
        clsAudio.terminate()

    clsRtpThread.bSendThreadRun = False
コード例 #36
0
ファイル: voicetest.py プロジェクト: do1kbl/hylink
def silence(nsecs=1.0):
    """
    send <nsecs> seconds of silence over RTP

    :param nsecs: number of seconds of silence
    :return: nothing
    """

    global _rtpseq, _rtptstamp

    pkt = RTPPacket()
    pkt.payload = audioop.lin2ulaw(samp_to_signed_bin([0] * RTP_FRAMESZ), 2)
    pkt.payloadType = RTPPayloadType.HYTERA_PCMU
    # NOTE: Extension must be correct or the repeater won't repeat the audio
    pkt.extension = {'type': 0x15, 'data': [0, 0, 0]}

    # generate some RTP frames
    for i in range(round((SAMPLE_RATE / RTP_FRAMESZ) * nsecs)):
        _rtpseq += 1
        _rtptstamp += RTP_FRAMESZ
        pkt.seq = _rtpseq
        pkt.timestamp = _rtptstamp
        rtpPort.send(pkt)
        time.sleep(RTP_FRAMESZ / SAMPLE_RATE)
コード例 #37
0
	def _lin2ulaw(self, data):
		import audioop
		return audioop.lin2ulaw(data, 2)
コード例 #38
0
 def _lin2ulaw(self, data):
     with warnings.catch_warnings():
         warnings.simplefilter('ignore', category=DeprecationWarning)
         import audioop
     return audioop.lin2ulaw(data, 2)
コード例 #39
0
ファイル: cmpaf.py プロジェクト: mcyril/ravel-ftn
# Compare different audio compression schemes.
コード例 #40
0
 def encode_value_library(self, original_value, byte_size=2):
     return audioop.lin2ulaw(original_value, byte_size)
コード例 #41
0
 def test_ulaw2lin(self):
     # Cursory
     d = audioop.lin2ulaw(data[0], 1)
     self.assertEqual(audioop.ulaw2lin(d, 1), data[0])
コード例 #42
0
ファイル: test_audioop.py プロジェクト: 1310701102/sl4a
 def test_ulaw2lin(self):
     # Cursory
     d = audioop.lin2ulaw(data[0], 1)
     self.assertEqual(audioop.ulaw2lin(d, 1), data[0])
コード例 #43
0
ファイル: test_audioop.py プロジェクト: 1310701102/sl4a
 def test_lin2ulaw(self):
     self.assertEqual(audioop.lin2ulaw(data[0], 1), '\xff\xe7\xdb')
     self.assertEqual(audioop.lin2ulaw(data[1], 2), '\xff\xff\xff')
     self.assertEqual(audioop.lin2ulaw(data[2], 4), '\xff\xff\xff')
コード例 #44
0
ファイル: aifc.py プロジェクト: mcyril/ravel-ftn
"""Stuff to parse AIFF-C and AIFF files.
コード例 #45
0
ファイル: converters.py プロジェクト: habnabit/divmod-sine
 def _encode(self, bytes):
     return audioop.lin2ulaw(bytes, 2)
コード例 #46
0
def testulaw2lin(data):
	# Cursory
	d = audioop.lin2ulaw(data[0], 1)
	if audioop.ulaw2lin(d, 1) <> data[0]:
		return 0
	return 1
コード例 #47
0
ファイル: sunau.py プロジェクト: mcyril/ravel-ftn
"""Stuff to parse Sun and NeXT audio files.
コード例 #48
0
ファイル: audiosym.py プロジェクト: tyralyn/ballin-shame
K.append(sunau.open("soundfiles/whatYouFeel1e.au","w"))
K.append(sunau.open("soundfiles/whatYouFeel1f.au","w"))
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())