def _play_to_pin(tune, base_tone, min_freq, duration, octave, tempo): """Using the prepared input send the notes to the pin """ pwm = isinstance(base_tone, pulseio.PWMOut) for note in tune.split(","): piano_note, note_duration = _parse_note(note, duration, octave) if piano_note in PIANO: if pwm: base_tone.frequency = int(PIANO[piano_note]) base_tone.duty_cycle = 2**15 else: # AudioOut interface changed in CP 3.x if sys.implementation.version[0] >= 3: pitch = int(PIANO[piano_note]) sine_wave = sine.sine_wave(16000, pitch) sine_wave_sample = audioio.RawSample(sine_wave) base_tone.play(sine_wave_sample, loop=True) else: base_tone.frequency = int(16000 * (PIANO[piano_note] / min_freq)) base_tone.play(loop=True) time.sleep(4 / note_duration * 60 / tempo) if pwm: base_tone.duty_cycle = 0 else: base_tone.stop() time.sleep(0.02)
def _play_to_pin(tune, base_tone, min_freq, duration, octave, tempo): """Using the prepared input send the notes to the pin """ pwm = isinstance(base_tone, pulseio.PWMOut) for note in tune.split(","): piano_note, note_duration = _parse_note(note, duration, octave) if piano_note in PIANO: if pwm: base_tone.frequency = int(PIANO[piano_note]) base_tone.duty_cycle = 2 ** 15 else: # AudioOut interface changed in CP 3.x if sys.implementation.version[0] >= 3: pitch = int(PIANO[piano_note]) sine_wave = sine.sine_wave(16000, pitch) sine_wave_sample = audioio.RawSample(sine_wave) base_tone.play(sine_wave_sample, loop=True) else: base_tone.frequency = int( 16000 * (PIANO[piano_note] / min_freq)) base_tone.play(loop=True) time.sleep(4 / note_duration * 60 / tempo) if pwm: base_tone.duty_cycle = 0 else: base_tone.stop() time.sleep(0.02)
def play(pin, rtttl, octave=None, duration=None, tempo=None): _, defaults, tune = rtttl.split(":") for default in defaults.split(","): if default[0] == "d" and not duration: duration = int(default[2:]) elif default[0] == "o" and not octave: octave = default[2:] elif default[0] == "b" and not tempo: tempo = int(default[2:]) if not octave: octave = 6 if not duration: duration = 4 if not tempo: tempo = 63 print("tempo", tempo, "octave", octave, "duration", duration) min_freq = 13000 for note in tune.split(","): p = None if note[0].isdigit(): p = note[1] else: p = note[0] if "#" in note: p += "#" o = octave if note[-1].isdigit(): o = note[-1] p = o + p if p[-1] != "p" and piano[p] < min_freq: min_freq = piano[p] wave = sine.sine_wave(16000, min_freq) base_tone = audioio.AudioOut(pin, wave) for note in tune.split(","): p = None d = duration if note[0].isdigit(): d = int(note[0]) p = note[1] else: p = note[0] if "." in note: d *= 1.5 if "#" in note: p += "#" o = octave if note[-1].isdigit(): o = note[-1] p = o + p if p in piano: base_tone.frequency = int(16000 * (piano[p] / min_freq)) base_tone.play(loop=True) print(p, d) time.sleep(4 / d * 60 / tempo) base_tone.stop() time.sleep(0.02)
def _get_wave(tune, octave): """Returns the proper waveform to play the song along with the minimum frequency in the song. """ min_freq = 13000 for note in tune.split(","): piano_note, _ = _parse_note(note, octave=octave) if piano_note[-1] != "p" and PIANO[piano_note] < min_freq: min_freq = PIANO[piano_note] return sine.sine_wave(16000, min_freq), min_freq
def _get_wave(tune, octave): """Returns the proper waveform to play the song along with the minimum frequency in the song. """ min_freq = 13000 for note in tune.split(","): piano_note = None if note[0].isdigit(): piano_note = note[1] else: piano_note = note[0] if "#" in note: piano_note += "#" note_octave = octave if note[-1].isdigit(): note_octave = note[-1] piano_note = note_octave + piano_note if piano_note[-1] != "p" and PIANO[piano_note] < min_freq: min_freq = PIANO[piano_note] return sine.sine_wave(16000, min_freq), min_freq
""" 'sine_demo.py'. ================================================= toggles the builtin LED using a sine wave """ import time import board import digitalio from adafruit_waveform import sine LED = digitalio.DigitalInOut(board.D13) LED.switch_to_output() SINE_SAMPLE = sine.sine_wave(150, 50) while True: for i in range(len(SINE_SAMPLE)): LED.value = i print(LED.value) time.sleep(0.50)
# Old Crow's MIDICV with CC in CircuitPython # 20 FEB 2018 21:46 import digitalio import audioio import board import time import busio import adafruit_ssd1306 import analogio from adafruit_waveform import sine # Create 440Hz sine tone sample and assign to feather's DAC FREQUENCY = 440 # 440 Hz middle 'A' SAMPLERATE = 8000 # 8000 samples/second, recommended! wave = sine.sine_wave(SAMPLERATE, FREQUENCY) a440 = audioio.AudioOut(board.A0, wave) a440.frequency = SAMPLERATE # Initialize MIDI engine states midiRunningStatus = 0 midiState = 0 midiNoteBufferIndex = 0 midiNoteBuffer = [128, 128, 128, 128, 128, 128, 128, 128, 128, 128] midiNoteToRemove = 255 # Set up MCP4922 dual DAC daccs = digitalio.DigitalInOut(board.A5) # A5 is DAC CS spi = busio.SPI(board.SCK, board.MOSI) from adafruit_bus_device.spi_device import SPIDevice mcp4922 = SPIDevice(spi, daccs, baudrate=8000000, polarity=0, phase=0)