def play_console(filename_or_stream): with wave.open(filename_or_stream, 'r') as wav: samplewidth = wav.getsampwidth() samplerate = wav.getframerate() nchannels = wav.getnchannels() bar_width = 60 update_rate = 20 # lower this if you hear the sound crackle! levelmeter = LevelMeter(rms_mode=False, lowest=-50.0) with Output(samplerate, samplewidth, nchannels, int(update_rate / 4)) as out: while True: frames = wav.readframes(samplerate // update_rate) if not frames: break sample = Sample.from_raw_frames(frames, wav.getsampwidth(), wav.getframerate(), wav.getnchannels()) out.play_sample(sample, async=True) levelmeter.update(sample) time.sleep( sample.duration * 0.4 ) # print the peak meter more or less halfway during the sample levelmeter.print(bar_width) print("\ndone") input("Enter to exit:")
def update(self, *args, **kwargs): frames = self.wave.readframes(self.samplerate//self.update_rate) if not frames: self.pbvar_left.set(0) self.pbvar_right.set(0) print("done!") return sample = Sample.from_raw_frames(frames, self.samplewidth, self.samplerate, self.nchannels) self.audio_out.play_sample(sample, async=True) time.sleep(sample.duration/3) # print the peak meter more or less halfway during the sample left, peak_l, right, peak_r = self.levelmeter.update(sample) self.pbvar_left.set(left-self.lowest_level) self.pbvar_right.set(right-self.lowest_level) if left > -3: self.pb_left.configure(style="red.Vertical.TProgressbar") elif left > -6: self.pb_left.configure(style="yellow.Vertical.TProgressbar") else: self.pb_left.configure(style="green.Vertical.TProgressbar") if right > -3: self.pb_right.configure(style="red.Vertical.TProgressbar") elif right > -6: self.pb_right.configure(style="yellow.Vertical.TProgressbar") else: self.pb_right.configure(style="green.Vertical.TProgressbar") self.after(self.update_rate, self.update)
def update(self, *args, **kwargs): frames = self.wave.readframes(self.samplerate // self.update_rate) if not frames: self.pbvar_left.set(0) self.pbvar_right.set(0) print("done!") return sample = Sample.from_raw_frames(frames, self.samplewidth, self.samplerate, self.nchannels) self.audio_out.play_sample(sample, async=True) time.sleep( sample.duration / 3) # print the peak meter more or less halfway during the sample left, peak_l, right, peak_r = self.levelmeter.update(sample) self.pbvar_left.set(left - self.lowest_level) self.pbvar_right.set(right - self.lowest_level) if left > -3: self.pb_left.configure(style="red.Vertical.TProgressbar") elif left > -6: self.pb_left.configure(style="yellow.Vertical.TProgressbar") else: self.pb_left.configure(style="green.Vertical.TProgressbar") if right > -3: self.pb_right.configure(style="red.Vertical.TProgressbar") elif right > -6: self.pb_right.configure(style="yellow.Vertical.TProgressbar") else: self.pb_right.configure(style="green.Vertical.TProgressbar") self.after(self.update_rate, self.update)
def update(self, *args, **kwargs): frames = self.wave.readframes(self.samplerate//self.update_rate) if not frames: self.pbvar_left.set(0) self.pbvar_right.set(0) print("done!") return sample = Sample.from_raw_frames(frames, self.samplewidth, self.samplerate, self.nchannels) self.audio_out.play_sample(sample) left, peak_l = self.levelmeter.level_left, self.levelmeter.peak_left right, peak_r = self.levelmeter.level_right, self.levelmeter.peak_right self.pbvar_left.set(left-self.lowest_level) self.pbvar_right.set(right-self.lowest_level) if left > -3: self.pb_left.configure(style="red.Vertical.TProgressbar") elif left > -6: self.pb_left.configure(style="yellow.Vertical.TProgressbar") else: self.pb_left.configure(style="green.Vertical.TProgressbar") if right > -3: self.pb_right.configure(style="red.Vertical.TProgressbar") elif right > -6: self.pb_right.configure(style="yellow.Vertical.TProgressbar") else: self.pb_right.configure(style="green.Vertical.TProgressbar") self.after(self.update_rate, self.update)
def __next__(self): frames = self.source.readframes(self.buffer_size) for filter in self.frames_filters: frames = filter(frames) if not frames: return None sample = Sample.from_raw_frames(frames, self.samplewidth, self.samplerate, self.nchannels) for filter in self.filters: sample = filter(sample) return sample
def __next__(self): frames = self.source.readframes(self.buffer_size) for filter in self.frames_filters: frames = filter(frames) if not frames: return None sample = Sample.from_raw_frames(frames, self.samplewidth, self.samplerate, self.nchannels) for filter in self.filters: sample = filter(sample) return sample
def __iter__(self): """ Yields tuple(timestamp, Sample) that represent the mixed audio streams. """ while True: mixed_sample = Sample.from_raw_frames(b"", self.samplewidth, self.samplerate, self.nchannels) for sample_stream in self.sample_streams: try: sample = next(sample_stream) except (os.error, ValueError): # Problem reading from stream. Assume stream closed. sample = None if sample: mixed_sample.mix(sample) else: self.remove_stream(sample_stream) yield self.timestamp, mixed_sample self.timestamp += mixed_sample.duration
def play_console(filename_or_stream): with wave.open(filename_or_stream, 'r') as wav: samplewidth = wav.getsampwidth() samplerate = wav.getframerate() nchannels = wav.getnchannels() bar_width = 60 update_rate = 20 # lower this if you hear the sound crackle! levelmeter = LevelMeter(rms_mode=False, lowest=-50.0) with Output(samplerate, samplewidth, nchannels, int(update_rate/4)) as out: while True: frames = wav.readframes(samplerate//update_rate) if not frames: break sample = Sample.from_raw_frames(frames, wav.getsampwidth(), wav.getframerate(), wav.getnchannels()) out.play_sample(sample, async=True) levelmeter.update(sample) time.sleep(sample.duration*0.4) # print the peak meter more or less halfway during the sample levelmeter.print(bar_width) print("\ndone") input("Enter to exit:")
def __iter__(self): """ Yields tuple(timestamp, Sample) that represent the mixed audio streams. """ while True: mixed_sample = Sample.from_raw_frames(b"", self.samplewidth, self.samplerate, self.nchannels) for sample_stream in self.sample_streams: try: sample = next(sample_stream) except (os.error, ValueError): # Problem reading from stream. Assume stream closed. sample = None if sample: mixed_sample.mix(sample) else: self.remove_stream(sample_stream) yield self.timestamp, mixed_sample self.timestamp += mixed_sample.duration
def play_console(filename_or_stream): with wave.open(filename_or_stream, 'r') as wav: samplewidth = wav.getsampwidth() samplerate = wav.getframerate() nchannels = wav.getnchannels() bar_width = 60 update_rate = 20 # lower this if you hear the sound crackle! levelmeter = LevelMeter(rms_mode=False, lowest=-50.0) with Output(samplerate, samplewidth, nchannels) as out: print("Audio API used:", out.audio_api) if not out.supports_streaming: raise RuntimeError("need api that supports streaming") out.register_notify_played(levelmeter.update) while True: frames = wav.readframes(samplerate//update_rate) if not frames: break sample = Sample.from_raw_frames(frames, wav.getsampwidth(), wav.getframerate(), wav.getnchannels()) out.play_sample(sample) levelmeter.print(bar_width) print("\ndone") input("Enter to exit:")