Esempio n. 1
0
def main():
    player = Player()
    player.open_stream()

    print("play sine wave")
    synthesizer = Synthesizer(osc1_waveform=Waveform.sine, osc1_volume=1.0, use_osc2=False)
    player.play_wave(synthesizer.generate_constant_wave(440.0, 3.0))
    time.sleep(0.5)

    print("play square wave")
    synthesizer = Synthesizer(osc1_waveform=Waveform.square, osc1_volume=0.8, use_osc2=False)
    player.play_wave(synthesizer.generate_constant_wave(440.0, 3.0))
    time.sleep(0.5)

    print("play triangle wave")
    synthesizer = Synthesizer(osc1_waveform=Waveform.triangle, osc1_volume=0.8, use_osc2=False)
    player.play_wave(synthesizer.generate_constant_wave(440.0, 3.0))
    time.sleep(0.5)

    print("play synthesized wave 1")
    synthesizer = Synthesizer(
        osc1_waveform=Waveform.sawtooth, osc1_volume=1.0,
        use_osc2=True, osc2_waveform=Waveform.sawtooth,
        osc2_volume=0.3, osc2_freq_transpose=6.0,
    )
    player.play_wave(synthesizer.generate_constant_wave(440.0, 3.0))
    time.sleep(0.5)

    print("play synthesized wave 2")
    synthesizer = Synthesizer(
        osc1_waveform=Waveform.square, osc1_volume=1.0,
        use_osc2=True, osc2_waveform=Waveform.sine,
        osc2_volume=0.3, osc2_freq_transpose=3.0,
    )
    player.play_wave(synthesizer.generate_constant_wave(440.0, 3.0))
Esempio n. 2
0
def play(chord):
    player = Player()
    player.open_stream()
    synthesizer = Synthesizer(osc1_waveform=Waveform.sine,
                              osc1_volume=0.4,
                              use_osc2=False)
    player.play_wave(synthesizer.generate_chord(chord, 1))
Esempio n. 3
0
def playVoice(notes):
    player = Player()
    player.open_stream()
    synthesizer = Synthesizer(osc1_waveform=Waveform.triangle, osc1_volume=1.0, use_osc2=False)

    for note in notes:
        player.play_wave(synthesizer.generate_constant_wave(synth(note), .5))
Esempio n. 4
0
 def worker():
     player = Player()
     player.open_stream()
     synthesizer = Synthesizer(osc1_waveform=Waveform.sine,
                               osc1_volume=0.7,
                               use_osc2=False)
     return player.play_wave(
         synthesizer.generate_constant_wave(areatone, 0.14))
Esempio n. 5
0
def play_beat(t):
    player = Player()
    player.open_stream()
    synthesizer = Synthesizer(osc1_waveform=Waveform.sawtooth,
                              osc1_volume=1.0,
                              use_osc2=False)
    for i in range(int(t / RATE)):
        player.play_wave(synthesizer.generate_chord(['A3'], 0.1))
        time.sleep(float(RATE - 0.1))
Esempio n. 6
0
def play_progression(progression):
    player = Player()
    player.open_stream()
    synthesizer = Synthesizer(osc1_waveform=Waveform.sine,
                              osc1_volume=10.0,
                              use_osc2=False)
    for measure in progression:
        for chord in measure:
            player.play_wave(
                synthesizer.generate_chord(chord[0], abs(chord[1])))
Esempio n. 7
0
def consumer(queue):
    player = Player()
    player.open_stream()

    while True:
        tone = queue.get()

        if tone is None:
            break

        player.play_wave(tone)
