File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -69,4 +69,30 @@ Then you can do::
6969 >>> f = wave.open('test.wav')
7070 >>> dac.write_timed(f.readframes(f.getnframes()), f.getframerate())
7171
72- This should play the WAV file.
72+ This should play the WAV file. Note that this will read the whole file into RAM
73+ so it has to be small enough to fit in it.
74+
75+ To play larger wave files you will have to use the micro-SD card to store it.
76+ Also the file must be read and sent to the DAC in small chunks that will fit
77+ the RAM limit of the microcontroller. Here is an example function that can
78+ play 8-bit wave files with up to 16kHz sampling::
79+
80+ import wave
81+ from pyb import DAC
82+ from pyb import delay
83+ dac = DAC(1)
84+
85+ def play(filename):
86+ f = wave.open(filename, 'r')
87+ total_frames = f.getnframes()
88+ framerate = f.getframerate()
89+
90+ for position in range(0, total_frames, framerate):
91+ f.setpos(position)
92+ dac.write_timed(f.readframes(framerate), framerate)
93+ delay(1000)
94+
95+ This function reads one second worth of data and sends it to DAC. It then waits
96+ one second and moves the file cursor to the new position to read the next second
97+ of data in the next iteration of the for-loop. It plays one second of audio at
98+ a time every one second.
You can’t perform that action at this time.
0 commit comments