Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Music/Music.cpp@14:1e6f74233e8e, 2019-05-08 (annotated)
- Committer:
- joshdavy
- Date:
- Wed May 08 14:41:56 2019 +0000
- Revision:
- 14:1e6f74233e8e
- Parent:
- 11:db27d3838514
Documentation test
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| joshdavy | 5:b9cf407bcc63 | 1 | |
| joshdavy | 5:b9cf407bcc63 | 2 | #include "Music.h" |
| joshdavy | 5:b9cf407bcc63 | 3 | PwmOut speaker(PTC10); |
| joshdavy | 14:1e6f74233e8e | 4 | /** |
| joshdavy | 14:1e6f74233e8e | 5 | * @brief Constructor (no paramateters) |
| joshdavy | 14:1e6f74233e8e | 6 | */ |
| joshdavy | 4:afbf3dd71403 | 7 | Music::Music() |
| joshdavy | 4:afbf3dd71403 | 8 | { |
| joshdavy | 4:afbf3dd71403 | 9 | |
| joshdavy | 4:afbf3dd71403 | 10 | } |
| joshdavy | 14:1e6f74233e8e | 11 | /** |
| joshdavy | 14:1e6f74233e8e | 12 | * @brief Deconstructor |
| joshdavy | 14:1e6f74233e8e | 13 | */ |
| joshdavy | 4:afbf3dd71403 | 14 | Music::~Music() |
| joshdavy | 4:afbf3dd71403 | 15 | { |
| joshdavy | 4:afbf3dd71403 | 16 | |
| joshdavy | 4:afbf3dd71403 | 17 | |
| joshdavy | 4:afbf3dd71403 | 18 | |
| joshdavy | 4:afbf3dd71403 | 19 | } |
| joshdavy | 11:db27d3838514 | 20 | |
| joshdavy | 14:1e6f74233e8e | 21 | /** |
| joshdavy | 14:1e6f74233e8e | 22 | * @brief Initialiser. Takes the sound data and initialises the buzzer |
| joshdavy | 14:1e6f74233e8e | 23 | * @param const int* data @details The audio data. A array of the amplitude of |
| joshdavy | 14:1e6f74233e8e | 24 | * the waveform over time |
| joshdavy | 14:1e6f74233e8e | 25 | * @param length @details Length of above sound array |
| joshdavy | 14:1e6f74233e8e | 26 | */ |
| joshdavy | 5:b9cf407bcc63 | 27 | void Music::init(const int* data,int length) |
| joshdavy | 4:afbf3dd71403 | 28 | { |
| joshdavy | 5:b9cf407bcc63 | 29 | |
| joshdavy | 4:afbf3dd71403 | 30 | _data = data; |
| joshdavy | 5:b9cf407bcc63 | 31 | _length = length; |
| joshdavy | 6:2ca1516ec1e2 | 32 | speaker.period(0.00003); // Has to << then 1/sample rate |
| joshdavy | 4:afbf3dd71403 | 33 | speaker.write(0); // until music played stay silent |
| joshdavy | 5:b9cf407bcc63 | 34 | _index = 0; |
| joshdavy | 4:afbf3dd71403 | 35 | } |
| joshdavy | 14:1e6f74233e8e | 36 | /** |
| joshdavy | 14:1e6f74233e8e | 37 | * @brief Plays the next sound sample. Must be called at the sample rate |
| joshdavy | 14:1e6f74233e8e | 38 | */ |
| joshdavy | 5:b9cf407bcc63 | 39 | void Music::play_next() |
| joshdavy | 4:afbf3dd71403 | 40 | { |
| joshdavy | 5:b9cf407bcc63 | 41 | double duty_cycle; |
| joshdavy | 5:b9cf407bcc63 | 42 | duty_cycle = ((128-_data[_index]*1.7)+128)/256.0; |
| joshdavy | 5:b9cf407bcc63 | 43 | //printf("%i = %f\n",_index,duty_cycle); |
| joshdavy | 5:b9cf407bcc63 | 44 | |
| joshdavy | 5:b9cf407bcc63 | 45 | if (_index < _length) { |
| joshdavy | 5:b9cf407bcc63 | 46 | speaker.write(duty_cycle); |
| joshdavy | 4:afbf3dd71403 | 47 | |
| joshdavy | 5:b9cf407bcc63 | 48 | _index++; |
| joshdavy | 5:b9cf407bcc63 | 49 | } else { |
| joshdavy | 6:2ca1516ec1e2 | 50 | _index = 0; |
| joshdavy | 5:b9cf407bcc63 | 51 | } |
| joshdavy | 4:afbf3dd71403 | 52 | |
| joshdavy | 8:21b6d4dbce44 | 53 | } |
| joshdavy | 8:21b6d4dbce44 | 54 | |
| joshdavy | 8:21b6d4dbce44 | 55 |