aboutsummaryrefslogtreecommitdiff
path: root/doc/api-c.txt
blob: 2c879efe83f14cf335bfca30bcdb2af8adc1a894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
=============================================================================

*** How to use ***

Add "echo.c" and "echoblob.h" in your program files. Then include the header
"echo.h" in whatever source files you need to access the Echo API, i.e.

   #include "echo.h"

(these files are present in the "c" directory)

Then use echo_init to initialize Echo and load the instrument list (see
below). After that you can use Echo as needed (e.g. call echo_play_bgm to
start playing music, etc.).

The file "echoblob.h" is the Z80 binary turned into a C array. If you want to
change the blob, just use the included blob2c tool. It's invoked as follows:

   blob2c «input.bin» «output.h»

=============================================================================

*** Initialization ***

void echo_init(const void* const* list)

   Initializes Echo. Loads the instrument list, loads the Z80 engine and gets
   it running. You need to call this before you can use Echo (usually when
   the program is just starting).
   
   The parameter 'list' is a pointer to an array of pointers, where each
   entry points to the EIF/EEF/EWF data of each instrument. The list ends
   with a NULL pointer. For example:
   
   const void* const list[] = {
      instrument1,
      instrument2,
      instrument3,
      NULL
   };
   
   (if NULL isn't defined for whatever reason just use 0 instead)

=============================================================================

*** Background music ***

void echo_play_bgm(const void *esf)

   Starts playback of the specified background music. The parameter 'esf'
   points to the ESF data for the background music.

void echo_stop_bgm()

   Stops playback of background music. Used both to stop and to pause music.

void echo_pause_bgm()

   Pauses BGM playback (SFXs should be unaffected).

void echo_resume_bgm()

   Resumes BGM playback after it has been paused with echo_pause_bgm.

=============================================================================

*** Sound effects ***

void echo_play_sfx(const void *esf)

   Starts playback of the specified sound effect. The parameter 'esf' points
   to the ESF data for the sound effect.

void echo_stop_sfx()

   Stops playback of sound effects.

=============================================================================

*** Direct events ***

void echo_play_direct(const void *esf)

   Injects events to be played as part of the BGM the next tick. The
   parameter 'esf' points to the ESF data to be injected.

   The injected events are a small stream on their own. The last event must
   be $FF (this will return back to the BGM). Do *not* issue $FC, $FD or $FE
   events, as you'll just break everything instead.

   The buffer is small, so don't go overboard. There's room for up to 128
   bytes (though again, each event is just 2-3 bytes). If there were direct
   events pending to play, the new events will be appended at the end, so
   take this into account when it comes to the buffer usage. You can check
   if there are pending events with Echo_GetStatus (see ECHO_STAT_DIRBUSY)
   if you're worried about running out of space.
   
   The buffer is only checked every tick.

=============================================================================

*** Control ***

uint16_t echo_get_status()

   Gets the current status of Echo. Returns an OR of the following flags,
   as relevant:
   
   ECHO_STAT_BGM ....... Background music is playing
   ECHO_STAT_SFX ....... Sound effect is playing
   ECHO_STAT_DIRBUSY ... Echo isn't done parsing direct events
   ECHO_STAT_BUSY ...... Echo is busy (can't take commands)
   
   The API will automatically wait if you try to send a command while Echo is
   busy, so the only reason to check for that is if you don't want to halt
   the 68000 until Echo is ready to take more commands.

void echo_set_volume(uint8_t vol)

   Sets the global volume. The value 'vol' ranges from 0 (quietest) to 255
   (loudest), and every channel is affected immediately. The scale of the
   volume in this case is *linear*.
   
   Note that since PCM doesn't have volume, it gets toggled on/off depending
   on the volume value (the cut off point is at 25%).

void echo_set_volume_ex(const uint8_t *ptr)

   Sets the global volume for each channel separately. The parameter 'ptr'
   points to a list of 16 bytes (one for each Echo channel). Values for FM
   and PSG channels are given in the same way as in events, that is:
   logarithmic scale, 0..127 for FM, 0..15 for PSG, lower = louder.
   
   The last byte (the one belonging to the PCM channel) is used to toggle
   whether PCM plays, either 0 (disabled) or 1 (enabled).
   
   NOTE: the Echo 1.4 docs requested for 13 bytes instead of 16. This has
   been changed for the sake of expansion. Currently the extra bytes are
   ignored, but consider adapting your code (just set them to zero).

const uint8_t echo_fm_vol_table[]
const uint8_t echo_psg_vol_table[]
    These two are not subroutines but rather look-up tables. They have 64
    byte-sized entries and they're used to convert a linear volume value into
    a hardware volume value (e.g. for echo_set_volume_ex).
    
    To give an idea of how to use these: take what you'd pass as argument to
    echo_set_volume divided by 4 (shift right by 2), then use it as an index
    to these arrays. The byte will be the volume as the hardware (or
    echo_set_volume_ex) wants it.

=============================================================================

*** Settings ***

void echo_set_pcm_rate(uint8_t rate)

   Changes the sample rate of PCM. Note this is a global parameter as it
   affects both BGM and SFX. The value is what one would write in timer A of
   the YM2612 register. Here are the approximate frequencies for some values
   (default is 0x04):
   
               NTSC         PAL |            NTSC        PAL
   -----------------------------|---------------------------
   0x01 ... 26632Hz ... 26389Hz | 0x07 ... 6658Hz ... 6597Hz
   0x02 ... 17755Hz ... 17593Hz | 0x08 ... 5918Hz ... 5864Hz
   0x03 ... 13316Hz ... 13194Hz | 0x09 ... 5326Hz ... 5278Hz
   0x04 ... 10653Hz ... 10556Hz | 0x0A ... 4842Hz ... 4798Hz
   0x05 .... 8877Hz .... 8796Hz | 0x0B ... 4439Hz ... 4398Hz
   0x06 .... 7609Hz .... 7539Hz | 0x0C ... 4097Hz ... 4060Hz
   
   The higher the sample rate, the better quality, but also takes up more
   space and, more importantly, reduces CPU time available for other things
   (which can hamper Echo's ability to process complex streams). Be careful
   if you increase the sample rate.

void echo_set_stereo(int enable)

   Toggles whether sound is forced to mono (enable == 0) or if stereo panning
   works (enable != 0). Will take effect for all following panning events.
   Can be used to implement a mono/stereo toggle in games.
   
   By default Echo is in stereo mode.

=============================================================================

*** Raw access ***

void echo_send_command(uint8_t command)

   Sends an argument-less command to Echo. The parameter 'command' is the
   command to send, and may be one of the following:
   
   ECHO_CMD_STOPBGM ..... Stop background music playback
   ECHO_CMD_PAUSEBGM .... Pause background music playback
   ECHO_CMD_RESUMEBGM ... Resume background music playback
   ECHO_CMD_STOPSFX ..... Stop sound effect playback

void echo_send_command_addr(uint8_t command, const void *address)

   Sends a command to Echo that takes an address as its argument. The
   parameter 'command' is the command to send, while the parameter 'address'
   is the address to use as argument. The command may be one of these:
   
   ECHO_CMD_PLAYBGM .... Start background music playback
   ECHO_CMD_PLAYSFX .... Start sound effect playback
   ECHO_CMD_LOADLIST ... Load instrument list (warning: see below)
   
   Do *NOT* use ECHO_CMD_LOADLIST unless you *REALLY* know you're doing, this
   makes Echo load the instrument list by itself and it expects a different
   format from the one used by the C API.

void echo_send_command_byte(uint8_t command, uint8_t byte)

   Sends a command to Echo that takes a byte as its argument. The parameter
   'command' is the command to send, while the parameter 'byte' is the byte
   to use as argument. The command may be... just this for now:
   
   ECHO_CMD_SETPCMRATE ... Change PCM sample rate

=============================================================================