1 // MSX computer KSS music file emulator
3 // Game_Music_Emu 0.5.5
8 #include "blargg_common.h"
11 #include "multi_buffer.h"
13 #include "kss_scc_apu.h"
18 #include "m3u_playlist.h"
19 #include "track_filter.h"
21 typedef int kss_time_t
;
22 typedef int kss_addr_t
;
23 typedef struct Z80_Cpu Kss_Cpu
;
27 sms_psg_flag
= 1 << 0,
29 msx_psg_flag
= 1 << 2,
30 msx_scc_flag
= 1 << 3,
31 msx_music_flag
= 1 << 4,
32 msx_audio_flag
= 1 << 5
35 enum { idle_addr
= 0xFFFF };
36 enum { scc_enabled_true
= 0xC000 };
37 enum { mem_size
= 0x10000 };
40 enum { header_size
= 0x20 };
41 enum { header_base_size
= 0x10 };
42 enum { header_ext_size
= header_size
- header_base_size
};
56 // KSSX extended data, if extra_header==0x10
60 byte last_track
[2]; // if no extended data, we set this to 0xFF
80 struct header_t header
;
86 unsigned scc_enabled
; // 0 or 0xC000
89 blip_time_t play_period
;
90 blip_time_t next_play
;
95 int const* voice_types
;
107 unsigned buf_changed_count
;
110 struct M3u_Playlist m3u
;
112 struct setup_t tfilter
;
113 struct Track_Filter track_filter
;
119 struct Multi_Buffer stereo_buf
; // NULL if using custom buffer
122 byte unmapped_read
[0x100];
123 byte unmapped_write
[page_size
];
124 byte ram
[mem_size
+ cpu_padding
];
127 // Basic functionality (see Gme_File.h for file loading/track info functions)
129 void Kss_init( struct Kss_Emu
* this );
130 blargg_err_t
Kss_load_mem( struct Kss_Emu
* this, const void* data
, long size
);
131 blargg_err_t
end_frame( struct Kss_Emu
* this, kss_time_t
);
133 // Set output sample rate. Must be called only once before loading file.
134 blargg_err_t
Kss_set_sample_rate( struct Kss_Emu
* this, int sample_rate
);
136 // Start a track, where 0 is the first track. Also clears warning string.
137 blargg_err_t
Kss_start_track( struct Kss_Emu
* this, int track
);
139 // Generate 'count' samples info 'buf'. Output is in stereo. Any emulation
140 // errors set warning string, and major errors also end track.
141 blargg_err_t
Kss_play( struct Kss_Emu
* this, int count
, sample_t
* buf
);
143 // Track status/control
145 // Number of milliseconds (1000 msec = 1 second) played since beginning of track
146 int Track_tell( struct Kss_Emu
* this );
148 // Seek to new time in track. Seeking backwards or far forward can take a while.
149 blargg_err_t
Track_seek( struct Kss_Emu
* this, int msec
);
152 blargg_err_t
Track_skip( struct Kss_Emu
* this, int n
);
154 // Set start time and length of track fade out. Once fade ends track_ended() returns
155 // true. Fade time can be changed while track is playing.
156 void Track_set_fade( struct Kss_Emu
* this, int start_msec
, int length_msec
);
158 // True if a track has reached its end
159 static inline bool Track_ended( struct Kss_Emu
* this )
161 return track_ended( &this->track_filter
);
164 // Disables automatic end-of-track detection and skipping of silence at beginning
165 static inline void Track_ignore_silence( struct Kss_Emu
* this, bool disable
)
167 this->track_filter
.silence_ignored_
= disable
;
170 // Get track length in milliseconds
171 static inline int Track_get_length( struct Kss_Emu
* this, int n
)
175 if ( (this->m3u
.size
> 0) && (n
< this->m3u
.size
) ) {
176 struct entry_t
* entry
= &this->m3u
.entries
[n
];
177 length
= entry
->length
;
181 length
= 120 * 1000; /* 2 minutes */
186 // Sound customization
188 // Adjust song tempo, where 1.0 = normal, 0.5 = half speed, 2.0 = double speed.
189 // Track length as returned by track_info() assumes a tempo of 1.0.
190 void Sound_set_tempo( struct Kss_Emu
* this, int t
);
192 // Mute/unmute voice i, where voice 0 is first voice
193 void Sound_mute_voice( struct Kss_Emu
* this, int index
, bool mute
);
195 // Set muting state of all voices at once using a bit mask, where -1 mutes them all,
196 // 0 unmutes them all, 0x01 mutes just the first voice, etc.
197 void Sound_mute_voices( struct Kss_Emu
* this, int mask
);
199 // Change overall output amplitude, where 1.0 results in minimal clamping.
200 // Must be called before set_sample_rate().
201 static inline void Sound_set_gain( struct Kss_Emu
* this, int g
)
203 assert( !this->sample_rate
); // you must set gain before setting sample rate
207 // Emulation (You shouldn't touch these
208 void cpu_write( struct Kss_Emu
* this, kss_addr_t
, int );
209 int cpu_in( struct Kss_Emu
* this, kss_time_t
, kss_addr_t
);
210 void cpu_out( struct Kss_Emu
* this, kss_time_t
, kss_addr_t
, int );
212 void cpu_write_( struct Kss_Emu
* this, kss_addr_t addr
, int data
);
213 bool run_cpu( struct Kss_Emu
* this, kss_time_t end
);
214 void jsr( struct Kss_Emu
* this, byte
const addr
[] );
216 static inline int sms_psg_enabled( struct Kss_Emu
* this ) { return this->chip_flags
& sms_psg_flag
; }
217 static inline int sms_fm_enabled( struct Kss_Emu
* this ) { return this->chip_flags
& sms_fm_flag
; }
218 static inline int msx_psg_enabled( struct Kss_Emu
* this ) { return this->chip_flags
& msx_psg_flag
; }
219 static inline int msx_scc_enabled( struct Kss_Emu
* this ) { return this->chip_flags
& msx_scc_flag
; }
220 static inline int msx_music_enabled( struct Kss_Emu
* this ) { return this->chip_flags
& msx_music_flag
;}
221 static inline int msx_audio_enabled( struct Kss_Emu
* this ) { return this->chip_flags
& msx_audio_flag
;}