Created
December 10, 2015 22:01
-
-
Save peace098beat/e00e30e10cb6b8ff753c to your computer and use it in GitHub Desktop.
PyAudioのVolumeをコントロール
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numpy import frombuffer, dtype, asarray, iinfo | |
def byteToPCM(data, sample_width): | |
d_type = 'float' | |
if sample_width == 2: | |
d_type = 'short' | |
return frombuffer(data, dtype=d_type) | |
def pcmToFloat(sig, type='float32'): | |
sig = asarray(sig) | |
if sig.dtype.kind != 'i': | |
raise TypeError('signal must be integer') | |
type = dtype(type) | |
if type.kind != 'f': | |
raise TypeError('type must be float') | |
return sig.astype(type) / type.type(-iinfo(sig.dtype).min) | |
def floatToPCM(sig, dtype='int16'): | |
return (sig * iinfo(dtype).max).astype(dtype) | |
def dd(self): | |
wf = self.wf | |
frate = wf.getframerate() | |
sampw = wf.getsampwidth() | |
nchan = wf.getnchannels() | |
def callback(in_data, frame_count, time_info, status): | |
data = wf.readframes(frame_count) | |
if type(data) == type(''): | |
data = str.encode(data) | |
# if len(data) < frame_count * sampw * nchan: | |
# if self.loop_box.isChecked(): | |
# wf.rewind() | |
# data = b''.join([data, | |
# wf.readframes(frame_count - int(len(data) / (sampw * nchan)))]) | |
# self.chain.reset() | |
# elif len(data) == 0: | |
# return data, pyaudio.paComplete | |
filtered = self.chain.filter(pcmToFloat(byteToPCM(data, sampw))) | |
# self.plotwin.updateSpectrum(np.fft.rfft(filtered)) | |
return bytes(floatToPCM(filtered)), pyaudio.paContinue | |
chunk_size = np.int(frate / self.plotwin.refresh_rate) | |
self.stream = pya.open(format=pya.get_format_from_width(wf.getsampwidth()), | |
channels=wf.getnchannels(), | |
rate=frate, | |
frames_per_buffer=chunk_size, | |
output=True, | |
stream_callback=callback) | |
self.chain.reset() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment