Exemplo n.º 1
0
def play():
    class Play(Program):
        def __init__(self, commands: List[Command]):
            super().__init__(commands)
            self.sounds = []

        def snd(self, x):
            self.sounds.append(res := super().snd(x))
            return res

    p = Play(parse_data())
    p.run()
    print(p.sounds)

    import simpleaudio as sa
    from tones import SAWTOOTH_WAVE, SINE_WAVE, SQUARE_WAVE, TRIANGLE_WAVE
    from tones.mixer import Mixer
    mixer = Mixer(amplitude=.3)
    name = 'track'
    mixer.create_track(name, SAWTOOTH_WAVE, vibrato_frequency=None, vibrato_variance=3)
    for s in p.sounds:
        mixer.add_tone(name, s / 3, duration=.1)

    bio = BytesIO()
    mixer.write_wav(bio)
    bio.seek(0)

    wo = sa.WaveObject.from_wave_file(bio)
    wo.play().wait_done()
Exemplo n.º 2
0
def _generate_sample_data(parsed, amplitude, wavetype):
    if wavetype not in [tones.SINE_WAVE, tones.SQUARE_WAVE]:
        raise ValueError("Invalid wave type '%s'" % wavetype)

    mixer = Mixer(SAMPLE_RATE, amplitude)
    numchannels = 0

    for i in range(len(parsed)):
        mixer.create_track(i, wavetype=wavetype, attack=0.01, decay=0.01)

    for i in range(len(parsed)):
        for pitch, time in parsed[i]:
            if pitch <= 0.0:
                mixer.add_silence(i, duration=time)
            else:
                mixer.add_tone(i, frequency=pitch, duration=time)

    return mixer.sample_data()