Esempio n. 8
0
def main():
    player = Player()
    player.open_stream()

    print("play major chord")
    synthesizer = Synthesizer(osc1_waveform=Waveform.sine,
                              osc1_volume=1.0,
                              use_osc2=False)
    chord = [BASE, BASE * 2.0**(4 / 12.0), BASE * 2.0**(7 / 12.0)]
    player.play_wave(synthesizer.generate_chord(chord, 3.0))
    time.sleep(0.5)

    print("play minor chord")
    chord = [BASE, BASE * 2.0**(3 / 12.0), BASE * 2.0**(7 / 12.0)]
    player.play_wave(synthesizer.generate_chord(chord, 3.0))
    time.sleep(0.5)

    print("play sus4 chord")
    chord = [BASE, BASE * 2.0**(5 / 12.0), BASE * 2.0**(7 / 12.0)]
    player.play_wave(synthesizer.generate_chord(chord, 3.0))
    time.sleep(0.5)

    print("play 7th chord")
    chord = [
        BASE, BASE * 2.0**(4 / 12.0), BASE * 2.0**(7 / 12.0),
        BASE * 2.0**(10 / 12.0)
    ]
    player.play_wave(synthesizer.generate_chord(chord, 3.0))
    time.sleep(0.5)

    print("play add9 chord")
    chord = [
        BASE, BASE * 2.0**(4 / 12.0), BASE * 2.0**(7 / 12.0),
        BASE * 2.0**(14 / 12.0)
    ]
    player.play_wave(synthesizer.generate_chord(chord, 3.0))
    time.sleep(0.5)

    print("play chord sequence")
    chord = [
        BASE * 2.0**(2 / 12.0), BASE * 2.0**(5 / 12.0), BASE * 2.0**(9 / 12.0),
        BASE * 2.0**(12 / 12.0)
    ]
    player.play_wave(synthesizer.generate_chord(chord, 1.0))
    chord = [
        BASE * 2.0**(2 / 12.0), BASE * 2.0**(7 / 12.0), BASE * 2.0**(11 / 12.0)
    ]
    player.play_wave(synthesizer.generate_chord(chord, 1.0))
    chord = [
        BASE, BASE * 2.0**(4 / 12.0), BASE * 2.0**(7 / 12.0),
        BASE * 2.0**(12 / 12.0)
    ]
    player.play_wave(synthesizer.generate_chord(chord, 1.0))
Esempio n. 9
0
def main():
    args = parse_args()
    chords = ChordProgression(args.chord)

    player = Player()
    player.open_stream()
    synthesizer = Synthesizer(osc1_waveform=Waveform.triangle,
                              osc1_volume=1.0,
                              use_osc2=False)

    for chord in chords:
        notes = chord.components_with_pitch(root_pitch=3)
        print("Play {}".format(chord))
        player.play_wave(synthesizer.generate_chord(notes, 1.0))
Esempio n. 10
0
def play_progression(chords, timings):
    player = Player()
    player.open_stream()
    synthesizer = Synthesizer(osc1_waveform=Waveform.sine,
                              osc1_volume=1.0,
                              use_osc2=True,
                              osc2_waveform=Waveform.sawtooth,
                              osc2_freq_transpose=2.0)
    #play all notes :)
    for (i, _), duration in zip(chords, timings):
        if i == 'rest':
            time.sleep(RATE / 8)
            print('rest')
        else:
            print('chord : {} timing : {}'.format(i, duration * RATE))
            simalt_notesplay(player, i, duration, synthesizer)
def main(protein_sequence):
    """Plays musical protein sequence."""

    # Create dictionary that will map each amino acid to the various musical properties
    association = {}

    # Generate association between amino acid and musical properties
    association.update(get_association(protein_sequence, hydrophobic_aa, hydrophobic_chords, 'square'))
    association.update(get_association(protein_sequence, less_hydrophobic_aa, less_hydrophobic_notes, 'sine'))
    association.update(get_association(protein_sequence, non_hydrophobic_aa, non_hydrophobic_chords, 'sawtooth'))

    # Create Player and Synthesizer objects to be used when playing protein sequence
    player = Player()
    player.open_stream()
    synthesizer = {
        'polar': {
            'sine': Synthesizer(osc1_waveform=Waveform.sine, osc1_volume=1.0, use_osc2=False),
            'sawtooth': Synthesizer(osc1_waveform=Waveform.sawtooth, osc1_volume=1.0, use_osc2=False),
            'square': Synthesizer(osc1_waveform=Waveform.sawtooth, osc1_volume=1.0, use_osc2=False)
        },
        'nonpolar': {
            'sine': Synthesizer(osc1_waveform=Waveform.sine, osc1_volume=0.5, use_osc2=False),
            'sawtooth': Synthesizer(osc1_waveform=Waveform.sawtooth, osc1_volume=0.5, use_osc2=False),
            'square': Synthesizer(osc1_waveform=Waveform.sawtooth, osc1_volume=0.5, use_osc2=False)
        },
    }

    # writer = Writer()
    # sounds = []

    # Loop through and play out each amino acid in the protein sequence
    for idx, aa in enumerate(protein_sequence):
        if aa not in association:
            continue

        note, instrument, volume, length = association[aa]

        length = length if idx != len(protein_sequence) - 1 else 0.5  # Set length of last note to be 0.5s

        if type(note) == list:  # Play chord
            sound = synthesizer[volume][instrument].generate_chord([notes[n] for n in note], length)
            print_notes(note, aa)
        else:  # Play single note
            sound = synthesizer[volume][instrument].generate_constant_wave(notes[note], length)
            print_notes([note], aa)

        player.play_wave(sound)
