Kore
Loading...
Searching...
No Matches
sound.c.h
1#include "sound.h"
2
3#define STB_VORBIS_HEADER_ONLY
4#include <kinc/libs/stb_vorbis.c>
5
6#include <kinc/audio2/audio.h>
7#include <kinc/error.h>
9
10#include <assert.h>
11#include <string.h>
12
13struct WaveData {
14 uint16_t audioFormat;
15 uint16_t numChannels;
16 uint32_t sampleRate;
17 uint32_t bytesPerSecond;
18 uint16_t bitsPerSample;
19 uint32_t dataSize;
20 uint8_t *data;
21};
22
23static void checkFOURCC(uint8_t **data, const char *fourcc) {
24 for (int i = 0; i < 4; ++i) {
25 kinc_affirm(**data == fourcc[i]);
26 ++*data;
27 }
28}
29
30static void readFOURCC(uint8_t **data, char *fourcc) {
31 for (int i = 0; i < 4; ++i) {
32 fourcc[i] = **data;
33 ++*data;
34 }
35 fourcc[4] = 0;
36}
37
38static void readChunk(uint8_t **data, struct WaveData *wave) {
39 char fourcc[5];
40 readFOURCC(data, fourcc);
41 uint32_t chunksize = kinc_read_u32le(*data);
42 *data += 4;
43 if (strcmp(fourcc, "fmt ") == 0) {
44 wave->audioFormat = kinc_read_u16le(*data + 0);
45 wave->numChannels = kinc_read_u16le(*data + 2);
46 wave->sampleRate = kinc_read_u32le(*data + 4);
47 wave->bytesPerSecond = kinc_read_u32le(*data + 8);
48 wave->bitsPerSample = kinc_read_u16le(*data + 14);
49 *data += chunksize;
50 }
51 else if (strcmp(fourcc, "data") == 0) {
52 wave->dataSize = chunksize;
53 wave->data = (uint8_t *)malloc(chunksize * sizeof(uint8_t));
54 kinc_affirm(wave->data != NULL);
55 memcpy(wave->data, *data, chunksize);
56 *data += chunksize;
57 }
58 else {
59 *data += chunksize;
60 }
61}
62
63static int16_t convert8to16(uint8_t sample) {
64 return (sample - 127) << 8;
65}
66
67static void splitStereo8(uint8_t *data, int size, int16_t *left, int16_t *right) {
68 for (int i = 0; i < size; ++i) {
69 left[i] = convert8to16(data[i * 2 + 0]);
70 right[i] = convert8to16(data[i * 2 + 1]);
71 }
72}
73
74static void splitStereo16(int16_t *data, int size, int16_t *left, int16_t *right) {
75 for (int i = 0; i < size; ++i) {
76 left[i] = data[i * 2 + 0];
77 right[i] = data[i * 2 + 1];
78 }
79}
80
81static void splitMono8(uint8_t *data, int size, int16_t *left, int16_t *right) {
82 for (int i = 0; i < size; ++i) {
83 left[i] = convert8to16(data[i]);
84 right[i] = convert8to16(data[i]);
85 }
86}
87
88static void splitMono16(int16_t *data, int size, int16_t *left, int16_t *right) {
89 for (int i = 0; i < size; ++i) {
90 left[i] = data[i];
91 right[i] = data[i];
92 }
93}
94
95#define MAXIMUM_SOUNDS 4096
96static kinc_a1_sound_t sounds[MAXIMUM_SOUNDS] = {0};
97
98static kinc_a1_sound_t *find_sound(void) {
99 for (int i = 0; i < MAXIMUM_SOUNDS; ++i) {
100 if (!sounds[i].in_use) {
101 return &sounds[i];
102 }
103 }
104 return NULL;
105}
106
107kinc_a1_sound_t *kinc_a1_sound_create_from_buffer(uint8_t *audio_data, const uint32_t size, kinc_a1_audioformat_t format) {
108 kinc_a1_sound_t *sound = find_sound();
109 assert(sound != NULL);
110 sound->in_use = true;
111 sound->volume = 1.0f;
112 sound->size = 0;
113 sound->left = NULL;
114 sound->right = NULL;
115 // size_t filenameLength = strlen(filename);
116 uint8_t *data = NULL;
117
118 if (format == KINC_A1_AUDIOFORMAT_OGG) {
119 int channels, sample_rate;
120 int samples = stb_vorbis_decode_memory(audio_data, size, &channels, &sample_rate, (short **)&data);
121 kinc_affirm(samples > 0);
122 sound->channel_count = (uint8_t)channels;
123 sound->samples_per_second = (uint32_t)sample_rate;
124 sound->size = samples * 2 * sound->channel_count;
125 sound->bits_per_sample = 16;
126 }
127 else if (format == KINC_A1_AUDIOFORMAT_WAV) {
128 struct WaveData wave = {0};
129 {
130 uint8_t *data = audio_data;
131
132 checkFOURCC(&data, "RIFF");
133 uint32_t filesize = kinc_read_u32le(data);
134 data += 4;
135 checkFOURCC(&data, "WAVE");
136 while (data + 8 - audio_data < (intptr_t)filesize) {
137 readChunk(&data, &wave);
138 }
139 }
140
141 sound->bits_per_sample = (uint8_t)wave.bitsPerSample;
142 sound->channel_count = (uint8_t)wave.numChannels;
143 sound->samples_per_second = wave.sampleRate;
144 data = wave.data;
145 sound->size = wave.dataSize;
146 }
147 else {
148 kinc_affirm(false);
149 }
150
151 if (sound->channel_count == 1) {
152 if (sound->bits_per_sample == 8) {
153 sound->left = (int16_t *)malloc(sound->size * sizeof(int16_t));
154 sound->right = (int16_t *)malloc(sound->size * sizeof(int16_t));
155 splitMono8(data, sound->size, sound->left, sound->right);
156 }
157 else if (sound->bits_per_sample == 16) {
158 sound->size /= 2;
159 sound->left = (int16_t *)malloc(sound->size * sizeof(int16_t));
160 sound->right = (int16_t *)malloc(sound->size * sizeof(int16_t));
161 splitMono16((int16_t *)data, sound->size, sound->left, sound->right);
162 }
163 else {
164 kinc_affirm(false);
165 }
166 }
167 else {
168 // Left and right channel are in s16 audio stream, alternating.
169 if (sound->bits_per_sample == 8) {
170 sound->size /= 2;
171 sound->left = (int16_t *)malloc(sound->size * sizeof(int16_t));
172 sound->right = (int16_t *)malloc(sound->size * sizeof(int16_t));
173 splitStereo8(data, sound->size, sound->left, sound->right);
174 }
175 else if (sound->bits_per_sample == 16) {
176 sound->size /= 4;
177 sound->left = (int16_t *)malloc(sound->size * sizeof(int16_t));
178 sound->right = (int16_t *)malloc(sound->size * sizeof(int16_t));
179 splitStereo16((int16_t *)data, sound->size, sound->left, sound->right);
180 }
181 else {
182 kinc_affirm(false);
183 }
184 }
185 sound->sample_rate_pos = kinc_a2_samples_per_second() / (float)sound->samples_per_second;
186 free(data);
187
188 return sound;
189}
190
191kinc_a1_sound_t *kinc_a1_sound_create(const char *filename) {
192 size_t filenameLength = strlen(filename);
193
194 kinc_a1_audioformat_t fileformat;
195 if (strncmp(&filename[filenameLength - 4], ".ogg", 4) == 0) {
196 fileformat = KINC_A1_AUDIOFORMAT_OGG;
197 }
198 else if (strncmp(&filename[filenameLength - 4], ".wav", 4) == 0) {
199 fileformat = KINC_A1_AUDIOFORMAT_WAV;
200 }
201 else {
202 assert(false);
203 }
204
205 kinc_file_reader_t file;
206 if (!kinc_file_reader_open(&file, filename, KINC_FILE_TYPE_ASSET)) {
207 return NULL;
208 }
209
210 uint8_t *filedata = (uint8_t *)malloc(kinc_file_reader_size(&file));
211 kinc_file_reader_read(&file, filedata, kinc_file_reader_size(&file));
213 size_t filesize = kinc_file_reader_size(&file);
214
215 kinc_a1_sound_t *sound = kinc_a1_sound_create_from_buffer(filedata, (uint32_t)filesize, fileformat);
216
217 free(filedata);
218
219 return sound;
220}
221
222void kinc_a1_sound_destroy(kinc_a1_sound_t *sound) {
223 free(sound->left);
224 free(sound->right);
225 sound->left = NULL;
226 sound->right = NULL;
227 sound->in_use = false;
228}
229
230float kinc_a1_sound_volume(kinc_a1_sound_t *sound) {
231 return sound->volume;
232}
233
234void kinc_a1_sound_set_volume(kinc_a1_sound_t *sound, float value) {
235 sound->volume = value;
236}
Audio2 is a low-level audio-API that allows you to directly provide a stream of audio-samples.
KINC_FUNC uint32_t kinc_a2_samples_per_second(void)
The current sample-rate of the system.
Contains functionality to stop the program in case of an error and create a user-visible error messag...
KINC_FUNC void kinc_affirm(bool condition)
Exits the program when a condition is untrue and shows a generic error message.
Provides an API very similar to fread and friends but handles the intricacies of where files are actu...
KINC_FUNC uint16_t kinc_read_u16le(uint8_t *data)
Interprets two bytes starting at the provided pointer as a little-endian uint16.
KINC_FUNC bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filepath, int type)
Opens a file for reading.
KINC_FUNC size_t kinc_file_reader_size(kinc_file_reader_t *reader)
Figures out the size of a file.
KINC_FUNC bool kinc_file_reader_close(kinc_file_reader_t *reader)
Closes a file.
KINC_FUNC uint32_t kinc_read_u32le(uint8_t *data)
Interprets four bytes starting at the provided pointer as a little-endian uint32.
KINC_FUNC size_t kinc_file_reader_read(kinc_file_reader_t *reader, void *data, size_t size)
Reads data from a file starting from the current reading-position and increases the reading-position ...
Sounds are pre-decoded on load and therefore primarily useful for playing sound-effects.
Definition sound.c.h:13