Example #1
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 ''
Example #2
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 ''
Example #3
0
def inout(fragment, timestamp, userdata):
    global queue, enc, dec, upsample, downsample
    try:
        #print [sys.getrefcount(x) for x in (None, upsample, downsample, enc, dec)]
        fragment1, downsample = audiospeex.resample(fragment,
                                                    input_rate=48000,
                                                    output_rate=8000,
                                                    state=downsample)
        fragment2, enc = audiospeex.lin2speex(fragment1,
                                              sample_rate=8000,
                                              state=enc)
        fragment3, dec = audiospeex.speex2lin(fragment2,
                                              sample_rate=8000,
                                              state=dec)
        fragment4, upsample = audiospeex.resample(fragment3,
                                                  input_rate=8000,
                                                  output_rate=48000,
                                                  state=upsample)
        fragment5 = fragment4 + fragment4  # create stereo
        # print len(fragment), len(fragment1), len(fragment2), len(fragment3), len(fragment4), len(fragment5)
        queue.append(fragment5)
        if len(queue) < 50:
            return ""
        fragment = queue.pop(0)
        return fragment
    except KeyboardInterrupt:
        pass
    except:
        print traceback.print_exc()
        return ""
Example #4
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 ""
Example #5
0
def inout(fragment, timestamp, userdata):
    global queue, upsample
    if queue:
        data = queue.pop(0)
        data, upsample = audiospeex.resample(data,
                                             input_rate=8000,
                                             output_rate=44100,
                                             state=upsample)
        return data
    return ""
Example #6
0
def inout(fragment, timestamp, userdata):
    global queue, enc, dec, upsample, downsample
    try:
        #print [sys.getrefcount(x) for x in (None, upsample, downsample, enc, dec)]
        fragment1, downsample = audiospeex.resample(fragment, input_rate=48000, output_rate=8000, state=downsample)
        fragment2, enc = audiospeex.lin2speex(fragment1, sample_rate=8000, state=enc)
        fragment3, dec = audiospeex.speex2lin(fragment2, sample_rate=8000, state=dec)
        fragment4, upsample = audiospeex.resample(fragment3, input_rate=8000, output_rate=48000, state=upsample)
        fragment5 = fragment4 + fragment4 # create stereo
        # print len(fragment), len(fragment1), len(fragment2), len(fragment3), len(fragment4), len(fragment5)
        queue.append(fragment5)
        if len(queue) < 50:
            return ""
        fragment = queue.pop(0)
        return fragment
    except KeyboardInterrupt:
        pass
    except:
        print traceback.print_exc()
        return ""
Example #7
0
    def inout(linear, stream_time, userdata):
        try:
            if audio_in:
                linear, userdata[2] = audiospeex.resample(
                    linear,
                    input_rate=samplerate,
                    output_rate=16000,
                    state=userdata[2])
                payload, userdata[3] = audiospeex.lin2speex(linear,
                                                            sample_rate=16000,
                                                            state=userdata[3])
                payload = '\xb2' + payload
                userdata[4] += 20  # millisec
                header = Header(time=userdata[4],
                                size=len(payload),
                                type=Message.AUDIO,
                                streamId=0)
                msg = Message(header, payload)
                queue_mic.put(msg)
        except:
            logger.exception('audio inout exception: resample, encode')

        try:
            # ignore mic input (linear) for now
            if audio_out:
                msg = queue_spk.get(block=False)
                first, payload = msg.data[0], msg.data[1:]
                if first == '\xb2':  # speex
                    linear, userdata[0] = audiospeex.speex2lin(
                        payload, sample_rate=16000, state=userdata[0])
                    linear, userdata[1] = audiospeex.resample(
                        linear,
                        input_rate=16000,
                        output_rate=samplerate,
                        state=userdata[1])
                    return linear
        except Empty:
            pass
        except:
            logger.exception('audio inout exception: decode, resample')
        return ''
Example #8
0
def record(output=None):
    """
    Record some audio.
    """
    import pyaudio
    import audiospeex
    chunk = 1024
    channels = 1
    format = pyaudio.paInt16
    rate = 44100
    p = pyaudio.PyAudio()
    stream = p.open(format=format, channels=channels, rate=rate,
                    input=True, frames_per_buffer=chunk)
    logger.info('Recording... (press Ctrl+C to stop)')
    rec = []
    while True:
        try:
            rec.append(stream.read(chunk))
        except IOError as e:
            if e[1] == pyaudio.paInputOverflowed:
                rec.append('\x00' * 16 * chunk * channels)
        except:
            break
    stream.close()
    p.terminate()
    speex = []
    downsample = None
    for d in rec:
        frag, downsample = audiospeex.resample(d, input_rate=rate, output_rate=8000, quality=8, state=downsample)
        speex.append(frag)
    data = ''.join(speex)
    try:
        wf = wave.open('tentative.raw', 'wb')
        wf.setnchannels(channels)
        wf.setsampwidth(p.get_sample_size(format))
        wf.setframerate(8000)
        wf.writeframes(data)
        wf.close()
        data = commands.getoutput('./speexEnc')
    finally:
        if False:#os.path.exists('tentative.raw'):
            os.remove('tentative.raw')
    if output is not None:
        print 'Saving recording to %s' % output
        with open(output, 'wb') as f:
            f.write(data)
    return data