Kore
Loading...
Searching...
No Matches
soundstream.c.h
1#include "soundstream.h"
2
3#define STB_VORBIS_HEADER_ONLY
4#include <kinc/libs/stb_vorbis.c>
5
7
8#include <stdlib.h>
9#include <string.h>
10
11static kinc_a1_sound_stream_t streams[256];
12static int nextStream = 0;
13static uint8_t buffer[1024 * 1024 * 10];
14static int bufferIndex;
15
16kinc_a1_sound_stream_t *kinc_a1_sound_stream_create(const char *filename, bool looping) {
17 kinc_a1_sound_stream_t *stream = &streams[nextStream];
18 stream->myLooping = looping;
19 stream->myVolume = 1;
20 stream->rateDecodedHack = false;
21 stream->end = false;
22 kinc_file_reader_t file;
23 kinc_file_reader_open(&file, filename, KINC_FILE_TYPE_ASSET);
24 stream->buffer = &buffer[bufferIndex];
25 bufferIndex += (int)kinc_file_reader_size(&file);
26 uint8_t *filecontent = (uint8_t *)malloc(kinc_file_reader_size(&file));
27 kinc_file_reader_read(&file, filecontent, kinc_file_reader_size(&file));
29 memcpy(stream->buffer, filecontent, kinc_file_reader_size(&file));
30 free(filecontent);
31 stream->vorbis = stb_vorbis_open_memory(buffer, (int)kinc_file_reader_size(&file), NULL, NULL);
32 if (stream->vorbis != NULL) {
33 stb_vorbis_info info = stb_vorbis_get_info(stream->vorbis);
34 stream->chans = info.channels;
35 stream->rate = info.sample_rate;
36 }
37 else {
38 stream->chans = 2;
39 stream->rate = 22050;
40 }
41 ++nextStream;
42 return stream;
43}
44
45int kinc_a1_sound_stream_channels(kinc_a1_sound_stream_t *stream) {
46 return stream->chans;
47}
48
49int kinc_a1_sound_stream_sample_rate(kinc_a1_sound_stream_t *stream) {
50 return stream->rate;
51}
52
53bool kinc_a1_sound_stream_looping(kinc_a1_sound_stream_t *stream) {
54 return stream->myLooping;
55}
56
57void kinc_a1_sound_stream_set_looping(kinc_a1_sound_stream_t *stream, bool loop) {
58 stream->myLooping = loop;
59}
60
61float kinc_a1_sound_stream_volume(kinc_a1_sound_stream_t *stream) {
62 return stream->myVolume;
63}
64
65void kinc_a1_sound_stream_set_volume(kinc_a1_sound_stream_t *stream, float value) {
66 stream->myVolume = value;
67}
68
69bool kinc_a1_sound_stream_ended(kinc_a1_sound_stream_t *stream) {
70 return stream->end;
71}
72
73float kinc_a1_sound_stream_length(kinc_a1_sound_stream_t *stream) {
74 if (stream->vorbis == NULL)
75 return 0;
76 return stb_vorbis_stream_length_in_seconds(stream->vorbis);
77}
78
79float kinc_a1_sound_stream_position(kinc_a1_sound_stream_t *stream) {
80 if (stream->vorbis == NULL)
81 return 0;
82 return stb_vorbis_get_sample_offset(stream->vorbis) / stb_vorbis_stream_length_in_samples(stream->vorbis) * kinc_a1_sound_stream_length(stream);
83}
84
85void kinc_a1_sound_stream_reset(kinc_a1_sound_stream_t *stream) {
86 if (stream->vorbis != NULL)
87 stb_vorbis_seek_start(stream->vorbis);
88 stream->end = false;
89 stream->rateDecodedHack = false;
90}
91
92float *kinc_a1_sound_stream_next_frame(kinc_a1_sound_stream_t *stream) {
93 if (stream->vorbis == NULL) {
94 for (int i = 0; i < stream->chans; ++i) {
95 stream->samples[i] = 0;
96 }
97 return stream->samples;
98 }
99 if (stream->rate == 22050) {
100 if (stream->rateDecodedHack) {
101 stream->rateDecodedHack = false;
102 return stream->samples;
103 }
104 }
105
106 float left, right;
107 float *samples_array[2] = {&left, &right};
108 int read = stb_vorbis_get_samples_float(stream->vorbis, stream->chans, samples_array, 1);
109 if (read == 0) {
110 if (kinc_a1_sound_stream_looping(stream)) {
111 stb_vorbis_seek_start(stream->vorbis);
112 stb_vorbis_get_samples_float(stream->vorbis, stream->chans, samples_array, 1);
113 }
114 else {
115 stream->end = true;
116 for (int i = 0; i < stream->chans; ++i) {
117 stream->samples[i] = 0;
118 }
119 return stream->samples;
120 }
121 }
122
123 stream->samples[0] = samples_array[0][0];
124 stream->samples[1] = samples_array[1][0];
125
126 stream->rateDecodedHack = true;
127 return stream->samples;
128}
Provides an API very similar to fread and friends but handles the intricacies of where files are actu...
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 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 ...
Sound-Streams are decoded while playing and as such are useful for large audio-files like music or sp...