Skip to content

Commit

Permalink
Updated to current libretro-common
Browse files Browse the repository at this point in the history
  • Loading branch information
albertofustinoni committed Jun 26, 2024
1 parent ff0355e commit 2898730
Show file tree
Hide file tree
Showing 257 changed files with 14,578 additions and 8,664 deletions.
1 change: 1 addition & 0 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ SOURCES_C += $(CORE_DIR)/libretro-common/compat/compat_strl.c \
$(CORE_DIR)/libretro-common/streams/file_stream_transforms.c \
$(CORE_DIR)/libretro-common/streams/memory_stream.c \
$(CORE_DIR)/libretro-common/string/stdstring.c \
$(CORE_DIR)/libretro-common/time/rtime.c \
$(CORE_DIR)/libretro-common/vfs/vfs_implementation.c
endif

Expand Down
7 changes: 6 additions & 1 deletion src/libretro/libretro-common/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
*.o
glsm/
*.[od]
*.dll
*.so
*.dylib
*.exe
163 changes: 111 additions & 52 deletions src/libretro/libretro-common/audio/audio_mix.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2010-2018 The RetroArch team
/* Copyright (C) 2010-2020 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (audio_mix.c).
Expand All @@ -20,18 +20,18 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <audio/audio_mix.h>
#include <stdlib.h>
#include <string.h>
#include <memalign.h>

#include <retro_environment.h>

#if defined(__SSE2__)
#include <emmintrin.h>
#elif defined(__ALTIVEC__)
#include <altivec.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memalign.h>
#include <retro_miscellaneous.h>
#include <audio/audio_mix.h>
#include <streams/file_stream.h>
Expand All @@ -48,7 +48,7 @@ void audio_mix_volume_C(float *out, const float *in, float vol, size_t samples)
#ifdef __SSE2__
void audio_mix_volume_SSE2(float *out, const float *in, float vol, size_t samples)
{
size_t i;
size_t i, remaining_samples;
__m128 volume = _mm_set1_ps(vol);

for (i = 0; i + 16 <= samples; i += 16, out += 16, in += 16)
Expand All @@ -71,7 +71,10 @@ void audio_mix_volume_SSE2(float *out, const float *in, float vol, size_t sample
_mm_storeu_ps(out + 4 * j, _mm_add_ps(input[j], additive[j]));
}

audio_mix_volume_C(out, in, vol, samples - i);
remaining_samples = samples - i;

for (i = 0; i < remaining_samples; i++)
out[i] += in[i] * vol;
}
#endif

Expand All @@ -80,12 +83,14 @@ void audio_mix_free_chunk(audio_chunk_t *chunk)
if (!chunk)
return;

#ifdef HAVE_RWAV
if (chunk->rwav && chunk->rwav->samples)
{
/* rwav_free only frees the samples */
rwav_free(chunk->rwav);
free(chunk->rwav);
}
#endif

if (chunk->buf)
free(chunk->buf);
Expand All @@ -108,57 +113,89 @@ void audio_mix_free_chunk(audio_chunk_t *chunk)
free(chunk);
}