Esempio n. 12
0
 def eeg_callback(self, path, args):
     lE, lF, rF, rE = args
     print "%s %f %f %f %f" % (path, lE, lF, rF, rE)
     player = Player()
     player.open_stream()
     synthesizer = Synthesizer(osc1_waveform=Waveform.sine,
                               osc1_volume=1.0,
                               use_osc2=False)
     number = ra.uniform(0.2, 1.0)
     player.play_wave(
         synthesizer.generate_constant_wave((lF - 1200) * ((600 - 200) /
                                                           (1200 - 750)) +
                                            200), number)
     player.play_wave(
         synthesizer.generate_constant_wave((rF - 1200) * ((600 - 200) /
                                                           (1200 - 750)) +
                                            200), number)
Esempio n. 13
0
def playPiece(lines: List[TemporalisedLine]):
    mapping, indexLines = asUniqueValues(lines)

    lowerLines = indexLines[0:-1]
    upperLine = indexLines[-1]

    sm = makeSimMap(lowerLines, upperLine)

    player = Player()
    player.open_stream()
    synthesizer = Synthesizer(osc1_waveform=Waveform.triangle, osc1_volume=1.0, use_osc2=False)

    for k in upperLine[0][:-1]:
        toPlay = [synth(mapping[n]) for n in sm[k].union([k])]
        player.play_wave(synthesizer.generate_chord([n for n in toPlay], 1 / len(lines)))

    # Play last note longer
    k = upperLine[0][-1]
    toPlay = [synth(mapping[n]) for n in sm[k].union([k])]
    player.play_wave(synthesizer.generate_chord([n for n in toPlay], 2 / len(lines)))
Esempio n. 14
0
def main():
    player = Player()
    player.open_stream()

    print("play major chord")
    synthesizer = Synthesizer(osc1_waveform=Waveform.sine,
                              osc1_volume=1.0,
                              use_osc2=False)
    chord = ["C4", "E4", "G4"]
    player.play_wave(synthesizer.generate_chord(chord, 3.0))
    time.sleep(0.5)

    print("play minor chord")
    chord = ["C4", "Eb4", "G4"]
    player.play_wave(synthesizer.generate_chord(chord, 3.0))
    time.sleep(0.5)

    print("play sus4 chord")
    chord = ["C4", "F4", "G4"]
    player.play_wave(synthesizer.generate_chord(chord, 3.0))
    time.sleep(0.5)

    print("play 7th chord")
    chord = ["C4", "E4", "G4", "Bb4"]
    player.play_wave(synthesizer.generate_chord(chord, 3.0))
    time.sleep(0.5)

    print("play add9 chord")
    chord = ["C4", "E4", "G4", "D5"]
    player.play_wave(synthesizer.generate_chord(chord, 3.0))
    time.sleep(0.5)

    print("play chord sequence")
    chord = ["D4", "F4", "A4", "C5"]
    player.play_wave(synthesizer.generate_chord(chord, 1.0))
    chord = ["D4", "G4", "B4"]
    player.play_wave(synthesizer.generate_chord(chord, 1.0))
    chord = ["E4", "G4", "C5"]
    player.play_wave(synthesizer.generate_chord(chord, 1.0))
