|
| 1 | +// Copyright (C) 2023 Fabian Greffrath |
| 2 | +// |
| 3 | +// This program is free software; you can redistribute it and/or |
| 4 | +// modify it under the terms of the GNU General Public License |
| 5 | +// as published by the Free Software Foundation; either version 2 |
| 6 | +// of the License, or (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// DESCRIPTION: |
| 14 | +// Load sound lumps with libsndfile. |
| 15 | + |
| 16 | +#include "SDL.h" |
| 17 | +#include "sndfile.h" |
| 18 | + |
| 19 | +#include "lprintf.h" |
| 20 | +#include "memio.h" |
| 21 | +#include "z_zone.h" |
| 22 | + |
| 23 | +static sf_count_t sfvio_get_filelen(void *user_data) |
| 24 | +{ |
| 25 | + MEMFILE *fs = user_data; |
| 26 | + long pos; |
| 27 | + sf_count_t len; |
| 28 | + |
| 29 | + pos = mem_ftell(fs); |
| 30 | + mem_fseek(fs, 0, MEM_SEEK_END); |
| 31 | + len = mem_ftell(fs); |
| 32 | + mem_fseek(fs, pos, MEM_SEEK_SET); |
| 33 | + |
| 34 | + return len; |
| 35 | +} |
| 36 | + |
| 37 | +static sf_count_t sfvio_seek(sf_count_t offset, int whence, void *user_data) |
| 38 | +{ |
| 39 | + MEMFILE *fs = user_data; |
| 40 | + mem_fseek(fs, offset, whence); |
| 41 | + return mem_ftell(fs); |
| 42 | +} |
| 43 | + |
| 44 | +static sf_count_t sfvio_read(void *ptr, sf_count_t count, void *user_data) |
| 45 | +{ |
| 46 | + return mem_fread(ptr, 1, count, (MEMFILE *)user_data); |
| 47 | +} |
| 48 | + |
| 49 | +static sf_count_t sfvio_tell(void *user_data) |
| 50 | +{ |
| 51 | + return mem_ftell((MEMFILE *)user_data); |
| 52 | +} |
| 53 | + |
| 54 | +void *Load_SNDFile(void *data, SDL_AudioSpec *sample, void **sampledata, |
| 55 | + Uint32 *samplelen) |
| 56 | +{ |
| 57 | + SNDFILE *sndfile; |
| 58 | + SF_INFO sfinfo = {0}; |
| 59 | + SF_VIRTUAL_IO sfvio = |
| 60 | + { |
| 61 | + sfvio_get_filelen, |
| 62 | + sfvio_seek, |
| 63 | + sfvio_read, |
| 64 | + NULL, |
| 65 | + sfvio_tell |
| 66 | + }; |
| 67 | + Uint32 local_samplelen; |
| 68 | + void *local_sampledata; |
| 69 | + sf_count_t num_frames; |
| 70 | + dboolean float_format; |
| 71 | + |
| 72 | + MEMFILE *sfdata = mem_fopen_read(data, *samplelen); |
| 73 | + |
| 74 | + sndfile = sf_open_virtual(&sfvio, SFM_READ, &sfinfo, sfdata); |
| 75 | + |
| 76 | + if (!sndfile) |
| 77 | + { |
| 78 | + lprintf(LO_WARN, "sf_open_virtual: %s\n", sf_strerror(sndfile)); |
| 79 | + mem_fclose(sfdata); |
| 80 | + return NULL; |
| 81 | + } |
| 82 | + |
| 83 | + if (sfinfo.frames <= 0 || sfinfo.channels <= 0) |
| 84 | + { |
| 85 | + sf_close(sndfile); |
| 86 | + mem_fclose(sfdata); |
| 87 | + return NULL; |
| 88 | + } |
| 89 | + |
| 90 | + switch ((sfinfo.format & SF_FORMAT_SUBMASK)) |
| 91 | + { |
| 92 | + case SF_FORMAT_PCM_24: |
| 93 | + case SF_FORMAT_PCM_32: |
| 94 | + case SF_FORMAT_FLOAT: |
| 95 | + case SF_FORMAT_DOUBLE: |
| 96 | + case SF_FORMAT_VORBIS: |
| 97 | + case SF_FORMAT_OPUS: |
| 98 | + case SF_FORMAT_ALAC_20: |
| 99 | + case SF_FORMAT_ALAC_24: |
| 100 | + case SF_FORMAT_ALAC_32: |
| 101 | +#ifdef HAVE_SNDFILE_MPEG |
| 102 | + case SF_FORMAT_MPEG_LAYER_I: |
| 103 | + case SF_FORMAT_MPEG_LAYER_II: |
| 104 | + case SF_FORMAT_MPEG_LAYER_III: |
| 105 | +#endif |
| 106 | + float_format = true; |
| 107 | + break; |
| 108 | + default: |
| 109 | + float_format = false; |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + local_samplelen = sfinfo.frames * sfinfo.channels * (float_format ? sizeof(float) : sizeof(short)); |
| 114 | + local_sampledata = Z_Malloc(local_samplelen); |
| 115 | + |
| 116 | + if (float_format) |
| 117 | + { |
| 118 | + num_frames = sf_readf_float(sndfile, local_sampledata, sfinfo.frames); |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + num_frames = sf_readf_short(sndfile, local_sampledata, sfinfo.frames); |
| 123 | + } |
| 124 | + |
| 125 | + if (num_frames < sfinfo.frames) |
| 126 | + { |
| 127 | + lprintf(LO_WARN, "sf_readf: %s\n", sf_strerror(sndfile)); |
| 128 | + sf_close(sndfile); |
| 129 | + mem_fclose(sfdata); |
| 130 | + Z_Free(local_sampledata); |
| 131 | + return NULL; |
| 132 | + } |
| 133 | + |
| 134 | + sf_close(sndfile); |
| 135 | + mem_fclose(sfdata); |
| 136 | + |
| 137 | + sample->channels = sfinfo.channels; |
| 138 | + sample->freq = sfinfo.samplerate; |
| 139 | + sample->format = float_format ? AUDIO_F32 : AUDIO_S16; |
| 140 | + |
| 141 | + *sampledata = local_sampledata; |
| 142 | + *samplelen = local_samplelen; |
| 143 | + |
| 144 | + return sample; |
| 145 | +} |
0 commit comments