summaryrefslogtreecommitdiff
path: root/apps/codecs/libgme/ay_emu.h
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libgme/ay_emu.h')
-rw-r--r--apps/codecs/libgme/ay_emu.h172
1 files changed, 172 insertions, 0 deletions
diff --git a/apps/codecs/libgme/ay_emu.h b/apps/codecs/libgme/ay_emu.h
new file mode 100644
index 0000000000..91252166e4
--- /dev/null
+++ b/apps/codecs/libgme/ay_emu.h
@@ -0,0 +1,172 @@
1// Sinclair Spectrum AY music file emulator
2
3// Game_Music_Emu 0.6-pre
4#ifndef AY_EMU_H
5#define AY_EMU_H
6
7#include "blargg_source.h"
8
9#include "multi_buffer.h"
10#include "z80_cpu.h"
11#include "ay_apu.h"
12#include "m3u_playlist.h"
13
14typedef short sample_t;
15
16// 64K memory to load code and data into before starting track. Caller
17// must parse the AY file.
18enum { mem_size = 0x10000 };
19enum { ram_addr = 0x4000 }; // where official RAM starts
20enum { buf_size = 2048 };
21
22// AY file header
23enum { header_size = 0x14 };
24struct header_t
25{
26 byte tag [8];
27 byte vers;
28 byte player;
29 byte unused [2];
30 byte author [2];
31 byte comment [2];
32 byte max_track;
33 byte first_track;
34 byte track_info [2];
35};
36
37struct file_t {
38 struct header_t const* header;
39 byte const* tracks;
40 byte const* end; // end of file data
41};
42
43struct mem_t {
44 uint8_t padding1 [0x100];
45 uint8_t ram [mem_size + 0x100];
46};
47
48struct Ay_Emu {
49 struct file_t file;
50
51 struct Blip_Buffer* beeper_output;
52 int beeper_delta;
53 int last_beeper;
54 int beeper_mask;
55
56 addr_t play_addr;
57 cpu_time_t play_period;
58 cpu_time_t next_play;
59
60 int cpc_latch;
61 bool spectrum_mode;
62 bool cpc_mode;
63
64 // general
65 int max_initial_silence;
66 int voice_count;
67 int mute_mask_;
68 double tempo;
69 double gain;
70
71 long sample_rate;
72
73 // track-specific
74 int current_track;
75 int track_count;
76 blargg_long out_time; // number of samples played since start of track
77 blargg_long emu_time; // number of samples emulator has generated since start of track
78 volatile bool track_ended;
79 bool emu_track_ended_; // emulator has reached end of track
80
81 // fading
82 blargg_long fade_start;
83 int fade_step;
84
85 // silence detection
86 bool ignore_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
91
92 long clock_rate_;
93 unsigned buf_changed_count;
94
95 // M3u Playlist
96 struct M3u_Playlist m3u;
97
98 // large items
99 struct Ay_Apu apu;
100 sample_t buf [buf_size];
101 struct Stereo_Buffer stereo_buf; // NULL if using custom buffer
102 struct Z80_Cpu cpu;
103 struct mem_t mem;
104};
105
106// Basic functionality (see Gme_File.h for file loading/track info functions)
107void Ay_init( struct Ay_Emu* this );
108
109blargg_err_t Ay_load_mem( struct Ay_Emu* this, byte const in [], int size );
110
111// Set output sample rate. Must be called only once before loading file.
112blargg_err_t Ay_set_sample_rate( struct Ay_Emu* this, long sample_rate );
113
114// Start a track, where 0 is the first track. Also clears warning string.
115blargg_err_t Ay_start_track( struct Ay_Emu* this, int track );
116
117// Generate 'count' samples info 'buf'. Output is in stereo. Any emulation
118// errors set warning string, and major errors also end track.
119blargg_err_t Ay_play( struct Ay_Emu* this, long count, sample_t* buf );
120
121
122// Track status/control
123
124// Number of milliseconds (1000 msec = 1 second) played since beginning of track
125long Track_tell( struct Ay_Emu* this );
126
127// Seek to new time in track. Seeking backwards or far forward can take a while.
128blargg_err_t Track_seek( struct Ay_Emu* this, long msec );
129
130// Skip n samples
131blargg_err_t Track_skip( struct Ay_Emu* this, long n );
132
133// Set start time and length of track fade out. Once fade ends track_ended() returns
134// true. Fade time can be changed while track is playing.
135void Track_set_fade( struct Ay_Emu* this, long start_msec, long length_msec );
136
137// Get track length in milliseconds
138long Track_get_length( struct Ay_Emu* this, int n );
139
140// Sound customization
141
142// Adjust song tempo, where 1.0 = normal, 0.5 = half speed, 2.0 = double speed.
143// Track length as returned by track_info() assumes a tempo of 1.0.
144void Sound_set_tempo( struct Ay_Emu* this, double t );
145
146// Mute/unmute voice i, where voice 0 is first voice
147void Sound_mute_voice( struct Ay_Emu* this, int index, bool mute );
148
149// Set muting state of all voices at once using a bit mask, where -1 mutes them all,
150// 0 unmutes them all, 0x01 mutes just the first voice, etc.
151void Sound_mute_voices( struct Ay_Emu* this, int mask );
152
153// Change overall output amplitude, where 1.0 results in minimal clamping.
154// Must be called before set_sample_rate().
155static inline void Sound_set_gain( struct Ay_Emu* this, double g )
156{
157 assert( !this->sample_rate ); // you must set gain before setting sample rate
158 this->gain = g;
159}
160
161// Emulation (You shouldn't touch these)
162void cpu_out( struct Ay_Emu* this, cpu_time_t, addr_t, int data );
163void cpu_out_( struct Ay_Emu* this, cpu_time_t, addr_t, int data );
164bool run_cpu( struct Ay_Emu* this, cpu_time_t end );
165
166static inline void disable_beeper( struct Ay_Emu *this )
167{
168 this->beeper_mask = 0;
169 this->last_beeper = 0;
170}
171
172#endif