def play_tone(): """Generate tone and transmit to I2S amp.""" length = 4000 // 440 sine_wave = array("H", [0] * length) for i in range(length): sine_wave[i] = int(sin(pi * 2 * i / 18) * (2**15) + 2**15) sample = RawSample(sine_wave, sample_rate=8000) i2s = I2SOut(TX, RX, D9) i2s.play(sample, loop=True) sleep(1) i2s.stop() sample.deinit() i2s.deinit()
def shutdown(): global px, color, clear, speaker, audio len = SAMPLERATE // FREQUENCY sine_wave = array.array("H", [0] * len) for i in range(len): sine_wave[i] = int(math.sin(math.pi * 2 * i / len) * (2**15) + 2**15) sine_wave_sample = RawSample(sine_wave) audio.play(sine_wave_sample, loop=1) px.fill(clear) time.sleep(0.2) audio.stop() px.fill(color) time.sleep(0.2) audio.play(sine_wave_sample, loop=1) px.fill(clear) time.sleep(0.2) audio.stop() px.fill(color) time.sleep(0.2) # dim for i in range(100, -1, -1): px.fill((color[0] * i / 100, color[1] * i / 100, color[2] * i / 100))
def make_sample_list(levels=10, volume=32767, range_l=24, start_l=8): """Make a list of tuples of (RawSample, sample_length) with a sine wave of varying resolution from high to low. The lower resolutions sound crunchier and louder on the CLUE.""" ### Make a range of sample lengths, default is between 32 and 8 sample_lens = [ int((x * (range_l + .99) / (levels - 1)) + start_l) for x in range(0, levels) ] sample_lens.reverse() wavefs = [] for s_len in sample_lens: raw_samples = array.array("H", [ round(volume * math.sin(2 * math.pi * (idx / s_len))) + AUDIO_MIDPOINT for idx in range(s_len) ]) sound_samples = RawSample(raw_samples) wavefs.append((sound_samples, s_len)) return wavefs
uart_server = UARTService() advertisement = ProvideServicesAdvertisement(uart_server) ble._adapter.name = "TTY_MACHINE" # pylint: disable=protected-access # constants for sine wave generation SIN_LENGTH = 100 # more is less choppy SIN_AMPLITUDE = 2**12 # 0 (min) to 32768 (max) 8192 is nice SIN_OFFSET = 32767.5 # for 16bit range, (2**16 - 1) / 2 DELTA_PI = 2 * math.pi / SIN_LENGTH # happy little constant sine_wave = [ int(SIN_OFFSET + SIN_AMPLITUDE * math.sin(DELTA_PI * i)) for i in range(SIN_LENGTH) ] tones = ( RawSample(array.array("H", sine_wave), sample_rate=1800 * SIN_LENGTH), # Bit 0 RawSample(array.array("H", sine_wave), sample_rate=1400 * SIN_LENGTH), # Bit 1 ) bit_0 = tones[0] bit_1 = tones[1] carrier = tones[1] char_pause = 0.0 # pause time between chars, set to 0 for fastest rate possible dac = audiopwmio.PWMAudioOut( board.A2) # the CLUE edge connector marked "#0" to STEMMA speaker # The CLUE's on-board speaker works OK, not great, just crank amplitude to full before trying. # dac = audiopwmio.PWMAudioOut(board.SPEAKER)
try: from audiopwmio import PWMAudioOut as AudioOut except ImportError: pass # not always supported by every board! FREQUENCY = 440 # 440 Hz middle 'A' SAMPLERATE = 8000 # 8000 samples/second, recommended! # Generate one period of sine wav. length = SAMPLERATE // FREQUENCY sine_wave = array.array("H", [0] * length) for i in range(length): sine_wave[i] = int(math.sin(math.pi * 2 * i / length) * (2**15) + 2**15) # Enable the speaker speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) speaker_enable.direction = digitalio.Direction.OUTPUT speaker_enable.value = True audio = AudioOut(board.SPEAKER) sine_wave_sample = RawSample(sine_wave) # A single sine wave sample is hundredths of a second long. If you set loop=False, it will play # a single instance of the sample (a quick burst of sound) and then silence for the rest of the # duration of the time.sleep(). If loop=True, it will play the single instance of the sample # continuously for the duration of the time.sleep(). audio.play(sine_wave_sample, loop=True) # Play the single sine_wave sample continuously... time.sleep(1) # for the duration of the sleep (in seconds) audio.stop() # and then stop.
Input of 0 returns 1.0, pi returns 0.0, 2*pi returns -1.0.""" return 1.0 - angle % twopi / twopi * 2 # make a sawtooth wave between +/- each value in volumes # phase shifted so it starts and ends near midpoint vol = 32767 sample_len = 10 waveraw = array.array("H", [ midpoint + round(vol * sawtooth((idx + 0.5) / sample_len * twopi + math.pi)) for idx in range(sample_len) ]) beep = RawSample(waveraw, sample_rate=sample_len * A4refhz) # play something to get things inside audio libraries initialised audio.play(beep, loop=True) time.sleep(0.1) audio.stop() audio.play(beep) # brightness 1.0 saves memory by removing need for a second buffer # 10 is number of NeoPixels on CPX/CPB numpixels = 10 pixels = neopixel.NeoPixel(board.NEOPIXEL, numpixels, brightness=1.0) # B is right (usb at top) button_right = digitalio.DigitalInOut(board.BUTTON_B) button_right.switch_to_input(pull=digitalio.Pull.DOWN)
### voice 0 seems to be louder than the rest, not using it mixer = Mixer(voice_count=len(st_mult) + 1, sample_rate=rate, channel_count=1, bits_per_sample=16, samples_signed=False) ### quiet audible click as this initialises audio.play(mixer) ### Create samples for each note (semitone) from C4 to C5 note_samples = [] for st, mult in enumerate(st_mult): sample = RawSample(array.array("H", [ round(midpoint + triangle(x * twopi / (buffer_size / round(c_waves * mult))) * vol) for x in range(buffer_size) ]), sample_rate=rate) mixer.voice[st + 1].level = 0.0 ##mixer.voice[st].play(sample, loop=True) note_samples.append(sample) def chromaticText(mxr, notes): """Chromatic scale test.""" for st in range(len(st_mult)): mxr.voice[st + 1].play(notes[st], loop=True) for idx in range(20 + 1): mxr.voice[st + 1].level = idx / 20 time.sleep(0.05) time.sleep(0.25) ### sustain
stereo_dac = AudioOut(board.A0, right_channel=board.A1, quiescent_value=0) frames = [locals()[f"frame_{i}"] for i in range(18)] buffers = [] for c, points in enumerate(frames): print(f"Building frame {c}") print(f"Number of array cells {((len(points) - 1) * samples) * 2}") print(f"Num lines {len(points) - 1}") line_array = array("H", [0] * ((len(points) - 1) * samples * 2)) i = 0 for pt1, pt2 in pairwise(points): pt1 = int(pt1[0] * 65535 / 400), int(pt1[1] * 65535 / 400) pt2 = int(pt2[0] * 65535 / 400), int(pt2[1] * 65535 / 400) temp = sum(gen_line(pt1, pt2), ()) for val in temp: line_array[i] = val i += 1 buffers.append( RawSample(line_array, channel_count=2, sample_rate=1_000_000)) i = 0 while True: stereo_dac.play(buffers[i], loop=True) sleep(0.08) stereo_dac.stop() i = (i + 1) % len(buffers)