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))
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))
def play_synthe(code): synth = Synthesizer( osc1_waveform=Waveform.square, osc1_volume=0.2, ) freq = scales_freq[code - 65] player.play_wave(synth.generate_constant_wave(frequency=freq, length=0.1))
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))
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)
def test_sine_wave(): synthesizer = Synthesizer(osc1_waveform=Waveform.sine, osc1_volume=1.0, use_osc2=False, rate=RATE) wave = synthesizer.generate_constant_wave(440.0, 1.0) eq_(wave.size, RATE) assert_almost_equal(wave.max(), 1.0, places=3) assert_almost_equal(wave.min(), -1.0, places=3) assert_almost_equal(wave.mean(), 0.0, places=3)
def test_write_wave(): writer = Writer() synthesizer = Synthesizer(osc1_waveform=Waveform.sine, osc1_volume=1.0, use_osc2=False) wave = synthesizer.generate_constant_wave(440.0, 3.0) writer.write_wave("./test_wave.wav", wave) ok_("write_wave() succeeded.")
def producer(queue, tone_heuristic): synthesizer = Synthesizer( osc1_waveform=Waveform.square, osc1_volume=1.0, use_osc2=False, ) tone_heuristic.warm_up() for num_interrupts in interruption_iter(): tone_num = round( tone_heuristic.get_tone(num_interrupts) * (len(TONES) - 1)) tone = synthesizer.generate_constant_wave(TONES[tone_num], SLEEP_TIME) print(tone_num, num_interrupts) queue.put(tone)
def producer(queue): max_interrupts = get_max_interrupts() synthesizer = Synthesizer( osc1_waveform=Waveform.square, osc1_volume=1.0, use_osc2=False, ) for num_interrupts in interruption_iter(): if max_interrupts < num_interrupts: max_interrupts = num_interrupts tone_num = round(num_interrupts / max_interrupts * (len(TONES) - 1)) print(tone_num, max_interrupts) # sleep(SLEEP_TIME) tone = synthesizer.generate_constant_wave(TONES[tone_num], SLEEP_TIME) queue.put(tone)
from synthesizer import Player, Synthesizer, Waveform import numpy as np player = Player() player.open_stream() synthesizer = Synthesizer(osc1_waveform=Waveform.square, osc1_volume=1.0, use_osc2=True) blank = synthesizer.generate_constant_wave(0, 0.5) bass = synthesizer.generate_constant_wave(146.83, 0.5) synthesizer2 = Synthesizer(osc2_waveform=Waveform.square, osc2_volume=1.0, use_osc2=True) melodi = synthesizer.generate_constant_wave(261.63, 0.5) instrumenter = [blank, bass, melodi] grid = [[True, False], [False, True], [True, True], [False, True]] for takt in grid: for instrument in range(0, len(takt)): out = blank if takt[instrument] == True: out = out + instrumenter[instrument] if out.equals(blank): # Find ud af at sammenligne numpy arrays (type?) player.play_wave(out)
import time from synthesizer import Player, Synthesizer, Waveform, Oscillator """ Emergency sound maker for when Helm isn't working""" def midi_to_freq(note): a = 440 #frequency of A (common value is 440Hz) return (a / 32) * (2**((note - 9) / 12)) player = Player() player.open_stream() synthesizer = Synthesizer(osc1_waveform=Waveform.sine, osc1_volume=0.25, use_osc2=False) oscillator = Oscillator(Waveform.sine, volume=0.25) prev_note = 440 with mido.open_input() as port: for message in port: if message.type == 'note_on': current_time = time.time() note = message.note freq = message.note * 10 print(message) player.play_wave(synthesizer.generate_constant_wave(freq, 0.01)) prev_note = message.note * 10
count += 1 elif music in chord_freq and aa_sequence[count] in large and aa_sequence[ count] in polar: #Chords A-G & large print(aa_sequence[count]) synthesizer = Synthesizer(instrument1, \ volume2,use_osc2=False) player.play_wave( synthesizer.generate_chord(chord_freq[music], length[2])) count += 1 elif music in note_freq and aa_sequence[count] in small and aa_sequence[ count] in nonpolar: #Just single notes & small print(aa_sequence[count]) synthesizer = Synthesizer(instrument1, \ volume1,use_osc2=False) player.play_wave( synthesizer.generate_constant_wave(note_freq[music], length[0])) count += 1 elif music in note_freq and aa_sequence[count] in small and aa_sequence[ count] in polar: #Just single notes & small print(aa_sequence[count]) synthesizer = Synthesizer(instrument1, \ volume2,use_osc2=False) player.play_wave( synthesizer.generate_constant_wave(note_freq[music], length[0])) count += 1 elif music in note_freq and aa_sequence[count] in medium and aa_sequence[ count] in nonpolar: #Just single notes & medium print(aa_sequence[count]) synthesizer = Synthesizer(instrument1, \ volume1,use_osc2=False) player.play_wave(
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";
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)
timeout = time.time() + 35 #seconds 32-35 is ok print("Timer engaged") while True: # timer to break the loop if time.time() > timeout: print("Timer finished") break # try/except to avoid errors while writing and reading at the same time try: with open("../input/outfile_area", "rb") as fa: area_out = pickle.load(fa) areatone, note = deep_purple(area_out) print(areatone, " Hz") notes.append(note) player.play_wave(synthesizer.generate_constant_wave( areatone, 0.05)) #areatone,note = transforming_to_tones(area_out) #areatone = theremin(area_out) #areatone, note = mama_isnt_home(area_out) #areatone, note=chosen_scale except: print("Hand frame casualty") print("-----42-----") print("Plotting notes:") # Plotting notes. plotting_notes(notes) print("Plots saved with .PDF format in input folder") # take a glance to what's written in just_video.py
from synthesizer import Synthesizer, Waveform, Writer synth = Synthesizer(osc1_waveform=Waveform.square, osc1_volume=1.0 * 0.5, use_osc2=False) writer = Writer() wave = synth.generate_constant_wave(frequency=440.0, length=0.1) writer.write_wave("A.wav", wave) wave = synth.generate_constant_wave(frequency=349.228, length=0.1) writer.write_wave("F.wav", wave) wave = synth.generate_constant_wave(frequency=391.995, length=0.1) writer.write_wave("G.wav", wave)
from synthesizer import Player, Synthesizer, Waveform from common.tones import Chord, Intervals, Tones from common import tones player = Player() player.open_stream() synthesizer = Synthesizer(osc1_waveform=Waveform.sine, osc1_volume=1.0, use_osc2=False) # Play A4 player.play_wave(synthesizer.generate_constant_wave(Tones.A4_freq, 1.0)) player.play_wave( synthesizer.generate_chord( tones.Chord.freqs_mult(Tones.A4_freq, Chord.Type.major), 2.0)) player.play_wave( synthesizer.generate_chord( tones.Chord.freqs_mult(Tones.A4_freq, Chord.Type.minor), 2.0)) player.play_wave( synthesizer.generate_chord( tones.Chord.freqs_mult(Tones.A4_freq, Chord.Type.dim), 2.0)) player.play_wave( synthesizer.generate_chord( tones.Chord.freqs_mult(Tones.A4_freq, Chord.Type.aug), 2.0)) # Play C major player.play_wave(synthesizer.generate_chord(Chord.C_major_chord, 3.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
def play(hz, volume): synthesizer = Synthesizer(Waveform.sine, volume, False) wave = synthesizer.generate_constant_wave(hz, 5) player_ = player.Player() player_.open_stream() player_.play_wave(wave)
from synthesizer import Synthesizer, Waveform, Writer # ここの説明は 音作り を参照 synth = Synthesizer(osc1_waveform=Waveform.sine, osc1_volume=1.0, use_osc2=False) # 一定音程の波形を生成する wave = synth.generate_constant_wave(frequency=440.0, length=1.0) # オーディオファイル出力用クラス writer = Writer() writer.write_wave("sine.wav", wave)
""" Synthesize a simple sound of a given frequency. """ from synthesizer import Player, Synthesizer, Waveform, Writer import scales player = Player() player.open_stream() synthesizer = Synthesizer(osc1_waveform=Waveform.sine, osc1_volume=1.0, use_osc2=False) # Play A4 player.play_wave(synthesizer.generate_constant_wave(440.0, 1.0)) # Play C major chord = [261.626, 329.628, 391.996] player.play_wave(synthesizer.generate_chord(chord, 1.0)) # Play exemplary "almost" chord # C_4 chord = [270.000, 329.628, 370.000] player.play_wave(synthesizer.generate_chord(chord, 2.0)) # write writer = Writer() chord = [170.000, 329.628, 570.000] wave = synthesizer.generate_chord(chord, 3.0) writer.write_wave("output_records/synth.wav", wave) # # generate the sound of a full scale of notes # scale_dict = scales.fit_frequencies(scales.full_scale)
from synthesizer import Player, Synthesizer, Waveform player = Player() player.open_stream() # 以下をexcelからコピペする arr = [(523, 0.375), (0, 0.0535714285714286), (523, 0.428571428571429), (784, 0.375), (0, 0.0535714285714286), (784, 0.428571428571429), (880, 0.375), (0, 0.0535714285714286), (880, 0.428571428571429), (784, 0.857142857142857), (698, 0.375), (0, 0.0535714285714286), (698, 0.428571428571429), (659, 0.375), (0, 0.0535714285714286), (659, 0.428571428571429), (587, 0.375), (0, 0.0535714285714286), (587, 0.428571428571429), (523, 0.857142857142857)] arr = [(659, 0.4), (494, 0.2), (523, 0.2), (587, 0.4), (523, 0.2), (494, 0.2), (440, 0.35), (0, 0.05), (440, 0.2), (523, 0.2), (659, 0.4), (587, 0.2), (523, 0.2), (494, 0.35), (0, 0.05), (494, 0.2), (523, 0.2), (587, 0.4), (659, 0.4), (523, 0.4), (440, 0.3), (0, 0.1), (440, 0.6), (0, 0.4), (587, 0.4), (698, 0.2), (880, 0.4), (784, 0.2), (698, 0.2), (659, 0.35), (0, 0.05), (659, 0.2), (523, 0.2), (659, 0.35), (0, 0.05), (587, 0.2), (523, 0.2), (494, 0.35), (0, 0.05), (494, 0.2), (523, 0.2), (587, 0.4), (659, 0.4), (523, 0.4), (440, 0.35), (0, 0.05), (440, 0.8)] synth = Synthesizer(osc1_waveform=Waveform.sine, osc1_volume=1.0, use_osc2=False) for p in arr: player.play_wave(synth.generate_constant_wave(p[0], p[1]))
import genome if __name__ == '__main__': player = Player() player.open_stream() synthesizer = Synthesizer(osc1_waveform=Waveform.sine, osc1_volume=1.0, use_osc2=False) w = 0.5 # weight for frequency transposition (0.5 = one octave lower) l = 0.05 # duration of each note nuc_freqs = { 'a': 440.00 * w, 'g': 392.00 * 2 * w, 'c': 523.25 * w, 't': 2793 * w } merge_repeated_nucleotides = True if (merge_repeated_nucleotides): for nuc_seg in genome.segments(): player.play_wave( synthesizer.generate_constant_wave(nuc_freqs[nuc_seg[0]], l * nuc_seg[1])) else: for nucleotide in genome.sequence(): player.play_wave( synthesizer.generate_constant_wave(nuc_freqs[nucleotide], l))