def startPlaying(self, frequency=440.0, amplitude=0.5, framerate=48000, duration=60, bufsize=1024):

        if self.isWindows:
            while True:
                try:
                    import winsound
                    winsound.Beep(int(frequency), duration * 1000)
                except AttributeError:
                    pass
                if self.play == False:
                    break
        else:
            # create stream
            channels = ((wb.sine_wave(frequency, amplitude=amplitude, framerate=framerate),),)
            nframes = framerate * duration
            while self.play:
                try:
                    samples = wb.compute_samples(channels, nframes)
                    self.audioStream = MyStream(self.audioDev.open(format=self.audioDev.get_format_from_width(2), channels=1, rate=framerate, output=True))
                    wb.write_wavefile(self.audioStream, samples, nframes=nframes, sampwidth=2, framerate=framerate, bufsize=bufsize)
                except AttributeError:
                    pass
            else:
                self.audioStream.stopIt()
                self.audioDev.terminate()
            return
示例#2
0
def play_pitch(pitch, ms):
    frequency = pitch_to_frequency(pitch)

    num_channels = 1
    bit_rate = 16
    sample_rate = 44100
    volume = 1
    data_encoding = "signed"

    sox_output_type = "raw"

    channels = ((wavebender.sine_wave(frequency),),)
    samples = wavebender.compute_samples(channels, (sample_rate/1000) * ms )
    wavebender.write_pcm(open(tempfile.gettempdir() + "/pitch", 'w+'), samples, framerate=sample_rate)

    subprocess.call(["play", "-e", data_encoding, "-r", str(sample_rate), "-v", str(volume), "-t", sox_output_type, "-c", str(num_channels), "-b", str(bit_rate), "-"], stdin=open(tempfile.gettempdir() + "/pitch"))
    def startPlaying(self,
                     frequency=440.0,
                     amplitude=0.5,
                     framerate=48000,
                     duration=60,
                     bufsize=1024):

        if self.isWindows:
            while True:
                try:
                    import winsound
                    winsound.Beep(int(frequency), duration * 1000)
                except AttributeError:
                    pass
                if self.play == False:
                    break
        else:
            # create stream
            channels = ((wb.sine_wave(frequency,
                                      amplitude=amplitude,
                                      framerate=framerate), ), )
            nframes = framerate * duration
            while self.play:
                try:
                    samples = wb.compute_samples(channels, nframes)
                    self.audioStream = MyStream(
                        self.audioDev.open(
                            format=self.audioDev.get_format_from_width(2),
                            channels=1,
                            rate=framerate,
                            output=True))
                    wb.write_wavefile(self.audioStream,
                                      samples,
                                      nframes=nframes,
                                      sampwidth=2,
                                      framerate=framerate,
                                      bufsize=bufsize)
                except AttributeError:
                    pass
            else:
                self.audioStream.stopIt()
                self.audioDev.terminate()
            return
示例#4
0
文件: wavegen.py 项目: EQ4/playingGod
    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
示例#5
0
def generate_wave(frequency=BEEP_FREQ, framerate=44100, amplitude=1, duration=0.03):
    return islice(sine_wave(frequency, framerate, amplitude), duration*framerate)
示例#6
0
# Written 25/6/14 by dh4gan
# This script tests the wavebender package 
# Found at https://github.com/zacharydenton/wavebender

# This is my hack of the binaural.py example file

from wavebender import sine_wave, compute_samples, write_wavefile

channels = ((sine_wave(170.0, amplitude=0.1),),
            (sine_wave(178.0, amplitude=0.1),))

samples = compute_samples(channels)
write_wavefile("test.wav", samples)