1 /*
2     midiplayback : SMF MIDI File player suitable for 8 - 32bit targets
3     Copyright (c) 2004-2016 Adrian M. Gin
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License v2 as published by
7     the Free Software Foundation;
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 
18     The above copyright notice and this permission notice shall be
19     included in all copies or substantial portions of the Software.
20 
21 */
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 
28 
29 #ifndef MIDI_PLAYBACK_H
30 #define MIDI_PLAYBACK_H
31 
32 #include "midiparser.h"
33 #include <stdint.h>
34 
35 #define MPB_EVENT_STACK_SIZE (16)
36 #define MPB_DEFAULT_TEMPO (120)
37 #define MPB_DEFAULT_PPQ (96)
38 
39 #define MPB_NULL_ON_ARRAY (255)
40 #define MPB_DEFAULT_NOTE_ON_DIVISOR (8)
41 //Continue Playing Status
42 enum
43 {
44     MPB_FILE_STILL_PLAYING = 0,
45     MPB_FILE_FINISHED,
46 
47 };
48 
49 //Playback Modes
50 typedef enum
51 {
52     MPB_PB_ALL_ON = 0,
53     MPB_PB_NO_VOL,
54     MPB_PB_NO_NOTES, //no notes and no lyrics too
55     MPB_PB_SAVE_MIDI_STATUS,
56     MPB_PB_ALL_OFF,
57 } MIDI_PB_MODE;
58 
59 
60 
61 
62 enum
63 {
64     MPB_TRACK_STOPPED = (0),
65     MPB_NEW_DATA,
66     MPB_PLAY_NEXT_EVENT,
67     MPB_QUEUED_EVENT,
68     MPB_REPOSITION_EVENT,
69 };
70 
71 
72 extern void SendMIDIEvent(MIDI_CHAN_EVENT_t* event);
73 
74 uint8_t MPB_PlayMIDIFile(MIDI_HEADER_CHUNK_t* MIDIHdr, char* filename);
75 void    MPB_ResetMIDITracks(MIDI_HEADER_CHUNK_t* MIDIHdr);
76 
77 uint8_t MPB_PlayTrack(MIDI_HEADER_CHUNK_t* MIDIHdr, MIDI_TRACK_CHUNK_t* track, MIDI_PB_MODE mode);
78 uint8_t MPB_AddEvent(MIDI_EVENT_t* event);
79 void MPB_ProcessMetaEvent(MIDI_HEADER_CHUNK_t* MIDIHdr, MIDI_TRACK_CHUNK_t* track, MIDI_EVENT_t* event);
80 void MPB_PlayEvent(MIDI_EVENT_t* event, MIDI_PB_MODE mode);
81 void MPB_OutputMIDIChanEvent(MIDI_CHAN_EVENT_t* chanEvent);
82 
83 void MPB_SetTranspose(MIDI_HEADER_CHUNK_t* MIDIHdr, int8_t transpose);
84 void MPB_ApplyTranspose(MIDI_EVENT_t* event, int8_t transpose);
85 
86 
87 uint16_t MPB_SetTickRate(uint16_t BPM, uint16_t PPQ);
88 void MPB_ResetMIDI(void);
89 void MPB_StopAllSounds(void);
90 void MPB_ProcessGenericEvent(MIDI_HEADER_CHUNK_t* MIDIHdr, MIDI_TRACK_CHUNK_t* track, MIDI_EVENT_t* event, MIDI_PB_MODE mode);
91 
92 void MPB_ReplayStatusBuffer(void);
93 void MPB_SaveMIDIStatus(MIDI_CHAN_EVENT_t* chanEvent);
94 
95 uint8_t MPB_FastFwd_ToEvent(MIDI_HEADER_CHUNK_t* MIDIHdr, uint32_t position, MIDI_PB_MODE mode, MIDI_CHAN_EVENT_t* event, MPB_FF_MODE_t ffMode);
96 void MPB_FastFwd_TestEvent(MIDI_HEADER_CHUNK_t* MIDIHdr, MIDI_EVENT_t* event);
97 
98 uint8_t MPB_RePositionTime(MIDI_HEADER_CHUNK_t* MIDIHdr, uint16_t timePosSec, MIDI_PB_MODE mode);
99 uint8_t MPB_RePosition(MIDI_HEADER_CHUNK_t* MIDIHdr, uint32_t position, MIDI_PB_MODE mode);
100 uint8_t MPB_ContinuePlay(MIDI_HEADER_CHUNK_t* MIDIHdr, MIDI_PB_MODE mode);
101 
102 void MPB_DetermineMIDIFileStats(MIDI_HEADER_CHUNK_t* MIDIHdr);
103 uint16_t MPB_CurrentTimePosition(MIDI_HEADER_CHUNK_t* MIDIHdr);
104 uint16_t MPB_CurrentBarPosition(MIDI_HEADER_CHUNK_t* MIDIHdr);
105 
106 void   _MIDI_readbuf(uint8_t* position, uint8_t* buf, uint16_t size);
107 uint8_t _MIDI_fileopen(char* filename);
108 void _MIDI_fileclose(void);
109 
110 #define MPB_ReadToBuffer(position, buf) _MIDI_readbuf(position, buf, MIDI_TRACK_BUFFER_SIZE)
111 #define MPB_OpenFile(x) _MIDI_fileopen(x)
112 #define MPB_CloseFile() _MIDI_fileclose()
113 
114 //Define in hardwareSpecific.h
115 //#define MIDI_Tx(x)          uartTx(PrimaryUART, x)
116 //#define MIXI_TxDump(x, n)   uartTxDump(PrimaryUART, x, n)
117 
118 MIDI_EVENT_t* MPB_GetNextEvent(void);
119 MIDI_EVENT_t* MPB_ConfirmEventTx(void);
120 
121 
122 void MPB_EnablePlayback(MIDI_HEADER_CHUNK_t* MIDIHdr);
123 void MPB_PausePlayback(MIDI_HEADER_CHUNK_t* MIDIHdr);
124 void MPB_TogglePlayback(MIDI_HEADER_CHUNK_t* MIDIHdr);
125 void MPB_SetPlaybackState(MIDI_HEADER_CHUNK_t* MIDIHdr, MidiPlaybackState_t state);
126 MidiPlaybackState_t MPB_GetPlaybackState(MIDI_HEADER_CHUNK_t* MIDIHdr);
127 
128 //Private Functions for StatusBuffer control
129 void _addEventToBuffer(MIDI_CHAN_EVENT_t* chanEvent);
130 MIDI_CHAN_EVENT_t* _getEventFromBuffer(uint8_t channel, uint16_t index);
131 void _clearEventBuffer(void);
132 MIDI_CHAN_EVENT_t* _findChanEventInBuffer(uint8_t firstByte, uint8_t secondByte);
133 
134 
135 #endif
136 
137 
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 
143 
144