当前位置: 首页>>代码示例>>Python>>正文


Python PyAudio.get_format_from_width方法代码示例

本文整理汇总了Python中pyaudio.PyAudio.get_format_from_width方法的典型用法代码示例。如果您正苦于以下问题:Python PyAudio.get_format_from_width方法的具体用法?Python PyAudio.get_format_from_width怎么用?Python PyAudio.get_format_from_width使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyaudio.PyAudio的用法示例。


在下文中一共展示了PyAudio.get_format_from_width方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: sine_tone

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
def sine_tone(frequency, duration, volume=1, sample_rate=22050):
  n_samples = int(sample_rate * duration)
  restframes = n_samples % sample_rate

  p = PyAudio()
  stream = p.open(format=p.get_format_from_width(2), # 16 bit
                  channels=2,
                  rate=sample_rate,
                  output=True)

  for i in xrange(0, 10):
    if i % 2 == 0:
      frequency = ZERO_FREQUENCY
    else:
      frequency = ONE_FREQUENCY

    s = lambda t: volume * math.sin(2 * math.pi * frequency * t / sample_rate)
    samples = (int(s(t) * 0x7f + 0x80) for t in xrange(n_samples))
    for buf in izip(*[samples]*sample_rate): # write several samples at a time
      stream.write(bytes(bytearray(buf)))

  # fill remainder of frameset with silence
  stream.write(b'\x80' * restframes)

  stream.stop_stream()
  stream.close()
  p.terminate()
开发者ID:kku1993,项目名称:sound-chat,代码行数:29,代码来源:play.py

示例2: main

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
def main():
    # read in some block data from pyaudio
    RATE=44100
    INPUT_BLOCK_TIME=0.2
    INPUT_FRAMES_PER_BLOCK=int(RATE*INPUT_BLOCK_TIME)
    pa=PyAudio()
         
    data=True
    fmt="%dh"%INPUT_FRAMES_PER_BLOCK
    total_rms=0
    total_blocks=0
    while data:
        for dr,subdr,fnames in os.walk(path):
            for filename in fnames:
                try:
                    print filename
                    wf=wave.open("%s/%s"%(path,filename),'rb')
                    strm=pa.open(format=pa.get_format_from_width(wf.getsampwidth()),
                        channels=wf.getnchannels(),
                        rate=wf.getframerate(),
                        input=True)
                    strm.stop_stream()
                    strm.close()
                    d=wf.readframes(INPUT_FRAMES_PER_BLOCK)
                    d=struct.unpack(fmt,d)
                    wf.close()
                    total_rms+=calc_rms(d)
                    total_blocks+=1
                except:
                    #print e
                    print "*** ERROR ***"
        data=False
    avg=total_rms/total_blocks
    print "The average is %f"%avg
开发者ID:sittigboyd,项目名称:dragonbot-listen,代码行数:36,代码来源:calc_avg_rms.py

示例3: __init__

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
class pybeeptone:
    def __init__(self, rate=44100):
        self.rate = 44100
        self.pyaudio = PyAudio()
        self.stream = self.pyaudio.open(
                                format = self.pyaudio.get_format_from_width(1),
                                channels = 1, rate = self.rate, output = True)

    def play_tone(self, freq=1000, duration=0.3):
        rate = self.rate
        length = int(math.ceil(self.rate*duration))
        data = ''.join( [chr(int(math.sin(x/((rate/freq)/math.pi))*127+128)) 
                            for x in xrange(length)] )
        self.stream.write(data)

    def play_rest(self, duration):
        rate = self.rate
        length = int(math.ceil(self.rate*duration))
        data = ''.join( [chr(int(128)) for x in xrange(length)] )
        self.stream.write(data)

    def close(self):
        self.stream.stop_stream()
        self.stream.close()
        self.pyaudio.terminate()
开发者ID:argon2008-aiti,项目名称:pybeeptone,代码行数:27,代码来源:pybeeptone.py

示例4: play

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
def play():
	
	wavName = 'test.wav'
	print "play %s" % (wavName)
	wf = wave.open(wavName, 'rb')

	pa = PyAudio()

	stream = pa.open(format=pa.get_format_from_width(wf.getsampwidth()),
					channels=wf.getnchannels(),
					rate=wf.getframerate(),
					output=True)

	data = wf.readframes(CHUNK)
	td = threading.Thread(target=startGame)
	td.start()
	while data != '':
		stream.write(data)
		data = wf.readframes(CHUNK)
		
		audio_data = np.fromstring(data, dtype=np.short)
		print data

	stream.stop_stream()
	stream.close()

	pa.terminate()
开发者ID:ej0cl6,项目名称:ciao,代码行数:29,代码来源:tool3.py

示例5: sine_tone

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
def sine_tone(frequencies, amplitudes, duration, volume=1.0, sample_rate=22050):
    n_samples = int(sample_rate * duration)
    restframes = n_samples % sample_rate

    p = PyAudio()
    stream = p.open(format=p.get_format_from_width(1), # 8bit
                    channels=1, # mono
                    rate=sample_rate,
                    output=True)

    def s(t):
        r = 0
        for i in range(0, len(frequencies)):
            r += volume * amplitudes[i] * math.sin(2 * math.pi * frequencies[i] * t / sample_rate)
        return r

    samples = (int(s(t) * 0x7f + 0x80) for t in range(n_samples))
    for buf in zip(*[samples]*sample_rate): # write several samples at a time
        stream.write(bytes(bytearray(buf)))

    # fill remainder of frameset with silence
    stream.write(b'\x80' * restframes)

    stream.stop_stream()
    stream.close()
    p.terminate()
开发者ID:yhamoudi,项目名称:Shape-Indexing,代码行数:28,代码来源:sound.py

示例6: Audio_play

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
def Audio_play(filepath):
    '''
    play audio
    '''
    CHUNK = 1024

    wf = wave.open(filepath, 'rb')
    pa = PyAudio()
    default_output = pa.get_default_host_api_info().get('defaultOutputDevice')
    stream =pa.open(format   = pa.get_format_from_width(wf.getsampwidth()), 
                    channels = wf.getnchannels(), 
                    rate     = wf.getframerate(), 
                    output   = True,
                    output_device_index = default_output)

    NUM = int(wf.getframerate()/CHUNK * 15)
    logging.info(">> START TO  PLAY  AUDIO")
    while NUM:
        data = wf.readframes(CHUNK)
        if data == " ": break
        stream.write(data)
        NUM -= 1
    stream.stop_stream()
    stream.close()
    del data
    pa.terminate()
开发者ID:hakehuang,项目名称:Auana-P,代码行数:28,代码来源:autool.py

示例7: Stream

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
class Stream(Thread):
    def __init__(self, f, on_terminated):
        self.__active = True
        self.__path = f
        self.__paused = True
        self.on_terminated = on_terminated
        self.__position = 0
        self.__chunks = []
        self.__pyaudio = PyAudio()
        Thread.__init__(self)
        self.start()

    def play(self):
        self.__paused = False

    def seek(self, seconds):
        self.__position = int(seconds * 10)

    def is_playing(self):
        return self.__active and not self.__paused

    def get_position(self):
        return int(self.__position / 10)

    def get_duration(self):
        return int(len(self.__chunks) / 10)

    def pause(self):
        self.__paused = True

    def kill(self):
        self.__active = False

    def __get_stream(self):
        self.__segment = AudioSegment.from_file(self.__path)
        self.__chunks = make_chunks(self.__segment, 100)
        return self.__pyaudio.open(format=self.__pyaudio.get_format_from_width(self.__segment.sample_width),
                                   channels=self.__segment.channels,
                                   rate=self.__segment.frame_rate,
                                   output=True)

    def run(self):
        stream = self.__get_stream()
        while self.__position < len(self.__chunks):
            if not self.__active:
                break
            if not self.__paused:
                # noinspection PyProtectedMember
                data = self.__chunks[self.__position]._data
                self.__position += 1
            else:
                free = stream.get_write_available()
                data = chr(0) * free
            stream.write(data)

        stream.stop_stream()
        self.__pyaudio.terminate()
        if self.__active:
            self.on_terminated()
开发者ID:mRokita,项目名称:sMusic-core,代码行数:61,代码来源:player.py

示例8: worker

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
 def worker():
     p = PyAudio()
     stream = p.open(format=p.get_format_from_width(2),
                     channels=1, rate=44100, output=True)
     while True:
         self.lock.acquire()
         stream.write(self.wavdata.tostring())
         self.lock.release()
开发者ID:chriskuech,项目名称:wavelab,代码行数:10,代码来源:composer.py

示例9: playWaveData

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
 def playWaveData(self, waveData):
     p = PyAudio()
     stream = p.open(format = p.get_format_from_width(1),
                     channels = 1,
                     rate = self.bitRate,
                     output = True)
     stream.write(waveData)
     stream.stop_stream()
     stream.close()
     p.terminate()
开发者ID:jrhoeber,项目名称:terminalComposer,代码行数:12,代码来源:PitchGenerator.py

示例10: openWav

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
    def openWav(self):

        chunk = 1024
        wf = wave.open(r"result.wav", 'rb')
        p = PyAudio()

        stream = p.open(format = p.get_format_from_width(wf.getsampwidth()), channels = wf.getnchannels(), rate = wf.getframerate(), output = True)
        while True:
            data = wf.readframes(chunk)
            if data == "":break
            stream.write(data)

        stream.close()
        p.terminate()
开发者ID:zhangbz,项目名称:AudioPrinterpy,代码行数:16,代码来源:main.py

示例11: AudioStream

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
class AudioStream(object):

    def __init__(self, sample_rate=44100, channels=1, width=2, chunk=1024,
                 input_device_index=None):
        self.sample_rate = sample_rate
        self.channels = channels
        self.width = width
        self.chunk = chunk
        self.input_device_index = input_device_index

    def __enter__(self):
        self._pa = PyAudio()
        if self.input_device_index is None:
            self.input_device_index = \
                self._pa.get_default_input_device_info()['index']
        self._stream = self._pa.open(
            format=self._pa.get_format_from_width(self.width),
            channels=self.channels,
            rate=self.sample_rate,
            input=True,
            frames_per_buffer=self.chunk,
            input_device_index=self.input_device_index)
        self._stream.start_stream()
        return self

    def read(self):
        ''' On a buffer overflow this returns 0 bytes. '''
        try:
            return self._stream.read(self.chunk)
        except IOError:
            return ''
        except AttributeError:
            raise Exception('Must be used as a context manager.')

    def stream(self):
        try:
            while True:
                bytes = self.read()
                if bytes:
                    self.handle(bytes)
        except (KeyboardInterrupt, SystemExit):
            pass

    def __exit__(self, type, value, traceback):
        self._stream.stop_stream()
        self._stream.close()
        self._pa.terminate()

    def handle(self, bytes):
        pass
开发者ID:shomchak,项目名称:lissajous,代码行数:52,代码来源:__init__.py

示例12: play

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
def play(wave_data):
    chunk_size = BITRATE/10

    p = PyAudio()
    stream = p.open(format = p.get_format_from_width(1), 
                channels = 1, 
                rate = BITRATE, 
                output = True)

    for chunk in itertools.islice(wave_data, chunk_size):
        stream.write(chunk)

    stream.stop_stream()
    stream.close()
    p.terminate()
开发者ID:jhobbs,项目名称:remorse,代码行数:17,代码来源:wavegen.py

示例13: Audio_play

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
def Audio_play():
#play audio
    CHUNK =8192
    filename = current_directory + '/audio_lib/source1.wav'
    wf = wave.open(filename, 'rb')
    pa = PyAudio()
    stream =pa.open(format = pa.get_format_from_width(wf.getsampwidth()), channels = wf.getnchannels(), rate = wf.getframerate(), output = True)

    NUM = int(wf.getframerate()/CHUNK * 15)
    logging.info("-> START PLAY AUDIO")
    while NUM:
        data = wf.readframes(CHUNK)
        if data == " ": break
        stream.write(data)
        NUM -= 1
    stream.stop_stream()
    stream.close()
    logging.info(">> PLAY DONE")
开发者ID:faygao52,项目名称:FITII,代码行数:20,代码来源:AUDIO.py

示例14: getWavFeature

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
def getWavFeature(wavName):
	print "processing %s" % (wavName)
	wf = wave.open(wavName, 'rb')
	pa = PyAudio()
	stream = pa.open(format=pa.get_format_from_width(wf.getsampwidth()),
					channels=wf.getnchannels(),
					rate=wf.getframerate(),
					output=True)

	data = wf.readframes(CHUNK)
	while data != '':
		stream.write(data)
		data = wf.readframes(CHUNK)

	stream.stop_stream()
	stream.close()
	pa.terminate()

	
开发者ID:ej0cl6,项目名称:ciao,代码行数:19,代码来源:tool3.py

示例15: button_play_Click

# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import get_format_from_width [as 别名]
def button_play_Click():
    global state, wav_out, stream_out, data_out, wave_form, wavefile_name, script_count, script_list

    state = 2   # change the state flag into "playing"

    # update button states
    button_stop.configure(state = NORMAL)
    button_record.configure(state = DISABLED)
    button_back.configure(state = DISABLED)
    button_next.configure(state = DISABLED)
    button_play.configure(state = DISABLED)

    wav_out = PyAudio()
    wave_form = wave.open(wavefile_name, 'rb')
    stream_out = wav_out.open(format = wav_out.get_format_from_width(wave_form.getsampwidth()),
                channels = wave_form.getnchannels(),
                rate = wave_form.getframerate(),
                output = True)
    
    data_out = wave_form.readframes(CHUNK)
    echo_text.configure(text = "Playing...", bg = 'blue', fg = 'white', font = ("Helvetica", 50))
    play_wave()
开发者ID:smilett,项目名称:PyRecorder,代码行数:24,代码来源:pyrecorder.py


注:本文中的pyaudio.PyAudio.get_format_from_width方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。