Jared DiCarlo
/
Player2
aaaaaaaaa
player.h@0:6679ec22f0e5, 2019-11-10 (annotated)
- Committer:
- dicarloj
- Date:
- Sun Nov 10 23:19:38 2019 +0000
- Revision:
- 0:6679ec22f0e5
aaaaaaaaaaaaaaa
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dicarloj | 0:6679ec22f0e5 | 1 | #ifndef PLAYER_H |
dicarloj | 0:6679ec22f0e5 | 2 | #define PLAYER_H |
dicarloj | 0:6679ec22f0e5 | 3 | |
dicarloj | 0:6679ec22f0e5 | 4 | #include <stdint.h> |
dicarloj | 0:6679ec22f0e5 | 5 | #include <stdio.h> |
dicarloj | 0:6679ec22f0e5 | 6 | |
dicarloj | 0:6679ec22f0e5 | 7 | // Midi player for tesla coils |
dicarloj | 0:6679ec22f0e5 | 8 | // - adjusts duty cycle to prevent coil from exploding when many notes play simultaneously |
dicarloj | 0:6679ec22f0e5 | 9 | // - messes with phase offset to work around 1-bit output resolution of tesla coil when possible |
dicarloj | 0:6679ec22f0e5 | 10 | |
dicarloj | 0:6679ec22f0e5 | 11 | // set this to audio sample frequency |
dicarloj | 0:6679ec22f0e5 | 12 | #define SAMPLE_FREQEUENCY 48000 |
dicarloj | 0:6679ec22f0e5 | 13 | |
dicarloj | 0:6679ec22f0e5 | 14 | #define CHEATER_FREQUENCY_MULT_POWER 9 |
dicarloj | 0:6679ec22f0e5 | 15 | |
dicarloj | 0:6679ec22f0e5 | 16 | // maximum number of notes that can be played |
dicarloj | 0:6679ec22f0e5 | 17 | // if this is too large, the code will be slow |
dicarloj | 0:6679ec22f0e5 | 18 | // it this is too small, notes might be left out |
dicarloj | 0:6679ec22f0e5 | 19 | #define MAX_NOTES 10 |
dicarloj | 0:6679ec22f0e5 | 20 | |
dicarloj | 0:6679ec22f0e5 | 21 | // 64-bit struct representing a single note to be played |
dicarloj | 0:6679ec22f0e5 | 22 | // each tick is 1/sample_frequency long |
dicarloj | 0:6679ec22f0e5 | 23 | // notes contain: |
dicarloj | 0:6679ec22f0e5 | 24 | // - starting time (up to 2^32 ticks, ~1 day at 50 kHz) |
dicarloj | 0:6679ec22f0e5 | 25 | // - duration (up to 2^24 ticks, ~6 minutes at 50 kHz) |
dicarloj | 0:6679ec22f0e5 | 26 | // * the lower 16 bits of duration are stored in _duration |
dicarloj | 0:6679ec22f0e5 | 27 | // - note (midi notes are 0-255, this is looked up in the frequency table) |
dicarloj | 0:6679ec22f0e5 | 28 | struct Note { |
dicarloj | 0:6679ec22f0e5 | 29 | Note(uint32_t start, uint32_t duration, uint8_t note) : |
dicarloj | 0:6679ec22f0e5 | 30 | _note(note) { |
dicarloj | 0:6679ec22f0e5 | 31 | if(start >= (1 << 16)) |
dicarloj | 0:6679ec22f0e5 | 32 | printf("! start tick %d is too long\n", start); |
dicarloj | 0:6679ec22f0e5 | 33 | _start_tick = start & 0xffff; |
dicarloj | 0:6679ec22f0e5 | 34 | if(duration >= (1 << 8)) |
dicarloj | 0:6679ec22f0e5 | 35 | printf("! Duration %d is too long\n", duration); |
dicarloj | 0:6679ec22f0e5 | 36 | _duration = duration & 0xff; |
dicarloj | 0:6679ec22f0e5 | 37 | } |
dicarloj | 0:6679ec22f0e5 | 38 | uint16_t _start_tick; |
dicarloj | 0:6679ec22f0e5 | 39 | uint8_t _duration; |
dicarloj | 0:6679ec22f0e5 | 40 | uint8_t _note; |
dicarloj | 0:6679ec22f0e5 | 41 | }; |
dicarloj | 0:6679ec22f0e5 | 42 | |
dicarloj | 0:6679ec22f0e5 | 43 | |
dicarloj | 0:6679ec22f0e5 | 44 | // set this to the Note array for the song |
dicarloj | 0:6679ec22f0e5 | 45 | extern Note* song; |
dicarloj | 0:6679ec22f0e5 | 46 | |
dicarloj | 0:6679ec22f0e5 | 47 | // set this to the length of the Note array |
dicarloj | 0:6679ec22f0e5 | 48 | extern uint32_t songLength; |
dicarloj | 0:6679ec22f0e5 | 49 | |
dicarloj | 0:6679ec22f0e5 | 50 | // this becomes true when the song is done playing |
dicarloj | 0:6679ec22f0e5 | 51 | extern bool songDone; |
dicarloj | 0:6679ec22f0e5 | 52 | |
dicarloj | 0:6679ec22f0e5 | 53 | |
dicarloj | 0:6679ec22f0e5 | 54 | // call this at SAMPLE_FREQUENCY to determine if the output should be enabled or not |
dicarloj | 0:6679ec22f0e5 | 55 | uint16_t play(); |
dicarloj | 0:6679ec22f0e5 | 56 | |
dicarloj | 0:6679ec22f0e5 | 57 | // call this once BEFORE calling play the first time and every time after switching songs |
dicarloj | 0:6679ec22f0e5 | 58 | void initialize_play(); |
dicarloj | 0:6679ec22f0e5 | 59 | |
dicarloj | 0:6679ec22f0e5 | 60 | #endif // PLAYER_H |