2015-08-02 12:12:44 +00:00
|
|
|
#ifndef DEPENDENCY_AUDIO_DECODE
|
2022-05-06 04:02:21 +00:00
|
|
|
// Stubs:
|
2015-08-02 12:12:44 +00:00
|
|
|
//(none required)
|
|
|
|
#else
|
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
# define DEPENDENCY_AUDIO_DECODE_OGG
|
|
|
|
# define DEPENDENCY_AUDIO_DECODE_MP3
|
|
|
|
# define DEPENDENCY_AUDIO_DECODE_WAV
|
|
|
|
|
|
|
|
# ifdef QB64_BACKSLASH_FILESYSTEM
|
|
|
|
# ifdef DEPENDENCY_AUDIO_DECODE_MP3
|
|
|
|
# include "mp3_mini\\src.c"
|
|
|
|
# endif
|
|
|
|
# ifdef DEPENDENCY_AUDIO_DECODE_WAV
|
|
|
|
# include "wav\\src.c"
|
|
|
|
# endif
|
|
|
|
# ifdef DEPENDENCY_AUDIO_DECODE_OGG
|
|
|
|
# include "ogg\\src.c"
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
# ifdef DEPENDENCY_AUDIO_DECODE_MP3
|
|
|
|
# include "mp3_mini/src.c"
|
|
|
|
# endif
|
|
|
|
# ifdef DEPENDENCY_AUDIO_DECODE_WAV
|
|
|
|
# include "wav/src.c"
|
|
|
|
# endif
|
|
|
|
# ifdef DEPENDENCY_AUDIO_DECODE_OGG
|
|
|
|
# include "ogg/src.c"
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
# include <string.h>
|
|
|
|
// forward refs:
|
|
|
|
void sub__sndvol(int32 handle, float volume);
|
2015-08-02 12:12:44 +00:00
|
|
|
void sub__sndclose(int32 handle);
|
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
int32 func__sndopen(qbs *filename, qbs *requirements, int32 passed) {
|
2017-08-06 03:37:54 +00:00
|
|
|
sndsetup();
|
2022-05-06 04:02:21 +00:00
|
|
|
if (new_error)
|
|
|
|
return 0;
|
2017-08-06 03:37:54 +00:00
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
static qbs *s1 = NULL;
|
|
|
|
if (!s1)
|
|
|
|
s1 = qbs_new(0, 0);
|
|
|
|
qbs_set(s1, qbs_add(filename, qbs_new_txt_len("\0", 1))); // s1=filename+CHR$(0)
|
2015-08-02 12:12:44 +00:00
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
// load file
|
|
|
|
if (s1->len == 1)
|
|
|
|
return 0; // return invalid handle if null length string
|
|
|
|
static int32 fh, result;
|
2017-08-06 03:37:54 +00:00
|
|
|
static int64 lof;
|
2022-05-06 04:02:21 +00:00
|
|
|
fh = gfs_open(s1, 1, 0, 0);
|
|
|
|
if (fh < 0)
|
|
|
|
return 0;
|
|
|
|
lof = gfs_lof(fh);
|
|
|
|
static uint8 *content;
|
|
|
|
content = (uint8 *)malloc(lof);
|
|
|
|
if (!content) {
|
|
|
|
gfs_close(fh);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
result = gfs_read(fh, -1, content, lof);
|
2017-08-06 03:37:54 +00:00
|
|
|
gfs_close(fh);
|
2022-05-06 04:02:21 +00:00
|
|
|
if (result < 0) {
|
|
|
|
free(content);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-08-06 03:37:54 +00:00
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
// identify file format
|
2017-08-06 03:37:54 +00:00
|
|
|
static snd_sequence_struct *seq;
|
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
// OGG?
|
|
|
|
# ifdef DEPENDENCY_AUDIO_DECODE_OGG
|
|
|
|
if (lof >= 3) {
|
|
|
|
if (content[0] == 79) {
|
|
|
|
if (content[1] == 103) {
|
|
|
|
if (content[2] == 103) { //"Ogg"
|
|
|
|
seq = snd_decode_ogg(content, lof);
|
|
|
|
goto got_seq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // 3
|
|
|
|
# endif
|
|
|
|
|
|
|
|
// WAV?
|
|
|
|
# ifdef DEPENDENCY_AUDIO_DECODE_WAV
|
|
|
|
if (lof >= 12) {
|
|
|
|
if ((*(uint32 *)&content[8]) == 0x45564157) { // WAVE
|
|
|
|
seq = snd_decode_wav(content, lof);
|
2017-08-06 03:37:54 +00:00
|
|
|
goto got_seq;
|
2022-05-06 04:02:21 +00:00
|
|
|
} // WAVE
|
2017-08-06 03:37:54 +00:00
|
|
|
}
|
2022-05-06 04:02:21 +00:00
|
|
|
# endif
|
2015-08-02 12:12:44 +00:00
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
// assume mp3!
|
|
|
|
// MP3?
|
|
|
|
# ifdef DEPENDENCY_AUDIO_DECODE_MP3
|
|
|
|
seq = snd_decode_mp3(content, lof);
|
|
|
|
# endif
|
2015-08-02 12:12:44 +00:00
|
|
|
|
|
|
|
got_seq:
|
2017-08-06 03:37:54 +00:00
|
|
|
free(content);
|
2022-05-06 04:02:21 +00:00
|
|
|
if (seq == NULL)
|
|
|
|
return 0;
|
2015-08-02 12:12:44 +00:00
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
// convert sequence (includes sample rate conversion etc etc)
|
2017-08-06 03:37:54 +00:00
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
// just perform sample_rate fix for now...
|
2017-08-06 03:37:54 +00:00
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
// 1. 8->16bit conversion and/or edian conversion
|
2017-08-06 03:37:54 +00:00
|
|
|
static int32 incorrect_format;
|
2022-05-06 04:02:21 +00:00
|
|
|
incorrect_format = 0;
|
|
|
|
if (seq->bits_per_sample != 16)
|
|
|
|
incorrect_format = 1;
|
|
|
|
if (seq->is_unsigned)
|
|
|
|
incorrect_format = 1;
|
|
|
|
// todo... if (seq->endian==???)
|
|
|
|
|
|
|
|
// this section does not fix the frequency, only the bits per sample
|
|
|
|
// and signed-ness of the data
|
|
|
|
if (incorrect_format) {
|
|
|
|
static int32 bps;
|
|
|
|
bps = seq->bits_per_sample / 8;
|
|
|
|
static int32 samples;
|
|
|
|
samples = seq->data_size / bps;
|
2017-08-06 03:37:54 +00:00
|
|
|
static uint8 *new_data;
|
2022-05-06 04:02:21 +00:00
|
|
|
if (bps != 2) {
|
|
|
|
new_data = (uint8 *)malloc(samples * 2);
|
|
|
|
} else {
|
|
|
|
new_data = (uint8 *)seq->data;
|
2017-08-06 03:37:54 +00:00
|
|
|
}
|
2022-05-06 04:02:21 +00:00
|
|
|
static int32 i, v;
|
|
|
|
for (i = 0; i < samples; i++) {
|
|
|
|
// read original value
|
|
|
|
v = 0;
|
|
|
|
if (bps == 1) {
|
|
|
|
if (seq->is_unsigned) {
|
|
|
|
v = *(uint8 *)(seq->data + i * 1);
|
|
|
|
v = (v - 128) * 256;
|
|
|
|
} else {
|
|
|
|
v = *(int8 *)(seq->data + i * 1);
|
|
|
|
v = v * 128;
|
2017-08-06 03:37:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-06 04:02:21 +00:00
|
|
|
if (bps == 2) {
|
|
|
|
if (seq->is_unsigned) {
|
|
|
|
v = *(uint16 *)(seq->data + i * 2);
|
|
|
|
v = v - 32768;
|
|
|
|
} else {
|
|
|
|
v = *(int16 *)(seq->data + i * 2);
|
2017-08-06 03:37:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-06 04:02:21 +00:00
|
|
|
// place new value into array
|
|
|
|
((int16 *)new_data)[i] = v;
|
|
|
|
} // i
|
|
|
|
if (bps != 2) {
|
|
|
|
free(seq->data);
|
|
|
|
seq->data = (uint16 *)new_data;
|
|
|
|
seq->data_size = samples * 2;
|
|
|
|
}
|
|
|
|
// update seq info
|
|
|
|
seq->bits_per_sample = 16;
|
|
|
|
seq->is_unsigned = 0;
|
|
|
|
} // incorrect format
|
|
|
|
|
|
|
|
// 2. samplerate conversion
|
|
|
|
if (seq->sample_rate != snd_frequency) { // need to resample seq->data
|
|
|
|
// create new resampler
|
2017-08-06 03:37:54 +00:00
|
|
|
SpeexResamplerState *state;
|
|
|
|
state = speex_resampler_init(seq->channels, seq->sample_rate, snd_frequency, SPEEX_RESAMPLER_QUALITY_MIN, NULL);
|
2022-05-06 04:02:21 +00:00
|
|
|
if (!state) { // NULL means failure
|
2017-08-06 03:37:54 +00:00
|
|
|
free(seq->data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
// allocate new memory for output
|
|
|
|
int32 out_samples_max = ((double)seq->data_size / seq->channels / 2) * ((((double)snd_frequency) / ((double)seq->sample_rate)) + 0.1) +
|
|
|
|
100; // 10%+100 extra samples as a buffer-zone
|
2017-08-06 03:37:54 +00:00
|
|
|
int16 *resampled = (int16 *)malloc(out_samples_max * seq->channels * sizeof(int16));
|
|
|
|
if (!resampled) {
|
|
|
|
free(seq->data);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-08-02 12:12:44 +00:00
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
// establish data sizes
|
|
|
|
// in_len will be set by the resampler to number of samples processed
|
|
|
|
spx_uint32_t in_len =
|
|
|
|
seq->data_size / seq->channels / 2; // divide by 2 because 2byte samples, divide by #channels because function wants it per-channel
|
|
|
|
// out_len will be set to the number of samples written
|
2017-10-22 13:51:17 +00:00
|
|
|
spx_uint32_t out_len = out_samples_max * seq->channels * sizeof(int16);
|
2017-08-06 03:37:54 +00:00
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
// resample!
|
2017-08-06 03:37:54 +00:00
|
|
|
if (speex_resampler_process_interleaved_int(state, (spx_int16_t *)seq->data, &in_len, (spx_int16_t *)resampled, &out_len) != RESAMPLER_ERR_SUCCESS) {
|
2022-05-06 04:02:21 +00:00
|
|
|
// Error
|
2017-08-06 03:37:54 +00:00
|
|
|
free(resampled);
|
|
|
|
free(seq->data);
|
|
|
|
speex_resampler_destroy(state);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
// destroy the resampler anyway
|
2017-08-06 03:37:54 +00:00
|
|
|
speex_resampler_destroy(state);
|
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
// establish real size of new data and update seq
|
|
|
|
free(seq->data); // That was the old data
|
|
|
|
seq->data_size = out_len * seq->channels * 2; // remember out_len is perchannel, and each sample is 2 bytes
|
|
|
|
seq->data = (uint16_t *)realloc(resampled, seq->data_size); // we overestimated the array size before, so make it the correct size now
|
|
|
|
if (!seq->data) { // realloc could fail
|
2017-08-06 03:37:54 +00:00
|
|
|
free(resampled);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
seq->sample_rate = snd_frequency;
|
|
|
|
}
|
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
// Unpack stereo data into separate left/right buffers
|
2017-08-10 11:50:31 +00:00
|
|
|
if (seq->channels == 1) {
|
|
|
|
seq->channels = 1;
|
|
|
|
seq->data_left = seq->data;
|
|
|
|
seq->data_left_size = seq->data_size;
|
|
|
|
seq->data_right = NULL;
|
|
|
|
seq->data_right_size = 0;
|
2022-05-06 04:02:21 +00:00
|
|
|
} else if (seq->channels == 2) {
|
2017-08-10 11:50:31 +00:00
|
|
|
seq->data_left_size = seq->data_right_size = seq->data_size / 2;
|
|
|
|
seq->data_left = (uint16_t *)malloc(seq->data_size / 2);
|
|
|
|
if (!seq->data_left) {
|
|
|
|
free(seq->data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
seq->data_right = (uint16_t *)malloc(seq->data_size / 2);
|
|
|
|
if (!seq->data_right) {
|
|
|
|
free(seq->data_left);
|
|
|
|
free(seq->data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (int sample = 0; sample < seq->data_size / 4; sample++) {
|
|
|
|
seq->data_left[sample] = seq->data[sample * 2];
|
|
|
|
seq->data_right[sample] = seq->data[sample * 2 + 1];
|
|
|
|
}
|
|
|
|
free(seq->data);
|
|
|
|
seq->data = NULL;
|
2022-05-06 04:02:21 +00:00
|
|
|
} else {
|
2017-08-10 11:50:31 +00:00
|
|
|
free(seq->data);
|
|
|
|
return 0;
|
2022-05-06 04:02:21 +00:00
|
|
|
}
|
2017-08-06 03:37:54 +00:00
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
// attach sequence to handle (& inc. refs)
|
|
|
|
// create snd handle
|
|
|
|
static int32 handle;
|
|
|
|
handle = list_add(snd_handles);
|
|
|
|
static snd_struct *snd;
|
|
|
|
snd = (snd_struct *)list_get(snd_handles, handle);
|
2017-08-06 03:37:54 +00:00
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
snd->internal = 0;
|
|
|
|
snd->type = 2;
|
|
|
|
snd->seq = seq;
|
|
|
|
snd->volume = 1.0;
|
2017-08-06 03:37:54 +00:00
|
|
|
|
2017-08-10 11:50:31 +00:00
|
|
|
if (seq->channels == 1) {
|
|
|
|
snd->bal_left_x = snd->bal_left_y = snd->bal_left_z = 0;
|
2022-05-06 04:02:21 +00:00
|
|
|
} else if (seq->channels == 2) {
|
2017-08-10 12:06:20 +00:00
|
|
|
snd->bal_left_x = -0.01;
|
2017-08-10 11:50:31 +00:00
|
|
|
snd->bal_left_y = snd->bal_left_z = 0;
|
2017-08-10 12:06:20 +00:00
|
|
|
snd->bal_right_x = 0.01;
|
2017-08-10 11:50:31 +00:00
|
|
|
snd->bal_right_y = snd->bal_right_z = 0;
|
|
|
|
}
|
|
|
|
snd->bal_update = 1;
|
|
|
|
sndupdate(snd);
|
2017-08-06 03:37:54 +00:00
|
|
|
return handle;
|
2015-08-02 12:12:44 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
mem_block func__memsound(int32 i, int32 targetChannel) {
|
|
|
|
|
2020-12-31 04:40:54 +00:00
|
|
|
static mem_block b;
|
2022-05-06 04:02:21 +00:00
|
|
|
|
|
|
|
if (new_error)
|
|
|
|
goto error;
|
|
|
|
if (i <= 0)
|
|
|
|
goto error;
|
|
|
|
|
2020-12-31 04:40:54 +00:00
|
|
|
sndsetup();
|
|
|
|
|
|
|
|
static snd_struct *sn;
|
2022-05-06 04:02:21 +00:00
|
|
|
sn = (snd_struct *)list_get(snd_handles, i);
|
|
|
|
if (!sn) {
|
2020-12-31 04:40:54 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2022-05-06 04:02:21 +00:00
|
|
|
if (!snd_allow_internal) {
|
|
|
|
if (sn->internal) {
|
2020-12-31 04:40:54 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2022-05-06 04:02:21 +00:00
|
|
|
if (targetChannel < 1 || targetChannel > sn->seq->channels)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (sn->lock_id) {
|
|
|
|
b.lock_offset = (ptrszint)sn->lock_offset;
|
|
|
|
b.lock_id = sn->lock_id; // get existing tag
|
|
|
|
} else {
|
2020-12-31 04:40:54 +00:00
|
|
|
new_mem_lock();
|
2022-05-06 04:02:21 +00:00
|
|
|
mem_lock_tmp->type = 5; // sound
|
|
|
|
b.lock_offset = (ptrszint)mem_lock_tmp;
|
|
|
|
b.lock_id = mem_lock_id;
|
|
|
|
sn->lock_offset = (void *)mem_lock_tmp;
|
|
|
|
sn->lock_id = mem_lock_id; // create tag
|
2020-12-31 04:40:54 +00:00
|
|
|
}
|
2022-05-06 04:02:21 +00:00
|
|
|
|
|
|
|
if (targetChannel == 1) {
|
|
|
|
b.offset = (ptrszint)sn->seq->data_left;
|
|
|
|
b.size = sn->seq->data_left_size;
|
2020-12-31 04:40:54 +00:00
|
|
|
}
|
2022-05-06 04:02:21 +00:00
|
|
|
|
|
|
|
if (targetChannel == 2) {
|
|
|
|
b.offset = (ptrszint)sn->seq->data_right;
|
|
|
|
b.size = sn->seq->data_right_size;
|
2020-12-31 04:40:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
b.type = 0; // sn->bytes_per_pixel+128+1024+2048;//integer+unsigned+pixeltype
|
|
|
|
b.elementsize = sn->seq->bits_per_sample / 8;
|
|
|
|
b.sound = i;
|
|
|
|
|
2020-12-31 04:40:54 +00:00
|
|
|
return b;
|
2022-05-06 04:02:21 +00:00
|
|
|
error:
|
|
|
|
b.offset = 0;
|
|
|
|
b.size = 0;
|
|
|
|
b.lock_offset = (ptrszint)mem_lock_base;
|
|
|
|
b.lock_id = 1073741821; // set invalid lock
|
|
|
|
b.type = 0;
|
|
|
|
b.elementsize = 0;
|
|
|
|
b.sound = 0;
|
2020-12-31 04:40:54 +00:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2022-05-06 04:02:21 +00:00
|
|
|
void sub__sndplayfile(qbs *filename, int32 sync, double volume, int32 passed) {
|
|
|
|
if (new_error)
|
|
|
|
return;
|
2017-08-06 03:37:54 +00:00
|
|
|
sndsetup();
|
2017-08-11 11:19:52 +00:00
|
|
|
int32 handle;
|
|
|
|
handle = func__sndopen(filename, NULL, 0);
|
2022-05-06 04:02:21 +00:00
|
|
|
if (!handle)
|
|
|
|
return;
|
2017-08-11 11:19:52 +00:00
|
|
|
if (passed & 2) {
|
|
|
|
sub__sndvol(handle, volume);
|
2017-08-06 03:37:54 +00:00
|
|
|
}
|
|
|
|
sub__sndplay(handle);
|
|
|
|
sub__sndclose(handle);
|
2015-08-02 12:12:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|