def construct(self): """Given a Wave object, produces a soundwave which can be written to file. Global vars: possible_shapes: List of strings used to keep track of what kinds of waves can be constructed in a way which can be queried by other modules. Internal vars: waveform: The audible protion of the wave. """ if self.shape == "sine": waveform = wb.sine_wave(self.frequency, self.sample_rate, self.amplitude) if self.shape == "square": waveform = wb.square_wave(self.frequency, self.sample_rate, self.amplitude) if self.shape == "damped": waveform = wb.damped_wave(self.frequency, self.sample_rate, self.amplitude) if self.shape == "white_noise": waveform = wb.white_noise(self.amplitude) i = -1 # A counter to keep position in the waveform. before_postwait = self.total_ticks - self.postwait while True: i += 1 cursor = i % self.total_ticks # Cursor here represents the position in the waveform if cursor <= self.prewait: yield 0 elif self.prewait < cursor <= before_postwait: yield waveform.next() else: yield 0
def sound_write(self, frequency=440.0, duration=10.0, amplitude=0.1): square_wave = wavebender.square_wave(frequency, amplitude=amplitude); channels = ((square_wave,),) samples = wavebender.compute_samples(channels, 44100 * duration * 1) self._samples_write(samples, duration);
#!/usr/bin/env python import wavebender from math import cos import sys f =open ("test.wav", 'wb'); square_wave = wavebender.square_wave(440.0, amplitude=0.1) channels = ((square_wave,),) duration = 2 samples = wavebender.compute_samples(channels, 44100 * duration * 1) wavebender.write_wavefile(f, samples, 44100 * duration * 1, nchannels=1)