Play sine wave audio use I2S. #18523
-
|
CircuitPython example import time, array, math, audiocore, board, audiobusio
sample_rate = 8000
tone_volume = .7
frequency = 440
length = sample_rate // frequency
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(board.D14, board.D13, board.D12)
sine_wave_sample = audiocore.RawSample(sine_wave, sample_rate = sample_rate)
while True:
audio.play(sine_wave_sample, loop = True)
time.sleep(1)
audio.stop()
time.sleep(1)On Micropython, refer https://github.com/miketeachman/micropython-esp32-i2s-examples, play audio samples from an I2S microphone module, and Play audio samples through a speaker using an I2S amplifier module. Read audio samples from an I2S microphone module from machine import I2S
from machine import Pin
bck_pin = Pin(14) # Bit clock output
ws_pin = Pin(13) # Word clock output
sdin_pin = Pin(12) # Serial data input
audio_in = I2S(I2S.NUM0, # create I2S peripheral to read audio
bck=bck_pin, ws=ws_pin, sdin=sdin_pin, # sample data from an INMP441
standard=I2S.PHILIPS, mode=I2S.MASTER_RX, # microphone module
dataformat=I2S.B32,
channelformat=I2S.RIGHT_LEFT,
samplerate=16000,
dmacount=16,dmalen=256)
samples = bytearray(2048) # bytearray to receive audio samples
num_bytes_read = audio_in.readinto(samples) # read audio samples from microphone
# note: blocks until sample array is full
# - see optional timeout argument
# to configure maximum blocking duration Play audio samples through a speaker using an I2S amplifier module from machine import I2S
from machine import Pin
bck_pin = Pin(14) # Bit clock output
ws_pin = Pin(13) # Word clock output
sdout_pin = Pin(12) # Serial data output
audio_out = I2S(I2S.NUM1, # create I2S peripheral to write audio
bck=bck_pin, ws=ws_pin, sdout=sdout_pin, # sample data to an Adafruit I2S Amplifier
standard=I2S.PHILIPS, mode=I2S.MASTER_TX, # breakout board,
dataformat=I2S.B16, # based on MAX98357A device
channelformat=I2S.ONLY_RIGHT,
samplerate=16000,
dmacount=16,dmalen=512)
samples = bytearray(1024) # bytearray containing audio samples to transmit
num_bytes_written = audio_out.write(samples) # write audio samples to amplifier
# note: blocks until sample array is emptied
# - see optional timeout argument
# to configure maximum blocking durationCan I play sine wave on MicroPython likes CircuitPython example? how generate this sine wave audio example bytearray on MicroPython? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
Yes, and it's conceptually the same. In fact, in the examples you linked to there's play_tone.py that does what you're looking for. Effectively: Fill the sample buffer with data created from a sine wave before playing it (just like in the CircuitPython example). |
Beta Was this translation helpful? Give feedback.
Yes, and it's conceptually the same. In fact, in the examples you linked to there's play_tone.py that does what you're looking for.
Effectively: Fill the sample buffer with data created from a sine wave before playing it (just like in the CircuitPython example).