diff options
author | Piotr MaliĆski <piotr.malinski@socialwifi.com> | 2017-07-19 09:44:44 +0200 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-07-21 16:52:16 +1000 |
commit | 8c9e22c12740477035bd5d78e43beeb6df598588 (patch) | |
tree | 82bb742c803806ebdb8c0c2845733f4c52290220 /docs/pyboard/tutorial | |
parent | 6cfe73759707e410d48783303ada318658d21e02 (diff) | |
download | micropython-8c9e22c12740477035bd5d78e43beeb6df598588.tar.gz micropython-8c9e22c12740477035bd5d78e43beeb6df598588.zip |
docs/pyboard/tutorial/amp_skin: Add example for playing large WAV files.
Diffstat (limited to 'docs/pyboard/tutorial')
-rw-r--r-- | docs/pyboard/tutorial/amp_skin.rst | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/docs/pyboard/tutorial/amp_skin.rst b/docs/pyboard/tutorial/amp_skin.rst index 64f139bb7e..697637f9d2 100644 --- a/docs/pyboard/tutorial/amp_skin.rst +++ b/docs/pyboard/tutorial/amp_skin.rst @@ -69,4 +69,30 @@ Then you can do:: >>> f = wave.open('test.wav') >>> dac.write_timed(f.readframes(f.getnframes()), f.getframerate()) -This should play the WAV file. +This should play the WAV file. Note that this will read the whole file into RAM +so it has to be small enough to fit in it. + +To play larger wave files you will have to use the micro-SD card to store it. +Also the file must be read and sent to the DAC in small chunks that will fit +the RAM limit of the microcontroller. Here is an example function that can +play 8-bit wave files with up to 16kHz sampling:: + + import wave + from pyb import DAC + from pyb import delay + dac = DAC(1) + + def play(filename): + f = wave.open(filename, 'r') + total_frames = f.getnframes() + framerate = f.getframerate() + + for position in range(0, total_frames, framerate): + f.setpos(position) + dac.write_timed(f.readframes(framerate), framerate) + delay(1000) + +This function reads one second worth of data and sends it to DAC. It then waits +one second and moves the file cursor to the new position to read the next second +of data in the next iteration of the for-loop. It plays one second of audio at +a time every one second. |