Skip to content

Instantly share code, notes, and snippets.

@recantha
Created March 20, 2021 16:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save recantha/3dc800c161da641cf7c900b9eac17c59 to your computer and use it in GitHub Desktop.
Save recantha/3dc800c161da641cf7c900b9eac17c59 to your computer and use it in GitHub Desktop.
# This is the code from: https://learn.adafruit.com/adafruit-max98357-i2s-class-d-mono-amp/circuitpython-wiring-test
# On a Raspberry Pi Pico, WHICH DOES NOT HAVE I2S... Distortion aside, this sounds correct.
# How is that possible?
import time
import array
import math
import audiocore
import board
import audiobusio
sample_rate = 8000
tone_volume = .1 # Increase or decrease this to adjust the volume of the tone.
frequency = 440 # Set this to the Hz of the tone you want to generate.
length = sample_rate // frequency # One freqency period
sine_wave = array.array("H", [0] * length)
for i in range(length):
sine_wave[i] = int((math.sin(math.pi * 2 * frequency * i / sample_rate) *
tone_volume + 1) * (2 ** 15 - 1))
audio = audiobusio.I2SOut(bit_clock=board.GP10, word_select=board.GP11, data=board.GP9)
sine_wave_sample = audiocore.RawSample(sine_wave, sample_rate=sample_rate)
wave_file = open("StreetChicken.wav", "rb")
wave = audiocore.WaveFile(wave_file)
print("Playing audio")
audio.play(wave, loop=True)
i = 0
while i < 200:
print(audio.playing)
time.sleep(0.1)
i = i + 1
@alexbustamante22
Copy link

Hi I am trying to implement this code into my project. However I am getting errors that say I do not have audiocore or audiobusio imported. Do you know from where I can import these?

@SchroedersKater
Copy link

These are modules that belong to the Adafruit CircuitPython project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment