1 // Nintendo Game Boy GBS music file emulator
3 // Game_Music_Emu 0.5.2
8 #include "multi_buffer.h"
11 #include "m3u_playlist.h"
13 /* typedef uint8_t byte; */
14 typedef short sample_t
;
16 enum { joypad_addr
= 0xFF00 };
17 enum { ram_addr
= 0xA000 };
18 enum { hi_page
= 0xFF00 - ram_addr
};
19 enum { io_base
= 0xFF00 };
20 enum { buf_size
= 2048 };
22 // Selects which sound hardware to use. AGB hardware is cleaner than the
23 // others. Doesn't take effect until next start_track().
25 sound_dmg
= mode_dmg
, // Game Boy monochrome
26 sound_cgb
= mode_cgb
, // Game Boy Color
27 sound_agb
= mode_agb
, // Game Boy Advance
28 sound_gbs
// Use DMG/CGB based on GBS (default)
32 enum { header_size
= 112 };
51 enum sound_t sound_hardware
;
58 blip_time_t play_period
;
59 blip_time_t next_play
;
64 unsigned buf_changed_count
;
71 volatile bool track_ended
;
73 blargg_long out_time
; // number of samples played since start of track
74 blargg_long emu_time
; // number of samples emulator has generated since start of track
75 bool emu_track_ended_
; // emulator has reached end of track
78 blargg_long fade_start
;
82 // Disable automatic end-of-track detection and skipping of silence at beginning
85 int max_initial_silence
;
87 int silence_lookahead
; // speed to run emulator when looking ahead for silence
88 long silence_time
; // number of samples where most recent silence began
89 long silence_count
; // number of samples of silence to play before using buf
90 long buf_remain
; // number of samples left in silence buffer
92 // Larger items at the end
93 // Header for currently loaded file
94 struct header_t header
;
97 struct M3u_Playlist m3u
;
101 struct Stereo_Buffer stereo_buf
;
103 sample_t buf
[buf_size
];
107 byte ram
[0x4000 + 0x2000 + cpu_padding
];
111 // Basic functionality
112 // Initializes Gbs_Emu structure
113 void Gbs_init( struct Gbs_Emu
* this );
115 // Stops (clear) Gbs_Emu structure
116 void Gbs_stop( struct Gbs_Emu
* this );
118 // Loads a file from memory
119 blargg_err_t
Gbs_load( struct Gbs_Emu
* this, void* data
, long size
);
121 // Set output sample rate. Must be called only once before loading file.
122 blargg_err_t
Gbs_set_sample_rate( struct Gbs_Emu
* this, long sample_rate
);
124 // Start a track, where 0 is the first track. Also clears warning string.
125 blargg_err_t
Gbs_start_track( struct Gbs_Emu
* this, int );
127 // Generate 'count' samples info 'buf'. Output is in stereo. Any emulation
128 // errors set warning string, and major errors also end track.
129 blargg_err_t
Gbs_play( struct Gbs_Emu
* this, long count
, sample_t
* buf
) ICODE_ATTR
;
131 // Track status/control
132 // Number of milliseconds (1000 msec = 1 second) played since beginning of track
133 long Track_tell( struct Gbs_Emu
* this );
135 // Seek to new time in track. Seeking backwards or far forward can take a while.
136 blargg_err_t
Track_seek( struct Gbs_Emu
* this, long msec
);
139 blargg_err_t
Track_skip( struct Gbs_Emu
* this, long n
);
141 // Set start time and length of track fade out. Once fade ends track_ended() returns
142 // true. Fade time can be changed while track is playing.
143 void Track_set_fade( struct Gbs_Emu
* this, long start_msec
, long length_msec
);
145 // Get track length in milliseconds
146 static inline long Track_get_length( struct Gbs_Emu
* this, int n
)
148 long length
= 120 * 1000; /* 2 minutes */
149 if ( (this->m3u
.size
> 0) && (n
< this->m3u
.size
) ) {
150 struct entry_t
* entry
= &this->m3u
.entries
[n
];
151 length
= entry
->length
;
158 // Sound customization
159 // Adjust song tempo, where 1.0 = normal, 0.5 = half speed, 2.0 = double speed.
160 // Track length as returned by track_info() assumes a tempo of 1.0.
161 void Sound_set_tempo( struct Gbs_Emu
* this, double );
163 // Mute/unmute voice i, where voice 0 is first voice
164 void Sound_mute_voice( struct Gbs_Emu
* this, int index
, bool mute
);
166 // Set muting state of all voices at once using a bit mask, where -1 mutes them all,
167 // 0 unmutes them all, 0x01 mutes just the first voice, etc.
168 void Sound_mute_voices( struct Gbs_Emu
* this, int mask
);
170 // Change overall output amplitude, where 1.0 results in minimal clamping.
171 // Must be called before set_sample_rate().
172 static inline void Sound_set_gain( struct Gbs_Emu
* this, double g
)
174 assert( !this->sample_rate_
); // you must set gain before setting sample rate
179 // Emulation (You shouldn't touch these)
181 blargg_err_t
Run_clocks( struct Gbs_Emu
* this, blip_time_t duration
) ICODE_ATTR
;
182 void Set_bank( struct Gbs_Emu
* this, int ) ICODE_ATTR
;
183 void Update_timer( struct Gbs_Emu
* this ) ICODE_ATTR
;
185 // Runs CPU until time becomes >= 0
186 void Run_cpu( struct Gbs_Emu
* this ) ICODE_ATTR
;
188 // Reads/writes memory and I/O
189 int Read_mem( struct Gbs_Emu
* this, addr_t addr
) ICODE_ATTR
;
190 void Write_mem( struct Gbs_Emu
* this, addr_t addr
, int data
) ICODE_ATTR
;
193 static inline blip_time_t
Time( struct Gbs_Emu
* this )
195 return Cpu_time( &this->cpu
) + this->end_time
;
198 void Jsr_then_stop( struct Gbs_Emu
* this, byte
const [] ) ICODE_ATTR
;
199 void Write_io_inline( struct Gbs_Emu
* this, int offset
, int data
, int base
) ICODE_ATTR
;
200 void Write_io_( struct Gbs_Emu
* this, int offset
, int data
) ICODE_ATTR
;
201 int Read_io( struct Gbs_Emu
* this, int offset
) ICODE_ATTR
;
202 void Write_io( struct Gbs_Emu
* this, int offset
, int data
) ICODE_ATTR
;