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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
use core::ffi::c_void;

pub const AUDIO_VOLUME_MAX: u32 = 0x8000;
pub const AUDIO_CHANNEL_MAX: u32 = 8;
pub const AUDIO_NEXT_CHANNEL: i32 = -1;
pub const AUDIO_SAMPLE_MIN: u32 = 64;
pub const AUDIO_SAMPLE_MAX: u32 = 65472;

#[repr(u32)]
pub enum AudioFormat {
    /// Channel set to stereo output
    Stereo = 0,
    /// Channel set to mono output
    Mono = 0x10,
}

#[repr(C)]
pub struct AudioInputParams {
    /// Unknown. Pass 0
    pub unknown1: i32,
    pub gain: i32,
    /// Unknown. Pass 0
    pub unknown2: i32,
    /// Unknown. Pass 0
    pub unknown3: i32,
    /// Unknown. Pass 0
    pub unknown4: i32,
    /// Unknown. Pass 0
    pub unknown5: i32,

}

#[repr(i32)]
pub enum AudioOutputFrequency {
    Khz48 = 48000,
    Khz44_1 = 44100,
    Khz32 = 32000,
    Khz24 = 24000,
    Khz22_05 = 22050,
    Khz16 = 16000,
    Khz12 = 12000,
    Khz11_025 = 11025,
    Khz8 = 8000,
}

#[repr(i32)]
pub enum AudioInputFrequency {
    Khz44_1 = 44100,
    Khz22_05 = 22050,
    Khz11_025 = 11025,
}

/// Make the given sample count a multiple of 64.
pub const fn audio_sample_align(sample_count:i32) -> i32 {
    (sample_count + 63) & !63
}

