本文整理汇总了Python中pyaudio.PyAudio.terminate方法的典型用法代码示例。如果您正苦于以下问题:Python PyAudio.terminate方法的具体用法?Python PyAudio.terminate怎么用?Python PyAudio.terminate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyaudio.PyAudio
的用法示例。
在下文中一共展示了PyAudio.terminate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: record
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [as 别名]
def record(self):
#open the input of wave
pa = PyAudio()
stream = pa.open(format = paInt16, channels = 1,
rate = self.getRate(pa), input = True,
frames_per_buffer = self.NUM_SAMPLES)
save_buffer = []
record_start = False
record_end = False
no_record_times = 0
while 1:
#read NUM_SAMPLES sampling data
string_audio_data = stream.read(self.NUM_SAMPLES)
if record_start == True :save_buffer.append(string_audio_data)
print max(array('h', string_audio_data))
if max(array('h', string_audio_data)) >5000:
record_start = True
no_record_times = 0
else:
no_record_times += 1
if record_start == False:continue
if no_record_times >10:
break
stream.close()
pa.terminate()
return save_buffer
示例2: record
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [as 别名]
def record():
pa = PyAudio()
in_stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE, input=True, frames_per_buffer=BUFFER_SIZE)
out_stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE,output=True)
save_count = 0
save_buffer = []
save_data = []
save_count = SAVE_LENGTH
print 'start recording'
while save_count>0:
string_audio_data = in_stream.read(BUFFER_SIZE)
audio_data = np.fromstring(string_audio_data, dtype=np.short)
print type(audio_data)
save_buffer.append( string_audio_data )
save_data.append( audio_data )
save_count = save_count - 1
#print 'save %s' % (wav.fileName)
#save_wave_file(wav.fileName, save_buffer)
save_wave_file("test.wav", save_buffer)
pa.terminate()
示例3: Audio_play
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [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()
示例4: sine_tone
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [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()
示例5: __init__
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [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()
示例6: record
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [as 别名]
def record(self):
pa = PyAudio()
in_stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE, input=True, frames_per_buffer=BUFFER_SIZE)
save_count = 0
save_buffer = []
save_count = SAVE_LENGTH
print 'start recording'
while save_count>0:
string_audio_data = in_stream.read(BUFFER_SIZE)
audio_data = np.fromstring(string_audio_data, dtype=np.short)
save_buffer.append( string_audio_data )
save_count = save_count - 1
print 'save %s' % (self.fileName)
pa.terminate()
wf = wave.open(self.fileName, 'wb')
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(SAMPLING_RATE)
wf.writeframes("".join(save_buffer))
wf.close()
self.stringAudioData = "".join(save_buffer)
save_data = np.fromstring(self.stringAudioData, dtype=np.short)
self.audioData = save_data[10000:10000+4608*4]
self.stringAudioData = self.audioData.tostring()
self.cutAudio = self.audioData
# self.cut2()
self.getFeature()
示例7: record
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [as 别名]
def record(self, time):
audio = PyAudio()
stream = audio.open(input_device_index=self.device_index,
output_device_index=self.device_index,
format=self.format,
channels=self.channel,
rate=self.rate,
input=True,
frames_per_buffer=self.chunk
)
print "Recording..."
frames = []
for i in range(0, self.rate / self.chunk * time):
data = stream.read(self.chunk)
frames.append(data)
stream.stop_stream()
print "Recording Complete"
stream.close()
audio.terminate()
write_frames = open_audio(self.file, 'wb')
write_frames.setnchannels(self.channel)
write_frames.setsampwidth(audio.get_sample_size(self.format))
write_frames.setframerate(self.rate)
write_frames.writeframes(''.join(frames))
write_frames.close()
self.convert()
示例8: sine_tone
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [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()
示例9: play
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [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()
示例10: Stream
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [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()
示例11: Audio_record_play
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [as 别名]
def Audio_record_play(seconds,play,filename):
'''
This function include record and play, if you want to play and record,
please set the play is True.
The sample rate is 44100
Bit:16
'''
CHUNK = 1024
CHANNELS = 2
SAMPLING_RATE = 44100
FORMAT = paInt16
NUM = int(SAMPLING_RATE/CHUNK * seconds)
save_buffer = []
if play is True:
source_file = autohandle_directory + '/audio_lib/'+'source1.wav'
swf = wave.open(source_file, 'rb')
#open audio stream
pa = PyAudio()
default_input = pa.get_default_host_api_info().get('defaultInputDevice')
stream = pa.open(
format = FORMAT,
channels = CHANNELS,
rate = SAMPLING_RATE,
input = True,
output = play,
frames_per_buffer = CHUNK,
input_device_index = default_input
)
logging.info(">> START TO RECORD AUDIO")
while NUM:
save_buffer.append(stream.read(CHUNK))
NUM -= 1
if play is True:
data = swf.readframes(CHUNK)
stream.write(data)
if data == " ": break
#close stream
stream.stop_stream()
stream.close()
pa.terminate()
# save wav file
def save_wave_file(filename,data):
wf_save = wave.open(filename, 'wb')
wf_save.setnchannels(CHANNELS)
wf_save.setsampwidth(pa.get_sample_size(FORMAT))
wf_save.setframerate(SAMPLING_RATE)
wf_save.writeframes("".join(data))
wf_save.close()
save_wave_file(filename, save_buffer)
del save_buffer[:]
示例12: __init__
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [as 别名]
class RadioServer:
def __init__(self):
self.pa=PyAudio()
self.input_names={}
self.output_names={}
self.listeners=[]
def registerDevices(self,inputDevice=None,outputDevice=None):
if inputDevice==None:
self.inputDevice=InputDevice(self.pa)
else:
self.inputDevice=inputDevice
if outputDevice==None:
self.outputDevice=OutputDevice(self.pa)
else:
self.outputDevice=outputDevice
def registerInput(self,descriptor,name):
self.input_names[name]=descriptor
def registerOutput(self,descriptor,name):
self.output_names[name]=descriptor
def subscribeToInput(self,name,queue):
self.inputDevice.subscribe(queue,self.input_names[name])
def subscribeToOutput(self,name,queue):
self.outputDevice.subscribe(queue,self.output_names[name])
def addListener(self,listener):
self.listeners.append(listener)
listener.bind(self)
def start(self):
for l in self.listeners:
l.start()
self.inputDevice.start()
self.outputDevice.start()
def stop(self):
self.inputDevice.stop()
self.outputDevice.stop()
for l in self.listeners:
l.stop()
self.pa.terminate()
def sigint(self,signal,frame):
self.stop()
def run_forever(self):
self.start()
signal.signal(signal.SIGINT,lambda s,f:self.sigint(s,f))
signal.pause()
示例13: playWaveData
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [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()
示例14: play
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [as 别名]
def play(self):
print "play %s" % (self.fileName)
pa = PyAudio()
stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE, output=True, frames_per_buffer=BUFFER_SIZE)
stream.write(self.stringAudioData)
# stream.write(self.cutAudio)
stream.stop_stream()
stream.close()
pa.terminate()
示例15: Record
# 需要导入模块: from pyaudio import PyAudio [as 别名]
# 或者: from pyaudio.PyAudio import terminate [as 别名]
def Record(self):
global CHANNELS
# 开启声音输入
pa = PyAudio()
stream = pa.open(format=paInt16, channels=CHANNELS, rate=self.sampling_rate, input=True,
frames_per_buffer=self.cacheblock_size)
save_count = 0 # 已经保存的样本块
silence_count = 0 # 持续无声音的样本块
save_buffer = [] # 音频缓冲
try:
print "start recording"
while True:
# 录音、取样
string_audio_data = stream.read(self.cacheblock_size)
# 将读入的数据转换为数组
audio_data = np.fromstring(string_audio_data, dtype=np.short)
# 样本值大于LEVEL的取样为成功取样,计算成功取样的样本的个数
large_sample_count = np.sum(audio_data > self.level)
print "Peak:",np.max(audio_data)," Sum:",large_sample_count
# 如果成功取样数大于SAMPLING_NUM,则当前数据块取样都成功
if large_sample_count > self.sampling_num:
# 有成功取样的数据块时,样本计数+1
save_count += 1
else:
# 有成功录取的块后,若取样失败,此时可能处于静音状态,静音计数+1
if(save_count > 0):
silence_count += 1
# 取样失败次数是否超过最大值
if (save_count <= self.max_save_length) and (silence_count <= self.max_silence_length):
# 将要保存的数据存放到save_buffer中
save_buffer.append(string_audio_data)
else:
# 将save_buffer中的数据写入WAV文件,WAV文件的文件名是保存的时刻
if len(save_buffer) > 0:
self.filename = datetime.now().strftime("%Y-%m-%d_%H_%M_%S") + ".wav"
self.__Save_wave_file(self.filename, save_buffer)
save_buffer = []
print self.filename, "saved"
break
except KeyboardInterrupt:
print "manual exit"
finally:
# stop stream
stream.stop_stream()
stream.close()
# close PyAudio
pa.terminate()
print "exit recording"
return self.filename