Nugget
Bare-metal libraries and examples for the original PlayStation
Loading...
Searching...
No Matches
modplayer.h
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2021 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
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.
34extern unsigned MOD_Channels;
35extern unsigned MOD_SongLength;
36extern unsigned MOD_CurrentOrder;
37extern unsigned MOD_CurrentPattern;
38extern unsigned MOD_CurrentRow;
39extern unsigned MOD_Speed;
40extern unsigned MOD_Tick;
41extern unsigned MOD_BPM;
42extern unsigned MOD_LoopStart;
43extern unsigned MOD_LoopCount;
44extern 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.
49extern 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.
56extern int MOD_ChangeRowNextTick;
58extern unsigned MOD_NextRow;
59extern unsigned MOD_NextOrder;
60
61// This can be used to decode MOD_RowPointer.
62extern const uint16_t MOD_PeriodTable[];
63
64// Internal HIT file structure, but conformant to
65// http://www.aes.id.au/modformat.html
66struct MODFileFormat;
67
68// Returns the number of channel from this module,
69// or 0 if the module is invalid.
70unsigned 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.
77uint32_t MOD_Load(const struct MODFileFormat* module);
78
79// Loads the specified module and gets it ready for
80// playback. The pointers have to be aligned to a
81// 4-bytes boundary. Will also setup the SPU. This
82// call is meant to be used with the separate .smp
83// file, which the new modconv.exe tool can generate.
84// Returns the number of channels from the module,
85// or 0 if the module is invalid. No relocation is
86// needed, and the sampleData pointer can simply be
87// freed after this call. If sampleData is NULL, the
88// function considers the samples are already loaded
89// in the SPU.
90unsigned MOD_LoadEx(const struct MODFileFormat* module, const uint8_t* sampleData);
91
92// Call this function periodically to play sound. The
93// frequency at which this is called will determine the
94// actual playback speed of the module. Most modules will
95// not change the default tempo, which requires calling
96// MOD_Poll 50 times per second, or exactly the vertical
97// refresh rate in PAL. Preferably call this from timer1's
98// IRQ however, and look up MOD_hblanks to decide of the
99// next target value to use.
100// To pause or stop playback, simply stop calling this
101// function. The internal player doesn't need any
102// sort of cleanup, and switching to another song simply
103// requires calling MOD_Load with a new file.
104void MOD_Poll();
105
106// New APIs from the original code from there on.
107
108// Defaults to 0. This is a boolean indicating if we
109// want the volume settings to be monaural or the same
110// as the original Amiga's Paula chip.
111extern int MOD_Stereo;
112
113// Indicates the number of hblank ticks to wait before
114// calling MOD_Poll. This value may or may not change
115// after a call to MOD_Poll, if the track requested a
116// tempo change.
117extern uint32_t MOD_hblanks;
118
119// Scales every voice's sample rate, in 4.12 fixed point. Defaults to
120// 4096 (1.0). Set it when the module's samples were resampled from the
121// rate the module was authored for, e.g. 2731 for samples stored at 2/3
122// of their original rate.
123extern uint32_t MOD_SampleRateScale;
124
125// It is possible to reclaim memory from the initial call
126// to MOD_Load, in case the module was loaded from an
127// external source. The number of bytes needed for the
128// player will be returned by MOD_Load. Call MOD_Relocate
129// with a new memory buffer that has at least this many bytes.
130// Caller is responsible for managing the memory.
131// It is fine to reuse the same buffer as the original input,
132// if you wish to simply realloc it after relocating it,
133// provided your realloc implementation guarantees that the
134// shrunk buffer will remain at the same location.
135//
136// For example, this pseudo-code is valid:
137// bool load_mod_file(File mod_file) {
138// void * buffer = malloc(file_size(mod_file));
139// readfile(mod_file, buffer);
140// uint32_t size = MOD_Load(buffer);
141// if (size == 0) {
142// free(buffer);
143// return false;
144// }
145// MOD_Relocate(buffer);
146// void * newbuffer = realloc(buffer, size);
147// if (newbuffer != buffer) {
148// free(newbuffer);
149// return false;
150// }
151// return true;
152// }
153void MOD_Relocate(uint8_t* buffer);
154
155// Set MOD Volume to musicVolume, where musicVolume is between 0 and 65535.
156// Defaults to 16384, and won't be reset by a subsequent MOD_Load, as it
157// behaves as a master music volume throughout the lifetime of the app.
158void MOD_SetMusicVolume(uint32_t musicVolume);
159
160// Plays an arbitrary note from the MOD's samples bank.
161// The volume will always be centered, so the sample will
162// be monaural. The voiceID ideally should be set to a
163// value that is above MOD_Channels. Remember the PS1
164// has 24 channels total, so voiceID can be between 0 and 23.
165// The note is a value between 0 and 35. The exact note played
166// is on the normal 12-notes, C, C#, D, ... scale, and there
167// are three octaves available, which gives the 12*3=36
168// interval value of the note argument. The volume argument
169// is between 0 and 63. You can simulate KeyOff by simply
170// setting the volume of the voice to 0. The volume will
171// be affected by the music volume as set by the function above.
172void MOD_PlayNote(unsigned voiceID, unsigned sampleID, unsigned note, int16_t volume);
173
174// Plays a sound effect.
175// As opposed to MOD_PlayNote(), MOD_PlaySoundEffect()'s volume
176// won't be affected by the global volume setting.
177// 0 == mute, 63 == max SPU voice volume
178void MOD_PlaySoundEffect(unsigned channel, unsigned sampleID, unsigned note, int16_t volume);
179
180// Added API to reset the SPU and silence everything.
181void MOD_Silence();
182
183// Read-only channel state, for visualizers. The channel has
184// to be below MOD_Channels. The volume is the channel's
185// current volume between 0 and 63, as set by the sample, Cxx
186// and the volume slides; tremolo is not included. The key on
187// count goes up by one, modulo 256, each time the player keys
188// on the channel's voice, so compare it to the previous value.
189unsigned MOD_GetChannelVolume(unsigned channel);
190unsigned MOD_GetChannelKeyOns(unsigned channel);
void MOD_Relocate(uint8_t *buffer)
Definition modplayer.c:359
unsigned MOD_GetChannelVolume(unsigned channel)
Definition modplayer.c:927
unsigned MOD_LoopCount
Definition modplayer.c:241
int MOD_ChangeRowNextTick
Definition modplayer.c:235
unsigned MOD_CurrentRow
Definition modplayer.c:226
unsigned MOD_Tick
Definition modplayer.c:228
uint8_t MOD_PatternDelay
Definition modplayer.c:239
void MOD_Silence()
Definition modplayer.c:352
unsigned MOD_GetChannelKeyOns(unsigned channel)
Definition modplayer.c:929
void MOD_Poll()
Definition modplayer.c:872
unsigned MOD_LoadEx(const struct MODFileFormat *module, const uint8_t *sampleData)
Definition modplayer.c:347
int MOD_ChangeOrderNextTick
Definition modplayer.c:237
int MOD_Stereo
Definition modplayer.c:242
const uint16_t MOD_PeriodTable[]
Definition modplayer.c:388
const uint8_t * MOD_RowPointer
Definition modplayer.c:234
void MOD_SetMusicVolume(uint32_t musicVolume)
Definition modplayer.c:920
unsigned MOD_NextRow
Definition modplayer.c:236
uint32_t MOD_SampleRateScale
Definition modplayer.c:244
unsigned MOD_Check(const struct MODFileFormat *module)
Definition modplayer.c:209
void MOD_PlaySoundEffect(unsigned channel, unsigned sampleID, unsigned note, int16_t volume)
Definition modplayer.c:913
unsigned MOD_CurrentPattern
Definition modplayer.c:225
unsigned MOD_Speed
Definition modplayer.c:227
void MOD_PlayNote(unsigned voiceID, unsigned sampleID, unsigned note, int16_t volume)
Definition modplayer.c:899
unsigned MOD_NextOrder
Definition modplayer.c:238
unsigned MOD_CurrentOrder
Definition modplayer.c:224
uint32_t MOD_Load(const struct MODFileFormat *module)
Definition modplayer.c:345
unsigned MOD_BPM
Definition modplayer.c:231
unsigned MOD_LoopStart
Definition modplayer.c:240
uint32_t MOD_hblanks
Definition modplayer.c:243
unsigned MOD_SongLength
Definition modplayer.c:220
unsigned MOD_Channels
Definition modplayer.c:219
Definition modplayer.c:50
static void * buffer
Definition syscalls.h:274