Esempio n. 15
0
class AudioDebug:
    def __init__(self, pre=["C3", "E3", "G3"], post=["C3", "D3", "F3"]):
        self.pre = pre
        self.post = post
        self.player = Player()
        self.player.open_stream()
        self.synthesizer = Synthesizer(osc1_waveform=Waveform.sine,
                                       osc1_volume=1.0,
                                       use_osc2=False)

    def play_chord(self, chord):
        self.player.play_wave(self.synthesizer.generate_chord(chord, 0.5))

    def __call__(self, func):
        def logic(*args, **kwargs):
            print('pre')
            self.play_chord(self.pre)
            result = func(*args, **kwargs)
            print('post')
            self.play_chord(self.post)
            return result

        return logic
Esempio n. 16
0
wait_time = 0
measures = 0
beats = 4
drumlist = []
notelist = []
counter = 0
seqloc = [1, 1, 1]
drum_ready = 0
synth_ready = 0
notes = ['a', 'ais', 'b', 'c', 'cis', 'd', 'dis', 'e', 'f', 'fis', 'g', 'gis']
hertz = 0


# Initialise synth
player = Player()
player.open_stream()
synth = Synthesizer(osc1_waveform=Waveform.triangle, osc1_volume=0.2, osc2_freq_transpose=0.48, use_osc2=True, osc2_waveform=Waveform.triangle, osc2_volume=0.1)

# Define midi variables
degrees = []  # MIDI note number for drums
degreesn = [] #MIDI note number for notes
track = 0
trackn = 1
channel = 0
time = [0]  # In beats
timen = [0]
duration = 1  # In beats
tempo = 60  # In BPM
volume = 100  # 0-127, as per the MIDI standard

# Script to clear screen
class MelodyPlayer(object):
    """
    A class used to play the melody generated by Melody

    Attributes
    ----------
    player : Player
        an object that plays the melody
    synthesizer : Synthesizer
        an object that characterizes the sound of the melody
    """
    def __init__(self, oscillator):
        """
        Instantiate the synthesizer and the player

        Parameters
        ----------
        oscillator : str
            defines the shape of the Waveform
        """

        self.player = Player()

        self.synthesizer = Synthesizer(osc1_waveform=WAVEFORM[oscillator])

    def play_melody(self, melody, bpm):
        """
        Plays the melody in a certain bpm

        Parameters
        ----------
        melody : Melody
            the melody that will be played
        bpm : int
            beats per minute that determine the speed the melody is played
        """

        self.player.open_stream()

        for note in melody.notes:
            wave_sound = self.generate_waves(note, bpm)
            self.player.play_wave(wave_sound)

    def save_melody(self, melody, bpm):
        """
        Saves the melody as WAV

        Saves each note individually and then concatenate then
        in a sigle WAV file

        Parameters
        ----------
        melody : Melody
            the melody that will be played
        bpm : int
            beats per minute that of the melody
        """

        writer = Writer()
        outfile = "melody.wav"
        next_note = "note.wav"
        data = []

        for note in melody.notes:
            sound = self.generate_waves(note, bpm)

            if note == melody.notes[0]:
                # Generates the first note
                writer.write_wave(outfile, sound)
                continue

            writer.write_wave(next_note, sound)

            infiles = [outfile, next_note]

            for infile in infiles:
                with wave.open(infile, 'rb') as w:
                    data.append([w.getparams(), w.readframes(w.getnframes())])

            self.append_note(outfile, data)

            # Deletes the note file
            os.remove(next_note)

    @staticmethod
    def append_note(outfile, data):
        """
        Auxiliary method for save_melody

        Code found on https://bit.ly/2EoFjIU

        Parameters
        ----------
        outfile : str
            path to output file
        data : list
            an array of WAV parameters
        """

        with wave.open(outfile, 'wb') as output:
            output.setparams(data[0][0])
            output.writeframes(data[0][1])
            output.writeframes(data[1][1])

        data.clear()

    def generate_waves(self, note, bpm):
        """
        Generate the wave that represents a note

        Parameters
        ---------
        note : MusicalNote
            The note that will be turned into wave
        bpm : int
            beats per minute that determines the lenght

        Returns
        -------
        ndarray
            an array that represents the normalized wave
        """

        lenght = note.lenght["duration"] / 16 * 60 / bpm

        # If the note is not played, the frequency is 0
        frequency = note.note["frequency"] if note.is_played else 0

        return self.synthesizer.generate_constant_wave(frequency, lenght)
