本文整理汇总了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()
示例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
示例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()
示例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()
示例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()
示例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()
示例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()
示例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()
示例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()
示例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()
示例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
示例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()
示例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")
示例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()
示例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()