Kore
Loading...
Searching...
No Matches
audio.c.h
1#include "audio.h"
2
3#include <stdint.h>
4
5#include <kinc/audio2/audio.h>
6#include <kinc/math/core.h>
9#include <kinc/video.h>
10
11#include <assert.h>
12#include <stdlib.h>
13
14static kinc_mutex_t mutex;
15
16#define CHANNEL_COUNT 16
17static kinc_a1_channel_t channels[CHANNEL_COUNT];
18static kinc_a1_stream_channel_t streamchannels[CHANNEL_COUNT];
19static kinc_internal_video_channel_t videos[CHANNEL_COUNT];
20
21static float sampleLinear(int16_t *data, double position) {
22 int pos1 = (int)position;
23 int pos2 = (int)(position + 1);
24 float sample1 = data[pos1] / 32767.0f;
25 float sample2 = data[pos2] / 32767.0f;
26 float a = (float)(position - pos1);
27 return sample1 * (1 - a) + sample2 * a;
28}
29
30static void kinc_a2_on_a1_mix(kinc_a2_buffer_t *buffer, uint32_t samples, void *userdata) {
31 kinc_a1_mix(buffer, samples);
32}
33
34/*float sampleHermite4pt3oX(s16* data, float position) {
35 float s0 = data[(int)(position - 1)] / 32767.0f;
36 float s1 = data[(int)(position + 0)] / 32767.0f;
37 float s2 = data[(int)(position + 1)] / 32767.0f;
38 float s3 = data[(int)(position + 2)] / 32767.0f;
39
40 float x = position - (int)(position);
41
42 // 4-point, 3rd-order Hermite (x-form)
43 float c0 = s1;
44 float c1 = 0.5f * (s2 - s0);
45 float c2 = s0 - 2.5f * s1 + 2 * s2 - 0.5f * s3;
46 float c3 = 0.5f * (s3 - s0) + 1.5f * (s1 - s2);
47 return ((c3 * x + c2) * x + c1) * x + c0;
48}*/
49
50void kinc_a1_mix(kinc_a2_buffer_t *buffer, uint32_t samples) {
51 for (uint32_t i = 0; i < samples; ++i) {
52 float left_value = 0.0f;
53 float right_value = 0.0f;
54#if 0
55 __m128 sseSamples[4];
56 for (int i = 0; i < channelCount; i += 4) {
57 s16 data[4];
58 for (int i2 = 0; i2 < 4; ++i2) {
59 if (channels[i + i2].sound != nullptr) {
60 data[i2] = *(s16*)&channels[i + i2].sound->data[channels[i + i2].position];
61 channels[i + i2].position += 2;
62 if (channels[i + i2].position >= channels[i + i2].sound->size) channels[i + i2].sound = nullptr;
63 }
64 else {
65 data[i2] = 0;
66 }
67 }
68 sseSamples[i / 4] = _mm_set_ps(data[3] / 32767.0f, data[2] / 32767.0f, data[1] / 32767.0f, data[0] / 32767.0f);
69 }
70 __m128 a = _mm_add_ps(sseSamples[0], sseSamples[1]);
71 __m128 b = _mm_add_ps(sseSamples[2], sseSamples[3]);
72 __m128 c = _mm_add_ps(a, b);
73 value = c.m128_f32[0] + c.m128_f32[1] + c.m128_f32[2] + c.m128_f32[3];
74 value = max(min(value, 1.0f), -1.0f);
75#else
76 kinc_mutex_lock(&mutex);
77 for (int i = 0; i < CHANNEL_COUNT; ++i) {
78 if (channels[i].sound != NULL) {
79 left_value += sampleLinear(channels[i].sound->left, channels[i].position) * channels[i].volume * channels[i].sound->volume;
80 left_value = kinc_max(kinc_min(left_value, 1.0f), -1.0f);
81 right_value += sampleLinear(channels[i].sound->right, channels[i].position) * channels[i].volume * channels[i].sound->volume;
82 right_value = kinc_max(kinc_min(right_value, 1.0f), -1.0f);
83
84 channels[i].position += channels[i].pitch / channels[i].sound->sample_rate_pos;
85 // channels[i].position += 2;
86 if (channels[i].position + 1 >= channels[i].sound->size) {
87 if (channels[i].loop) {
88 channels[i].position = 0;
89 }
90 else {
91 channels[i].sound = NULL;
92 }
93 }
94 }
95 }
96 for (int i = 0; i < CHANNEL_COUNT; ++i) {
97 if (streamchannels[i].stream != NULL) {
98 float *samples = kinc_a1_sound_stream_next_frame(streamchannels[i].stream);
99 left_value += samples[0] * kinc_a1_sound_stream_volume(streamchannels[i].stream);
100 left_value = kinc_max(kinc_min(left_value, 1.0f), -1.0f);
101 right_value += samples[1] * kinc_a1_sound_stream_volume(streamchannels[i].stream);
102 right_value = kinc_max(kinc_min(right_value, 1.0f), -1.0f);
103 if (kinc_a1_sound_stream_ended(streamchannels[i].stream)) {
104 streamchannels[i].stream = NULL;
105 }
106 }
107 }
108
109 for (int i = 0; i < CHANNEL_COUNT; ++i) {
110 if (videos[i].stream != NULL) {
111 float *samples = kinc_internal_video_sound_stream_next_frame(videos[i].stream);
112 left_value += samples[0];
113 left_value = kinc_max(kinc_min(left_value, 1.0f), -1.0f);
114 right_value += samples[1];
115 right_value = kinc_max(kinc_min(right_value, 1.0f), -1.0f);
116 if (kinc_internal_video_sound_stream_ended(videos[i].stream)) {
117 videos[i].stream = NULL;
118 }
119 }
120 }
121
122 kinc_mutex_unlock(&mutex);
123#endif
124 assert(buffer->channel_count >= 2);
125 buffer->channels[0][buffer->write_location] = left_value;
126 buffer->channels[1][buffer->write_location] = right_value;
127
128 buffer->write_location += 1;
129 if (buffer->write_location >= buffer->data_size) {
130 buffer->write_location = 0;
131 }
132 }
133}
134
135void kinc_a1_init(void) {
136 for (int i = 0; i < CHANNEL_COUNT; ++i) {
137 channels[i].sound = NULL;
138 channels[i].position = 0;
139 }
140 for (int i = 0; i < CHANNEL_COUNT; ++i) {
141 streamchannels[i].stream = NULL;
142 streamchannels[i].position = 0;
143 }
144 kinc_mutex_init(&mutex);
145
146 kinc_a2_init();
147 kinc_a2_set_callback(kinc_a2_on_a1_mix, NULL);
148}
149
150kinc_a1_channel_t *kinc_a1_play_sound(kinc_a1_sound_t *sound, bool loop, float pitch, bool unique) {
151 kinc_a1_channel_t *channel = NULL;
152 kinc_mutex_lock(&mutex);
153 bool found = false;
154 for (int i = 0; i < CHANNEL_COUNT; ++i) {
155 if (channels[i].sound == sound) {
156 found = true;
157 break;
158 }
159 }
160 if (!found || !unique) {
161 for (int i = 0; i < CHANNEL_COUNT; ++i) {
162 if (channels[i].sound == NULL) {
163 channels[i].sound = sound;
164 channels[i].position = 0;
165 channels[i].loop = loop;
166 channels[i].pitch = pitch;
167 channels[i].volume = 1.0f;
168 channel = &channels[i];
169 break;
170 }
171 }
172 }
173 kinc_mutex_unlock(&mutex);
174 return channel;
175}
176
177void kinc_a1_stop_sound(kinc_a1_sound_t *sound) {
178 kinc_mutex_lock(&mutex);
179 for (int i = 0; i < CHANNEL_COUNT; ++i) {
180 if (channels[i].sound == sound) {
181 channels[i].sound = NULL;
182 channels[i].position = 0;
183 break;
184 }
185 }
186 kinc_mutex_unlock(&mutex);
187}
188
189void kinc_a1_play_sound_stream(kinc_a1_sound_stream_t *stream) {
190 kinc_mutex_lock(&mutex);
191
192 for (int i = 0; i < CHANNEL_COUNT; ++i) {
193 if (streamchannels[i].stream == stream) {
194 streamchannels[i].stream = NULL;
195 streamchannels[i].position = 0;
196 break;
197 }
198 }
199
200 for (int i = 0; i < CHANNEL_COUNT; ++i) {
201 if (streamchannels[i].stream == NULL) {
202 streamchannels[i].stream = stream;
203 streamchannels[i].position = 0;
204 break;
205 }
206 }
207
208 kinc_mutex_unlock(&mutex);
209}
210
211void kinc_a1_stop_sound_stream(kinc_a1_sound_stream_t *stream) {
212 kinc_mutex_lock(&mutex);
213 for (int i = 0; i < CHANNEL_COUNT; ++i) {
214 if (streamchannels[i].stream == stream) {
215 streamchannels[i].stream = NULL;
216 streamchannels[i].position = 0;
217 break;
218 }
219 }
220 kinc_mutex_unlock(&mutex);
221}
222
223void kinc_internal_play_video_sound_stream(struct kinc_internal_video_sound_stream *stream) {
224 kinc_mutex_lock(&mutex);
225 for (int i = 0; i < CHANNEL_COUNT; ++i) {
226 if (videos[i].stream == NULL) {
227 videos[i].stream = stream;
228 videos[i].position = 0;
229 break;
230 }
231 }
232 kinc_mutex_unlock(&mutex);
233}
234
235void kinc_internal_stop_video_sound_stream(struct kinc_internal_video_sound_stream *stream) {
236 kinc_mutex_lock(&mutex);
237 for (int i = 0; i < CHANNEL_COUNT; ++i) {
238 if (videos[i].stream == stream) {
239 videos[i].stream = NULL;
240 videos[i].position = 0;
241 break;
242 }
243 }
244 kinc_mutex_unlock(&mutex);
245}
246
247float kinc_a1_channel_get_volume(kinc_a1_channel_t *channel) {
248 return channel->volume;
249}
250
251void kinc_a1_channel_set_volume(kinc_a1_channel_t *channel, float volume) {
252 KINC_ATOMIC_EXCHANGE_FLOAT(&channel->volume, volume);
253}
Provides atomics aka interlocked operations.
Audio1 is a high-level audio-API that lets you directly play audio-files. Depending on the target-sys...
KINC_FUNC void kinc_a1_init(void)
Initialize the Audio1-API.
Definition audio.c.h:135
Audio2 is a low-level audio-API that allows you to directly provide a stream of audio-samples.
KINC_FUNC void kinc_a2_init(void)
Initializes the Audio2-API.
KINC_FUNC void kinc_a2_set_callback(void(*kinc_a2_audio_callback)(kinc_a2_buffer_t *buffer, uint32_t samples, void *userdata), void *userdata)
Sets the callback that's used to provide audio-samples. This is the primary method of operation for A...
Just a few very simple additions to math.h the C-lib.
Provides mutexes which are used to synchronize threads and uber-mutexes which are used to synchronize...
KINC_FUNC void kinc_mutex_lock(kinc_mutex_t *mutex)
Locks a mutex. A mutex can only be locked from one thread - when other threads attempt to lock the mu...
KINC_FUNC void kinc_mutex_init(kinc_mutex_t *mutex)
Initializes a mutex-object.
KINC_FUNC void kinc_mutex_unlock(kinc_mutex_t *mutex)
Unlocks the mutex which then allows other threads to lock it.
Hardware-assisted video decoding support. Actually supported formats vary per target-system.