1 // Sega Master System/Mark III, Sega Genesis/Mega Drive, BBC Micro VGM music file emulator
3 // Game_Music_Emu 0.5.5
7 #include "blargg_common.h"
8 #include "blargg_source.h"
10 #include "multi_buffer.h"
11 #include "ym2413_emu.h"
12 #include "ym2612_emu.h"
15 typedef short sample_t
;
16 typedef int vgm_time_t
;
17 typedef int fm_time_t
;
19 enum { fm_time_bits
= 12 };
20 enum { blip_time_bits
= 12 };
21 enum { buf_size
= 2048 };
24 enum { header_size
= 0x40 };
33 byte track_duration
[4];
35 byte loop_duration
[4];
37 byte noise_feedback
[2];
46 enum { gme_max_field
= 63 };
49 /* times in milliseconds; -1 if unknown */
54 /* empty string if not available */
60 // Emulates VGM music using SN76489/SN76496 PSG, YM2612, and YM2413 FM sound chips.
61 // Supports custom sound buffer and frequency equalization when VGM uses just the PSG.
62 // FM sound chips can be run at their proper rates, or slightly higher to reduce
63 // aliasing on high notes. Currently YM2413 support requires that you supply a
64 // YM2413 sound chip emulator. I can provide one I've modified to work with the library.
69 bool disable_oversampling
;
76 byte
const* file_begin
;
80 byte
const* loop_begin
;
86 int dac_disabled
; // -1 if disabled
88 struct Blip_Buffer
* blip_buf
;
92 unsigned buf_changed_count
;
93 int max_initial_silence
;
102 blargg_long out_time
; // number of samples played since start of track
103 blargg_long emu_time
; // number of samples emulator has generated since start of track
104 bool emu_track_ended_
; // emulator has reached end of track
105 volatile bool track_ended
;
108 blargg_long fade_start
;
112 int silence_lookahead
; // speed to run emulator when looking ahead for silence
114 long silence_time
; // number of samples where most recent silence began
115 long silence_count
; // number of samples of silence to play before using buf
116 long buf_remain
; // number of samples left in silence buffer
118 // larger items at the end
119 struct track_info_t info
;
120 sample_t buf_
[buf_size
];
122 struct Ym2612_Emu ym2612
;
123 struct Ym2413_Emu ym2413
;
126 struct Blip_Synth pcm
;
127 struct Stereo_Buffer stereo_buf
;
129 struct Resampler resampler
;
131 struct Stereo_Buffer buf
;
134 void Vgm_init( struct Vgm_Emu
* this );
136 // Disable running FM chips at higher than normal rate. Will result in slightly
137 // more aliasing of high notes.
138 static inline void Vgm_disable_oversampling( struct Vgm_Emu
* this, bool disable
) { this->disable_oversampling
= disable
; }
140 // Header for currently loaded file
141 static inline struct header_t
*header( struct Vgm_Emu
* this ) { return (struct header_t
*) this->file_begin
; }
143 // Basic functionality (see Gme_File.h for file loading/track info functions)
144 blargg_err_t
Vgm_load_mem( struct Vgm_Emu
* this, byte
const* new_data
, long new_size
, bool parse_info
);
146 // True if any FM chips are used by file. Always false until init_fm()
148 static inline bool uses_fm( struct Vgm_Emu
* this ) { return Ym2612_enabled( &this->ym2612
) || Ym2413_enabled( &this->ym2413
); }
150 // Set output sample rate. Must be called only once before loading file.
151 blargg_err_t
Vgm_set_sample_rate( struct Vgm_Emu
* this, long sample_rate
);
153 // Start a track, where 0 is the first track. Also clears warning string.
154 blargg_err_t
Vgm_start_track( struct Vgm_Emu
* this );
156 // Generate 'count' samples info 'buf'. Output is in stereo. Any emulation
157 // errors set warning string, and major errors also end track.
158 blargg_err_t
Vgm_play( struct Vgm_Emu
* this, long count
, sample_t
* buf
) ICODE_ATTR
;
160 // Track status/control
162 // Number of milliseconds (1000 msec = 1 second) played since beginning of track
163 long Track_tell( struct Vgm_Emu
* this );
165 // Seek to new time in track. Seeking backwards or far forward can take a while.
166 blargg_err_t
Track_seek( struct Vgm_Emu
* this, long msec
);
169 blargg_err_t
Track_skip( struct Vgm_Emu
* this, long n
);
171 // Set start time and length of track fade out. Once fade ends track_ended() returns
172 // true. Fade time can be changed while track is playing.
173 void Track_set_fade( struct Vgm_Emu
* this, long start_msec
, long length_msec
);
175 // Get track length in milliseconds
176 static inline long Track_get_length( struct Vgm_Emu
* this )
178 long length
= this->info
.length
;
181 length
= this->info
.intro_length
+ 2 * this->info
.loop_length
; // intro + 2 loops
183 length
= 150 * 1000; // 2.5 minutes
189 // Sound customization
191 // Adjust song tempo, where 1.0 = normal, 0.5 = half speed, 2.0 = double speed.
192 // Track length as returned by track_info() assumes a tempo of 1.0.
193 void Sound_set_tempo( struct Vgm_Emu
* this, double t
);
195 // Mute/unmute voice i, where voice 0 is first voice
196 void Sound_mute_voice( struct Vgm_Emu
* this, int index
, bool mute
);
198 // Set muting state of all voices at once using a bit mask, where -1 mutes them all,
199 // 0 unmutes them all, 0x01 mutes just the first voice, etc.
200 void Sound_mute_voices( struct Vgm_Emu
* this, int mask
);
202 // Change overall output amplitude, where 1.0 results in minimal clamping.
203 // Must be called before set_sample_rate().
204 static inline void Sound_set_gain( struct Vgm_Emu
* this, double g
)
206 assert( !this->sample_rate
); // you must set gain before setting sample rate