Nugget
modplayer.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2021 PCSX-Redux authors
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 
27 #pragma once
28 
29 #include <stdint.h>
30 
31 // Once MOD_Load returns, these values will be valid.
32 // Unless specified, consider them read only, but
33 // modifying them might be doable if you know what you are doing.
34 extern unsigned MOD_Channels;
35 extern unsigned MOD_SongLength;
36 extern unsigned MOD_CurrentOrder;
37 extern unsigned MOD_CurrentPattern;
38 extern unsigned MOD_CurrentRow;
39 extern unsigned MOD_Speed;
40 extern unsigned MOD_Tick;
41 extern unsigned MOD_BPM;
42 extern unsigned MOD_LoopStart;
43 extern unsigned MOD_LoopCount;
44 extern uint8_t MOD_PatternDelay;
45 
46 // This is a pointer to the current row that's
47 // being played. Used for decoding. The number
48 // of relevant bytes for a row is 4 * MOD_Channels.
49 extern const uint8_t* MOD_RowPointer;
50 
51 // These four are fine to change outside of MOD_Poll.
52 // The first two are booleans, and the next two are the values
53 // you need them to be set at when MOD_Poll is called next.
54 // If you need immediate row / pattern change, also set
55 // MOD_Tick to MOD_Speed.
56 extern int MOD_ChangeRowNextTick;
57 extern int MOD_ChangeOrderNextTick;
58 extern unsigned MOD_NextRow;
59 extern unsigned MOD_NextOrder;
60 
61 // This can be used to decode MOD_RowPointer.
62 extern const uint16_t MOD_PeriodTable[];
63 
64 // Internal HIT file structure, but conformant to
65 // http://www.aes.id.au/modformat.html
66 struct MODFileFormat;
67 
68 // Returns the number of channel from this module,
69 // or 0 if the module is invalid.
70 unsigned MOD_Check(const struct MODFileFormat* module);
71 
72 // Loads the specified module and gets it ready for
73 // playback. Returns the number of bytes needed if
74 // relocation is desired. The pointer has to be
75 // aligned to a 4-bytes boundary. Will also setup
76 // the SPU.
77 uint32_t MOD_Load(const struct MODFileFormat* module);
78 
79 // Call this function periodically to play sound. The
80 // frequency at which this is called will determine the
81 // actual playback speed of the module. Most modules will
82 // not change the default tempo, which requires calling
83 // MOD_Poll 50 times per second, or exactly the vertical
84 // refresh rate in PAL. Preferably call this from timer1's
85 // IRQ however, and look up MOD_hblanks to decide of the
86 // next target value to use.
87 // To pause or stop playback, simply stop calling this
88 // function. The internal player doesn't need any
89 // sort of cleanup, and switching to another song simply
90 // requires calling MOD_Load with a new file.
91 void MOD_Poll();
92 
93 // New APIs from the original code from there on.
94 
95 // Defaults to 0. This is a boolean indicating if we
96 // want the volume settings to be monaural or the same
97 // as the original Amiga's Paula chip.
98 extern int MOD_Stereo;
99 
100 // Indicates the number of hblank ticks to wait before
101 // calling MOD_Poll. This value may or may not change
102 // after a call to MOD_Poll, if the track requested a
103 // tempo change.
104 extern uint32_t MOD_hblanks;
105 
106 // It is possible to reclaim memory from the initial call
107 // to MOD_Load, in case the module was loaded from an
108 // external source. The number of bytes needed for the
109 // player will be returned by MOD_Load. Call MOD_Relocate
110 // with a new memory buffer that has at least this many bytes.
111 // Caller is responsible for managing the memory.
112 // It is fine to reuse the same buffer as the original input,
113 // if you wish to simply realloc it after relocating it,
114 // provided your realloc implementation guarantees that the
115 // shrunk buffer will remain at the same location.
116 //
117 // For example, this pseudo-code is valid:
118 // bool load_mod_file(File mod_file) {
119 // void * buffer = malloc(file_size(mod_file));
120 // readfile(mod_file, buffer);
121 // uint32_t size = MOD_Load(buffer);
122 // if (size == 0) {
123 // free(buffer);
124 // return false;
125 // }
126 // MOD_Relocate(buffer);
127 // void * newbuffer = realloc(buffer, size);
128 // if (newbuffer != buffer) {
129 // free(newbuffer);
130 // return false;
131 // }
132 // return true;
133 // }
134 void MOD_Relocate(uint8_t* buffer);
135 
136 // Set MOD Volume to musicVolume, where musicVolume is between 0 and 65535.
137 // Defaults to 16384, and won't be reset by a subsequent MOD_Load, as it
138 // behaves as a master music volume throughout the lifetime of the app.
139 void MOD_SetMusicVolume(uint32_t musicVolume);
140 
141 // Plays an arbitrary note from the MOD's samples bank.
142 // The volume will always be centered, so the sample will
143 // be monaural. The voiceID ideally should be set to a
144 // value that is above MOD_Channels. Remember the PS1
145 // has 24 channels total, so voiceID can be between 0 and 23.
146 // The note is a value between 0 and 35. The exact note played
147 // is on the normal 12-notes, C, C#, D, ... scale, and there
148 // are three octaves available, which gives the 12*3=36
149 // interval value of the note argument. The volume argument
150 // is between 0 and 63. You can simulate KeyOff by simply
151 // setting the volume of the voice to 0. The volume will
152 // be affected by the music volume as set by the function above.
153 void MOD_PlayNote(unsigned voiceID, unsigned sampleID, unsigned note, int16_t volume);
154 
155 // Plays a sound effect.
156 // As opposed to MOD_PlayNote(), MOD_PlaySoundEffect()'s volume
157 // won't be affected by the global volume setting.
158 // 0 == mute, 63 == max SPU voice volume
159 void MOD_PlaySoundEffect(unsigned channel, unsigned sampleID, unsigned note, int16_t volume);
160 
161 // Added API to reset the SPU and silence everything.
162 void MOD_Silence();
Definition: modplayer.c:51