psp_extern! {
    #![name = "sceAudio"]
    #![flags = 0x4001]
    #![version = (0, 0)]

    #[psp(0x5EC81C55)]
    /// Allocate and initialize a hardware output channel.
    ///
    /// # Parameters
    ///
    /// - `channel`: Use a value between 0-7 to reserve a specific channel. Pass
    ///   `AUDIO_NEXT_CHANNEL` to get the first available channel.
    /// - `sample_count`: The number of samples that can be output on the channel
    ///   per output call. It must be a value between `AUDIO_SAMPLE_MIN` and
    ///   `AUDIO_SAMPLE_MAX`, and it must be aligned to 64 bytes. Use
    ///   `audio_sample_align()` to align it.
    /// - `format`: The output format to use for the channel. One of `AudioFormat`.
    ///
    /// # Return value
    ///
    /// The channel number on success, or <0 on error.
    pub fn sceAudioChReserve(channel: i32, sample_count: i32, format: AudioFormat) -> i32;

    #[psp(0x6FC46853)]
    /// Release a hardware output channel.
    ///
    /// # Parameters
    ///
    /// - `channel`: The channel to release.
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioChRelease(channel: i32) -> i32;

    #[psp(0x8C1009B2)]
    /// Output audio to the specified channel.
    ///
    /// # Parameters
    ///
    /// - `channel`: The channel number.
    /// - `vol`: The volume.
    /// - `buf`: Pointer to PCM data to output
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioOutput(channel: i32, vol: i32, buf: *mut c_void) -> i32;

    #[psp(0x136CAF51)]
    /// Output audio to the specified channel (blocking)
    ///
    /// # Parameters
    ///
    /// - `channel`: The channel number.
    /// - `vol`: The volume.
    /// - `buf`: Pointer to PCM data to output
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.

    pub fn sceAudioOutputBlocking(channel: i32, vol: i32, buf: *mut c_void) -> i32;

    #[psp(0xE2D56B2D)]
    /// Output panned audio to the specified channel.
    ///
    /// # Parameters
    ///
    /// - `channel`: The channel number.
    /// - `left_vol`: The left volume.
    /// - `right_vol`: The right volume.
    /// - `buf` Pointer to PCM data to output
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioOutputPanned(channel: i32, left_vol: i32, right_vol: i32, buf: *mut c_void) -> i32;

    #[psp(0x13F592BC)]
    /// Output panned audio to the specified channel (blocking)
    ///
    /// # Parameters
    ///
    /// - `channel`: The channel number.
    /// - `left_vol`: The left volume.
    /// - `right_vol`: The right volume.
    /// - `buf`: Pointer to PCM data to output
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.

    pub fn sceAudioOutputPannedBlocking(channel: i32, left_vol: i32, right_vol: i32, buf: *mut c_void) -> i32;

    #[psp(0xE9D97901)]
    /// Get count of uplayed samples remaining
    ///
    /// # Parameters
    ///
    /// - `channel`: The channel number.
    ///
    /// # Return value
    ///
    /// Number of samples to be played, <0 on error.
    pub fn sceAudioGetChannelRestLen(channel: i32) -> i32;

    #[psp(0xB011922F)]
    /// Get count of uplayed samples remaining
    ///
    /// # Parameters
    ///
    /// - `channel`: The channel number.
    ///
    /// # Return value
    ///
    /// Number of samples to be played, <0 on error.
    pub fn sceAudioGetChannelRestLength(channel: i32) -> i32;

    #[psp(0xCB2E439E)]
    /// Change the output sample count, after it's already been reserved
    ///
    /// # Parameters
    ///
    /// - `channel`: The channel number.
    /// - `sample_count`: The number of samples to output in one output call.
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioSetChannelDataLen(channel: i32, sample_count: i32) -> i32;

    #[psp(0x95FD0C2D)]
    /// Change the format of a channel
    ///
    /// # Parameters
    ///
    /// - `channel`: The channel number.
    /// - `format`: One of `AudioFormat`.
    ///
    /// # Return value
    ///
    /// 0 on success, < 0 on error.
    pub fn sceAudioChangeChannelConfig(channel: i32, format: AudioFormat) -> i32;

    #[psp(0xB7E1D8E7)]
    /// Change the volume of a channel
    ///
    /// # Parameters
    ///
    /// - `channel`: The channel number.
    /// - `left_vol`: The left volume.
    /// - `right_vol`: The right volume.
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioChangeChannelVolume(channel:i32, left_vol: i32, right_vol:i32) -> i32;

    #[psp(0x01562BA3)]
    /// Reserve the audio output and set the sample count
    ///
    /// # Parameters
    ///
    /// - `sample_count`: The number of samples to output in one output call (min 17, max 4111).
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioOutput2Reserve(sample_count: i32) -> i32;

    #[psp(0x43196845)]
    /// Release the audio output
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioOutput2Release() -> i32;

    #[psp(0x63F2889C)]
    /// Change the output sample count, after it's already been reserved
    ///
    /// # Parameters
    ///
    /// - `sample_count`: The number of samples to output in one output call (min 17, max 4111)
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioOutput2ChangeLength(sample_count: i32) -> i32;

    #[psp(0x2D53F36E)]
    /// Output audio (blocking)
    ///
    /// # Parameters
    ///
    /// - `vol`: The volume.
    /// - `buf`: Pointer to PCM data.
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioOutput2OutputBlocking(vol: i32, buf: *mut c_void) -> i32;

    #[psp(0x647CEF33)]
    /// Get count of unplayed samples remaining
    ///
    /// # Return value
    ///
    /// Number of samples to be played, < 0 on error.
    pub fn sceAudioOutput2GetRestSample() -> i32;

    #[psp(0x38553111)]
    /// Reserve the audio output
    ///
    /// # Parameters
    ///
    /// - `sample_count`: The number of samples to output in one output call (min 17, max 4111).
    /// - `freq`: One of `AudioOutputFrequency`.
    /// - `channels`: Number of channels. Pass 2 (stereo).
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioSRCChReserve(sample_count: i32, freq: AudioOutputFrequency, channels: i32) -> i32;

    #[psp(0x5C37C0AE)]
    /// Release the audio output
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioSRCChRelease() -> i32;

    #[psp(0xE0727056)]
    /// Output audio (blocking)
    ///
    /// # Parameters
    ///
    /// - `vol`: The volume.
    /// - `buf`: Pointer to PCM data.
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioSRCOutputBlocking(vol: i32, buf: *mut c_void) -> i32;

    #[psp(0x7DE61688)]
    /// Init audio input
    ///
    /// # Parameters
    ///
    /// - `unknown1`: Unknown. Pass 0.
    /// - `gain`: Gain.
    /// - `unknown2`: Unknown. Pass 0.
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioInputInit(unknown1: i32, gain: i32, unknown2: i32) -> i32;

    #[psp(0xE926D3FB)]
    /// Init audio input (with extra arguments)
    ///
    /// # Parameters
    ///
    /// - `params`: A pointer to an `AudioInputParams` struct.
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioInputInitEx(params: *mut AudioInputParams) -> i32;

    #[psp(0x086E5895)]
    /// Perform audio input (blocking)
    ///
    /// # Parameters
    ///
    /// - `sample_count`: Number of samples.
    /// - `freq`: One of `AudioInputFrequency`.
    /// - `buf`: Pointer to where the audio data will be stored.
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioInputBlocking(sample_count: i32, freq: AudioInputFrequency, buf: *mut c_void);

    #[psp(0x6D4BEC68)]
    /// Perform audio input
    ///
    /// # Parameters
    ///
    /// - `sample_count`: Number of samples.
    /// - `freq`: One of `AudioInputFrequency`.
    /// - `buf`: Pointer to where the audio data will be stored.
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioInput(sample_count: i32, freq: AudioInputFrequency, buf: *mut c_void);

    #[psp(0xA708C6A6)]
    /// Get the number of samples that were acquired
    ///
    /// # Return value
    ///
    /// Number of samples acquired, <0 on error.
    pub fn sceAudioGetInputLength() -> i32;

    #[psp(0x87B2E651)]
    /// Wait for non-blocking audio input to complete
    ///
    /// # Return value
    ///
    /// 0 on success, <0 on error.
    pub fn sceAudioWaitInputEnd() -> i32;

    #[psp(0xA633048E)]
    /// Poll for non-blocking audio input status
    ///
    /// # Return value
    ///
    /// 0 if input has completed, 1 if not completed, <0 on error.
    pub fn sceAudioPollInputEnd() -> i32;
}