Esempio n. 18
0
from synthesizer import Player, Synthesizer, Waveform

p = Player()
p.open_stream()

s = Synthesizer(osc1_waveform=Waveform.sine, osc1_volume=1.0, use_osc2=False)

i = 0
while (i < 3):
    A = s.generate_constant_wave(88.0, 1.0)
    p.play_wave(A)
    i += 1
Esempio n. 19
0
def digitalChime(number_of_notes):
    player = Player()
    synthesizer = Synthesizer(osc1_waveform=Waveform.sawtooth,
                              osc1_volume=0.1,
                              use_osc2=False)
    player.open_stream()

    #find base note
    base = random.randint(1, 6)
    time = random.randint(1, 4) * 0.25

    octive = 1
    sig = 3

    #play this many notes
    for i in range(0, number_of_notes):
        next = random.randint(1, 6)

        print('|  ', end='')

        if i % sig == 0:
            base = random.randint(1, 6)
            if base == 1:
                base = Notes['C4']
                print('Cv ', end='♪ ')
            elif base == 2:
                base = Notes['D4']
                print('Dv ', end='♪ ')
            elif base == 3:
                base = Notes['F4']
                print('Fv ', end='♪ ')
            elif base == 4:
                base = Notes['G4']
                print('Gv ', end='♪ ')
            elif base == 5:
                base = Notes['A4']
                print('Av ', end='♪ ')
            else:
                base = Notes['C5']
                print('C  ', end='♪ ')
            base = (base / 2) * octive
        else:
            print('"  ', end='♫ ')

        if next == 1:
            next = Notes['C4']
            print('C  ', end='')
        elif next == 2:
            next = Notes['D4']
            print('D  ', end='')
        elif next == 3:
            next = Notes['F4']
            print('F  ', end='')
        elif next == 4:
            next = Notes['G4']
            print('G  ', end='')
        elif next == 5:
            next = Notes['A4']
            print('A  ', end='')
        else:
            next = Notes['C5']
            print('C^ ', end='')

        if i % (int)(8 / (time)) == 0:
            print(' | ', end='♫ ')
            sig = random.randint(2, 8)
            time = random.randint(2, 10) * 0.125
            octive = random.randint(0, 2)
            if octive == 0:
                octive = 0.5
            elif octive < 0:
                octive = 0.25
            print(octive * 4, end=' ♪ ')
            print(time, end=' ♪\n')
        else:
            print(' |')

        next *= octive
        chord = [next, base]

        #print(octive)
        #print(time)

        player.play_wave(synthesizer.generate_chord(chord, time))
        # player.play_wave(synthesizer.generate_constant_wave(next,0.5))

    player.play_wave(synthesizer.generate_chord(chord, time * sig - 1))
