Пример #1
0
def bells():
    def makebell(freq):
        synth = WaveSynth()
        duration = 2
        divider = 2.2823535
        fm = Triangle(freq/divider, amplitude=0.5)
        s = synth.sine(freq, duration, fm_lfo=fm)
        # apply ADSR envelope that resembles bell amp curve, see http://www.hibberts.co.uk/make.htm
        s.envelope(0, duration*0.25, .5, duration*0.75)
        s.echo(2, 5, 0.06, 0.6)
        return s.make_32bit(False)
    b_l1 = makebell(key_freq(56))
    b_l2 = makebell(key_freq(60))
    b_h1 = makebell(key_freq(78)).amplify(0.7)
    b_h2 = makebell(key_freq(82)).amplify(0.7)
    b_h3 = makebell(key_freq(84)).amplify(0.7)
    bells = b_l1.mix_at(1.0, b_h1)
    bells.mix_at(1.5, b_h2)
    bells.mix_at(2, b_h3)
    bells.mix_at(3, b_l2)
    bells.mix_at(4, b_h2)
    bells.mix_at(4.5, b_h3)
    bells.mix_at(5, b_h1)
    bells.make_16bit()
    with Output.for_sample(bells) as out:
        out.play_sample(bells)
        out.wait_all_played()
Пример #2
0
def demo_song():
    synth = WaveSynth()
    notes = {note: key_freq(49+i) for i, note in enumerate(['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'])}
    tempo = 0.3

    def synth_sample(freq, duration):
        harmonics = [(1, 1), (2, 1/2), (4, 1/4), (6, 1/6)]
        a = synth.harmonics(freq, duration, harmonics)
        return a.envelope(0.05, 0.2, 0.8, 0.5)

    silence = Sample.from_array([0]*int(synth.samplerate*tempo*2), synth.samplerate, numchannels=1)
    song = "A A B. A D. C#.. ;  A A B. A E. D.. ;  A A A. F#.. D C#.. B ;  G G F#.. D E D ; ; "\
        "A A B. A D C#.. ; A A B. A E D. ; A A A. F#.. D C#.. B ; G G F#.. D E D ; ; "
    with Output(synth.samplerate, synth.samplewidth, 1, mixing="sequential", queue_size=50) as out:
        for note in song.split():
            if note == ";":
                print()
                out.play_sample(silence)
                continue
            print(note, end="  ", flush=True)
            if note.endswith(".."):
                sample = synth_sample(notes[note[:-2]], tempo*4)
            elif note.endswith("."):
                sample = synth_sample(notes[note[:-1]], tempo*2)
            else:
                sample = synth_sample(notes[note], tempo)
            out.play_sample(sample)
        print()
        out.wait_all_played()
Пример #3
0
def demo_song(profiling=False):
    synth = WaveSynth()
    notes = {note: key_freq(49+i) for i, note in enumerate(['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'])}
    tempo = 0.3

    def instrument(freq, duration):
        harmonics = [(1, 1), (2, 1/2), (4, 1/4), (6, 1/6)]
        a = synth.harmonics(freq, duration, harmonics)
        return a.envelope(0.05, 0.2, 0.8, 0.5)

    print("Synthesizing tones...")
    perf_c = time.perf_counter()
    quarter_notes = {note: instrument(notes[note], tempo) for note in notes}
    half_notes = {note: instrument(notes[note], tempo*2) for note in notes}
    full_notes = {note: instrument(notes[note], tempo*4) for note in notes}
    silence = Sample.from_array([0]*int(synth.samplerate*tempo*2), synth.samplerate, numchannels=1)
    if profiling:
        print(time.perf_counter()-perf_c)
    else:
        song = "A A B. A D. C#.. ;  A A B. A E. D.. ;  A A A. F#.. D C#.. B ;  G G F#.. D E D ; ; "\
            "A A B. A D C#.. ; A A B. A E D. ; A A A. F#.. D C#.. B ; G G F#.. D E D ; ; "
        with Output(synth.samplerate, synth.samplewidth, 1, mixing="sequential") as out:
            for note in song.split():
                if note == ";":
                    print()
                    out.play_sample(silence)
                    continue
                print(note, end="  ", flush=True)
                if note.endswith(".."):
                    sample = full_notes[note[:-2]]
                elif note.endswith("."):
                    sample = half_notes[note[:-1]]
                else:
                    sample = quarter_notes[note]
                out.play_sample(sample)
            print()
            out.wait_all_played()
Пример #4
0
import itertools
import math
from collections import OrderedDict
from synthplayer.sample import Sample
from synthplayer.synth import WaveSynth, key_freq, octave_notes, major_chord_keys
from synthplayer.synth import Sine, Triangle, Pulse, Square, SquareH, Sawtooth, SawtoothH, WhiteNoise, Linear, Harmonics
from synthplayer.synth import FastSine, FastPulse, FastSawtooth, FastSquare, FastTriangle
from synthplayer.synth import EchoFilter, EnvelopeFilter, AbsFilter, ClipFilter, DelayFilter
from synthplayer.playback import Output
from synthplayer import params


# some note frequencies for octaves 1 to 7
notes = [
    None,
    OrderedDict((note, key_freq(4+i)) for i, note in enumerate(octave_notes)),
    OrderedDict((note, key_freq(16+i)) for i, note in enumerate(octave_notes)),
    OrderedDict((note, key_freq(28+i)) for i, note in enumerate(octave_notes)),
    OrderedDict((note, key_freq(40+i)) for i, note in enumerate(octave_notes)),
    OrderedDict((note, key_freq(52+i)) for i, note in enumerate(octave_notes)),
    OrderedDict((note, key_freq(64+i)) for i, note in enumerate(octave_notes)),
    OrderedDict((note, key_freq(76+i)) for i, note in enumerate(octave_notes))
]


def demo_tones():
    synth = WaveSynth()
    with Output(nchannels=1, mixing="sequential", queue_size=2) as out:
        for wave in [synth.square_h, synth.square, synth.sine, synth.triangle, synth.sawtooth, synth.sawtooth_h]:
            print(wave.__name__)
            for note, freq in list(notes[4].items())[6:]: