1 // TurboGrafx-16/PC Engine HES music file emulator
3 // Game_Music_Emu 0.5.2
7 #include "blargg_source.h"
9 #include "multi_buffer.h"
12 #include "hes_apu_adpcm.h"
14 #include "m3u_playlist.h"
16 typedef short sample_t
;
18 enum { buf_size
= 2048 };
21 enum { header_size
= 0x20 };
59 hes_time_t play_period
;
60 hes_time_t last_frame_hook
;
70 unsigned buf_changed_count
;
77 volatile bool track_ended
;
79 blargg_long out_time
; // number of samples played since start of track
80 blargg_long emu_time
; // number of samples emulator has generated since start of track
81 bool emu_track_ended_
; // emulator has reached end of track
84 blargg_long fade_start
;
88 // Disable automatic end-of-track detection and skipping of silence at beginning
91 int max_initial_silence
;
93 int silence_lookahead
; // speed to run emulator when looking ahead for silence
94 long silence_time
; // number of samples where most recent silence began
95 long silence_count
; // number of samples of silence to play before using buf
96 long buf_remain
; // number of samples left in silence buffer
98 // Larger files at the end
99 // Header for currently loaded file
100 struct header_t header
;
103 struct M3u_Playlist m3u
;
106 byte
* write_pages
[page_count
+ 1]; // 0 if unmapped or I/O space
110 struct Hes_Apu_Adpcm adpcm
;
112 struct Stereo_Buffer stereo_buf
;
113 sample_t buf
[buf_size
];
117 byte sgx
[3 * page_size
+ cpu_padding
];
121 // Basic functionality
122 // Initializes Hes_Emu structure
123 void Hes_init( struct Hes_Emu
* this );
125 // Stops (clear) Hes_Emu structure
126 void Hes_stop( struct Hes_Emu
* this );
128 // Loads a file from memory
129 blargg_err_t
Hes_load( struct Hes_Emu
* this, void* data
, long size
);
131 // Set output sample rate. Must be called only once before loading file.
132 blargg_err_t
Hes_set_sample_rate( struct Hes_Emu
* this, long sample_rate
);
134 // Start a track, where 0 is the first track. Also clears warning string.
135 blargg_err_t
Hes_start_track( struct Hes_Emu
* this, int );
137 // Generate 'count' samples info 'buf'. Output is in stereo. Any emulation
138 // errors set warning string, and major errors also end track.
139 blargg_err_t
Hes_play( struct Hes_Emu
* this, long count
, sample_t
* buf
) ICODE_ATTR
;
141 // Track status/control
142 // Number of milliseconds (1000 msec = 1 second) played since ning of track
143 long Track_tell( struct Hes_Emu
* this );
145 // Seek to new time in track. Seeking backwards or far forward can take a while.
146 blargg_err_t
Track_seek( struct Hes_Emu
* this, long msec
);
149 blargg_err_t
Track_skip( struct Hes_Emu
* this, long n
);
151 // Set start time and length of track fade out. Once fade ends track_ended() returns
152 // true. Fade time can be changed while track is playing.
153 void Track_set_fade( struct Hes_Emu
* this, long start_msec
, long length_msec
);
155 // Get track length in milliseconds
156 static inline long Track_get_length( struct Hes_Emu
* this, int n
)
158 long length
= 120 * 1000; /* 2 minutes */
159 if ( (this->m3u
.size
> 0) && (n
< this->m3u
.size
) ) {
160 struct entry_t
* entry
= &this->m3u
.entries
[n
];
161 length
= entry
->length
;
168 // Sound customization
169 // Adjust song tempo, where 1.0 = normal, 0.5 = half speed, 2.0 = double speed.
170 // Track length as returned by track_info() assumes a tempo of 1.0.
171 void Sound_set_tempo( struct Hes_Emu
* this, double );
173 // Mute/unmute voice i, where voice 0 is first voice
174 void Sound_mute_voice( struct Hes_Emu
* this, int index
, bool mute
);
176 // Set muting state of all voices at once using a bit mask, where -1 mutes them all,
177 // 0 unmutes them all, 0x01 mutes just the first voice, etc.
178 void Sound_mute_voices( struct Hes_Emu
* this, int mask
);
180 // Change overall output amplitude, where 1.0 results in minimal clamping.
181 // Must be called before set_sample_rate().
182 static inline void Sound_set_gain( struct Hes_Emu
* this, double g
)
184 assert( !this->sample_rate_
); // you must set gain before setting sample rate
189 // Emulation (You shouldn't touch these)
191 int Cpu_read( struct Hes_Emu
* this, hes_addr_t
) ICODE_ATTR
;
192 void Cpu_write( struct Hes_Emu
* this, hes_addr_t
, int ) ICODE_ATTR
;
193 void Cpu_write_vdp( struct Hes_Emu
* this, int addr
, int data
) ICODE_ATTR
;
194 int Cpu_done( struct Hes_Emu
* this ) ICODE_ATTR
;
196 int Emu_cpu_read( struct Hes_Emu
* this, hes_addr_t
) ICODE_ATTR
;
197 void Emu_cpu_write( struct Hes_Emu
* this, hes_addr_t
, int data
) ICODE_ATTR
;
199 static inline byte
const* Emu_cpu_set_mmr( struct Hes_Emu
* this, int page
, int bank
)
201 this->write_pages
[page
] = 0;
203 return Rom_at_addr( &this->rom
, bank
* (blargg_long
) page_size
);
209 data
= this->cpu
.ram
;
215 data
= &this->sgx
[(bank
- 0xF9) * page_size
];
219 if ( bank
!= 0xFF ) {
220 dprintf( "Unmapped bank $%02X\n", bank
);
222 return this->rom
.unmapped
;
225 this->write_pages
[page
] = data
;