Esempio n. 20
0
def play():
    ####################
    player = Player()
    player.open_stream()
    synthesizer = Synthesizer(osc1_waveform=Waveform.sine, osc1_volume=1.0, use_osc2=False)
    #####################
    chordOrNot = [0,0,0,1]

    #SCALING_FACTOR = 7.0/255 #TODO
    durations = (0.25,0.5)#,0.5,0.75,1.0)
    # TODO: Gaussian

    # Part 0: Chord dictionary
    freq_update = {}
    freq = {'A0': 27.5, 'A#0': 29.14, 'B0': 30.87, 'C1': 32.7, 'C#1': 34.65, 'D1': 36.71, 'D#1': 38.89, 'E1': 41.2, 'F1': 43.65, 'F#1': 46.25, 'G1': 49.0, 'G#1': 51.91, 'A1': 55.0, 'A#1': 58.27, 'B1': 61.74, 'C2': 65.41, 'C#2': 69.3, 'D2': 73.42, 'D#2': 77.78, 'E2': 82.41, 'F2': 87.31, 'F#2': 92.5, 'G2': 98.0, 'G#2': 103.83, 'A2': 110.0, 'A#2': 116.54, 'B2': 123.47, 'C3': 130.81, 'C#3': 138.59, 'D3': 146.83, 'D#3': 155.56, 'E3': 164.81, 'F3': 174.61, 'F#3': 185.0, 'G3': 196.0, 'G#3': 207.65, 'A3': 220.0, 'A#3': 233.08, 'B3': 246.94, 'C4': 261.63, 'C#4': 277.18, 'D4': 293.66, 'D#4': 311.13, 'E4': 329.63, 'F4': 349.23, 'F#4': 369.99, 'G4': 392.0, 'G#4': 415.3, 'A4': 440.0, 'A#4': 466.16, 'B4': 493.88, 'C5': 523.25, 'C#5': 554.37, 'D5': 587.33, 'D#5': 622.25, 'E5': 659.26, 'F5': 698.46, 'F#5': 739.99, 'G5': 783.99, 'G#5': 830.61, 'A5': 880.0, 'A#5': 932.33, 'B5': 987.77, 'C6': 1046.5, 'C#6': 1108.73, 'D6': 1174.66, 'D#6': 1244.51, 'E6': 1318.51, 'F6': 1396.91, 'F#6': 1479.98, 'G6': 1567.98, 'G#6': 1661.22, 'A6': 1760.0, 'A#6': 1864.66, 'B6': 1975.53, 'C7': 2093.0, 'C#7': 2217.46, 'D7': 2349.32, 'D#7': 2489.02, 'E7': 2637.02, 'F7': 2793.83, 'F#7': 2959.96, 'G7': 3135.96, 'G#7': 3322.44, 'A7': 3520.0, 'A#7': 3729.31, 'B7': 3951.07, 'C8': 4186.01}
    for k,v in freq.items():
        note = k[:-1]
        octave = int(k[-1])
        freq = v
        if octave == 4:
            freq_update[note] = freq
    freq = freq_update

    # Part 1: Choose a scale. Extract the notes and chords from that scale.
    #all_possible_scales = list(Scale.all('major'))
    m = 'major'
    all_possible_scales = [Scale('C4',m), Scale('A4',m), Scale('F4',m), Scale('G4',m)]
    choice_of_scale = random.choice(all_possible_scales)
    notes = [choice_of_scale[i] for i in range(len(choice_of_scale))]

    # Part 2: Choose a permutation of chords and notes from the list.

    ## Once it is over, pick a new random permutation and keep going unless stopped.

    # Part 3: Go through the image and based on pixed values, play the permutation.
    # Part 3 -->

    image = cv2.imread('images/nature.jpg', 0)

    #image = str(request.get('img'))

    image = skimage.measure.block_reduce(image, (150,150), np.mean)
    image = image.flatten()
    # pooling stuff happens here

    image = np.random.permutation(image)


    for px in image: #px is the pixel value
        if px == 255:
            px = px-1
        isChord = random.choice(chordOrNot)
        note = math.trunc(px*len(notes)/255.0)
        duration = random.choice(durations)
        if note >= len(notes):
            continue
        note = str(notes[note])

        if note not in freq:
            flatOrSharp = note[-1]
            if flatOrSharp == '#':
                note = chr(ord(note[0])+1)
            else:
                note = chr(ord(note[0])-1)
        
        if note not in freq:
            continue
        fr = freq[note]
        if(isChord):
            # play a chord
            notes_in_chord = Chord(Note(note), 'M').notes
            freq_list = []
            for n in notes_in_chord:
                a = str(n)
                if a not in freq:
                    flatOrSharp = a[-1]
                    if flatOrSharp == '#':
                        a = chr(ord(a[0])+1)
                    else:
                        a = chr(ord(a[0])-1)
                        if a not in freq:
                            break
                freq_list.append(freq[a])
            player.play_wave(synthesizer.generate_chord(freq_list, duration))
        else:
            # play a note
            player.play_wave(synthesizer.generate_constant_wave(fr, duration))
    return "Successfully vocalized image";
Esempio n. 21
0
def test_open_default_stream():
    player = Player()
    player.open_stream()
    ok_("open_stream() succeeded.")