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
|
/* SPDX-License-Identifier: MIT */
/*
* konaudio.h
*
* Copyright (c) 2026 KonAki
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef KONAUDIO_H
#define KONAUDIO_H
#include <stdint.h>
#include "konassets.h"
/*** definition ***/
#define KON_AUDIO_MAX_PLAYING 64
#define KON_AUDIO_BUSES 4
/* buses are just groups with their own volume. what they mean is up to you */
#define KON_BUS_SFX 0
#define KON_BUS_MUSIC 1
#define KON_BUS_VOICE 2
#define KON_BUS_UI 3
typedef struct kon_audio kon_audio_t;
typedef uint32_t kon_audioHandle_t; /* 0 means nothing is playing */
typedef struct kon_audioParams {
float volume; /* 1 */
float pan; /* -1 left to 1 right, ignored when positional */
float pitch; /* 1 is normal, 2 is an octave up and twice as fast */
int loop;
uint64_t loopStart; /* frame to jump back to */
uint64_t loopEnd; /* frame to jump from, 0 means the end of the sound */
int bus;
int positional;
float x, y, z;
float minDistance; /* full volume inside this */
float maxDistance; /* silent outside this */
} kon_audioParams_t;
/* opens the default output device. the device decides the real rate, see kon_audioRate */
kon_audio_t *kon_audioCreate(uint32_t sampleRate);
/* no device. you pull the mix yourself with kon_audioMix, for tests or rendering to a file */
kon_audio_t *kon_audioCreateOffline(uint32_t sampleRate);
void kon_audioDestroy(kon_audio_t *audio);
uint32_t kon_audioRate(const kon_audio_t *audio);
/*** playing ***/
/* volume 1, pan 0, pitch 1, sfx bus, positional range 1 to 20 */
kon_audioParams_t kon_audioParamsDefault(void);
/* pan goes from -1 (left) to 1 (right). returns 0 when everything is busy.
the sound has to stay alive until it is done or stopped, see kon_audioStopSound */
kon_audioHandle_t kon_audioPlay(kon_audio_t *audio, const kon_sound_t *sound, float volume, float pan, int loop);
kon_audioHandle_t kon_audioPlayEx(kon_audio_t *audio, const kon_sound_t *sound, const kon_audioParams_t *params);
/* shortcut for a sound in the world, use mono sounds for these */
kon_audioHandle_t kon_audioPlayAt(kon_audio_t *audio, const kon_sound_t *sound, float volume, float x, float y, float z);
void kon_audioStop(kon_audio_t *audio, kon_audioHandle_t handle);
void kon_audioStopSound(kon_audio_t *audio, const kon_sound_t *sound);
void kon_audioStopAll(kon_audio_t *audio);
int kon_audioIsPlaying(kon_audio_t *audio, kon_audioHandle_t handle);
int kon_audioPlayingCount(kon_audio_t *audio);
/* all of these do nothing on a handle that is already finished */
void kon_audioHandleSetVolume(kon_audio_t *audio, kon_audioHandle_t handle, float volume);
void kon_audioHandleSetPan(kon_audio_t *audio, kon_audioHandle_t handle, float pan);
void kon_audioHandleSetPitch(kon_audio_t *audio, kon_audioHandle_t handle, float pitch);
void kon_audioHandleSetPosition(kon_audio_t *audio, kon_audioHandle_t handle, float x, float y, float z);
/* moves the handle's own fade to target (0 to 1) over some seconds. stopAtEnd ends it once it arrives */
void kon_audioHandleFade(kon_audio_t *audio, kon_audioHandle_t handle, float target, float seconds, int stopAtEnd);
/*** mixing ***/
void kon_audioSetVolume(kon_audio_t *audio, float volume);
void kon_audioSetBusVolume(kon_audio_t *audio, int bus, float volume);
/* the ears for positional sounds. forward and up do not have to be normalised.
starts at the origin looking down -z with y up, so +x is right */
void kon_audioSetListener(kon_audio_t *audio, float x, float y, float z, float fx, float fy, float fz, float ux, float uy, float uz);
/* biggest sample of the last mix, after master volume. for meters */
float kon_audioPeak(kon_audio_t *audio);
/* mixes interleaved stereo float frames. the device calls this, offline you call it */
void kon_audioMix(kon_audio_t *audio, float *out, uint32_t frames);
/*** music ***/
/* one looping track on the music bus. a new sound crossfades from the old one.
params can be NULL, loop and bus are always set for you. asking for the track that already plays does nothing */
kon_audioHandle_t kon_audioMusic(kon_audio_t *audio, const kon_sound_t *sound, const kon_audioParams_t *params, float fadeSeconds);
void kon_audioMusicStop(kon_audio_t *audio, float fadeSeconds);
const kon_sound_t *kon_audioMusicCurrent(kon_audio_t *audio);
#endif
|