audio_chunk_t* audio_mix_load_wav_file(const char *path, int sample_rate)
audio_chunk_t* audio_mix_load_wav_file(const char *path, int sample_rate,
const char *resampler_ident, enum resampler_quality quality)
{
#ifdef HAVE_RWAV
int sample_size;
int64_t len = 0;
void *buf = NULL;
audio_chunk_t *chunk = (audio_chunk_t*)calloc(1, sizeof(*chunk));
int64_t len = 0;
void *buf = NULL;
audio_chunk_t *chunk = (audio_chunk_t*)malloc(sizeof(*chunk));

if (!chunk)
return NULL;

chunk->buf = NULL;
chunk->upsample_buf = NULL;
chunk->float_buf = NULL;
chunk->float_resample_buf = NULL;
chunk->resample_buf = NULL;
chunk->len = 0;
chunk->resample_len = 0;
chunk->sample_rate = sample_rate;
chunk->resample = false;
chunk->resampler = NULL;
chunk->resampler_data = NULL;
chunk->ratio = 0.00f;
chunk->rwav = (rwav_t*)malloc(sizeof(rwav_t));

if (!chunk->rwav)
goto error;

chunk->rwav->bitspersample = 0;
chunk->rwav->numchannels = 0;
chunk->rwav->samplerate = 0;
chunk->rwav->numsamples = 0;
chunk->rwav->subchunk2size = 0;
chunk->rwav->samples = NULL;

if (!filestream_read_file(path, &buf, &len))
{
printf("Could not open WAV file for reading.\n");
goto error;
}

chunk->sample_rate = sample_rate;
chunk->buf = buf;
chunk->len = len;
chunk->rwav = (rwav_t*)malloc(sizeof(rwav_t));
chunk->buf = buf;
chunk->len = len;

if (rwav_load(chunk->rwav, chunk->buf, chunk->len) == RWAV_ITERATE_ERROR)
{
printf("error: could not load WAV file\n");
goto error;
}

/* numsamples does not know or care about
* multiple channels, but we need space for 2 */
chunk->upsample_buf = (int16_t*)memalign_alloc(128,
chunk->upsample_buf = (int16_t*)memalign_alloc(128,
chunk->rwav->numsamples * 2 * sizeof(int16_t));

sample_size = chunk->rwav->bitspersample / 8;
sample_size = chunk->rwav->bitspersample / 8;

if (sample_size == 1)
{
unsigned i;

for (i = 0; i < chunk->rwav->numsamples; i++)
{
uint8_t *sample = (
(uint8_t*)chunk->rwav->samples) +
(i * chunk->rwav->numchannels);

chunk->upsample_buf[i * 2] = (int16_t)((sample[0] - 128) << 8);
if (chunk->rwav->numchannels == 1)
{
for (i = 0; i < chunk->rwav->numsamples; i++)
{
uint8_t *sample = (
(uint8_t*)chunk->rwav->samples) + i;

if (chunk->rwav->numchannels == 1)
chunk->upsample_buf[(i * 2) + 1] = (int16_t)((sample[0] - 128) << 8);
else if (chunk->rwav->numchannels == 2)
chunk->upsample_buf[(i * 2) + 1] = (int16_t)((sample[1] - 128) << 8);
}
chunk->upsample_buf[i * 2] =
(int16_t)((sample[0] - 128) << 8);
chunk->upsample_buf[(i * 2) + 1] =
(int16_t)((sample[0] - 128) << 8);
}
}
else if (chunk->rwav->numchannels == 2)
{
for (i = 0; i < chunk->rwav->numsamples; i++)
{
uint8_t *sample = (
(uint8_t*)chunk->rwav->samples) +
(i * 2);

chunk->upsample_buf[i * 2] =
(int16_t)((sample[0] - 128) << 8);
chunk->upsample_buf[(i * 2) + 1] =
(int16_t)((sample[1] - 128) << 8);
}
}
}
else if (sample_size == 2)
{
Expand All @@ -168,43 +205,50 @@ audio_chunk_t* audio_mix_load_wav_file(const char *path, int sample_rate)

for (i = 0; i < chunk->rwav->numsamples; i++)
{
int16_t sample = ((int16_t*)chunk->rwav->samples)[i];
int16_t sample = ((int16_t*)
chunk->rwav->samples)[i];

chunk->upsample_buf[i * 2] = sample;
chunk->upsample_buf[(i * 2) + 1] = sample;
}
}
else if (chunk->rwav->numchannels == 2)
memcpy(chunk->upsample_buf, chunk->rwav->samples, chunk->rwav->subchunk2size);
memcpy(chunk->upsample_buf, chunk->rwav->samples,
chunk->rwav->subchunk2size);
}
else if (sample_size != 2)
{
/* we don't support any other sample size besides 8 and 16-bit yet */
printf("error: we don't support a sample size of %d\n", sample_size);
goto error;
}

if (sample_rate != (int)chunk->rwav->samplerate)
{
chunk->resample = true;
chunk->ratio = (double)sample_rate / chunk->rwav->samplerate;
chunk->ratio = (double)sample_rate / chunk->rwav->samplerate;

retro_resampler_realloc(&chunk->resampler_data,
&chunk->resampler,
NULL,
RESAMPLER_QUALITY_DONTCARE,
resampler_ident,
quality,
chunk->ratio);

if (chunk->resampler && chunk->resampler_data)
{
struct resampler_data info;

chunk->float_buf = (float*)memalign_alloc(128, chunk->rwav->numsamples * 2 * chunk->ratio * sizeof(float));
chunk->float_buf = (float*)memalign_alloc(128,
chunk->rwav->numsamples * 2 *
chunk->ratio * sizeof(float));

/* why is *3 needed instead of just *2? does the sinc driver require more space than we know about? */
chunk->float_resample_buf = (float*)memalign_alloc(128, chunk->rwav->numsamples * 3 * chunk->ratio * sizeof(float));
/* why is *3 needed instead of just *2? Does the
* sinc driver require more space than we know about? */
chunk->float_resample_buf = (float*)memalign_alloc(128,
chunk->rwav->numsamples * 3 *
chunk->ratio * sizeof(float));

convert_s16_to_float(chunk->float_buf, chunk->upsample_buf, chunk->rwav->numsamples * 2, 1.0);
convert_s16_to_float(chunk->float_buf,
chunk->upsample_buf, chunk->rwav->numsamples * 2, 1.0);

info.data_in = (const float*)chunk->float_buf;
info.data_out = chunk->float_resample_buf;
Expand All @@ -216,17 +260,21 @@ audio_chunk_t* audio_mix_load_wav_file(const char *path, int sample_rate)

chunk->resampler->process(chunk->resampler_data, &info);

/* number of output_frames does not increase with multiple channels, but assume we need space for 2 */
chunk->resample_buf = (int16_t*)memalign_alloc(128, info.output_frames * 2 * sizeof(int16_t));
/* number of output_frames does not increase with
* multiple channels, but assume we need space for 2 */
chunk->resample_buf = (int16_t*)memalign_alloc(128,
info.output_frames * 2 * sizeof(int16_t));
chunk->resample_len = info.output_frames;
convert_float_to_s16(chunk->resample_buf, chunk->float_resample_buf, info.output_frames * 2);
convert_float_to_s16(chunk->resample_buf,
chunk->float_resample_buf, info.output_frames * 2);
}
}

return chunk;

error:
audio_mix_free_chunk(chunk);
#endif
return NULL;
}

Expand All @@ -235,12 +283,14 @@ size_t audio_mix_get_chunk_num_samples(audio_chunk_t *chunk)
if (!chunk)
return 0;

#ifdef HAVE_RWAV
if (chunk->rwav)
{
if (chunk->resample)
return chunk->resample_len;
return chunk->rwav->numsamples;
}
#endif

/* no other filetypes supported yet */
return 0;
Expand All @@ -256,11 +306,13 @@ size_t audio_mix_get_chunk_num_samples(audio_chunk_t *chunk)
*
* Returns: A signed 16-bit audio sample.
**/
int16_t audio_mix_get_chunk_sample(audio_chunk_t *chunk, unsigned channel, size_t index)
int16_t audio_mix_get_chunk_sample(audio_chunk_t *chunk,
unsigned channel, size_t index)
{
if (!chunk)
return 0;

#ifdef HAVE_RWAV
if (chunk->rwav)
{
int sample_size = chunk->rwav->bitspersample / 8;
Expand All @@ -271,15 +323,18 @@ int16_t audio_mix_get_chunk_sample(audio_chunk_t *chunk, unsigned channel, size_

if (chunk->resample)
sample = (uint8_t*)chunk->resample_buf +
(sample_size * index * chunk->rwav->numchannels) + (channel * sample_size);
(sample_size * index * chunk->rwav->numchannels)
+ (channel * sample_size);
else
sample = (uint8_t*)chunk->upsample_buf +
(sample_size * index * chunk->rwav->numchannels) + (channel * sample_size);
(sample_size * index * chunk->rwav->numchannels)
+ (channel * sample_size);

sample_out = (int16_t)*sample;

return sample_out;
}
#endif

/* no other filetypes supported yet */
return 0;
Expand All @@ -290,6 +345,7 @@ int16_t* audio_mix_get_chunk_samples(audio_chunk_t *chunk)
if (!chunk)
return 0;

#ifdef HAVE_RWAV
if (chunk->rwav)
{
int16_t *sample;
Expand All @@ -301,6 +357,7 @@ int16_t* audio_mix_get_chunk_samples(audio_chunk_t *chunk)

return sample;
}
#endif

return NULL;
}
Expand All @@ -310,8 +367,10 @@ int audio_mix_get_chunk_num_channels(audio_chunk_t *chunk)
if (!chunk)
return 0;

#ifdef HAVE_RWAV
if (chunk->rwav)
return chunk->rwav->numchannels;
#endif

/* don't support other formats yet */
return 0;
Expand Down
Loading

0 comments on commit 2898730

Please sign in to comment.