mirror of
https://github.com/love2d/megasource.git
synced 2026-08-17 11:11:35 +02:00
Update OpenAL-soft to 1.23.1-bc7cb17.
This commit is contained in:
@@ -22,7 +22,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/* This file contains an example for applying convolution reverb to a source. */
|
||||
/* This file contains an example for applying convolution to a source. */
|
||||
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
@@ -38,10 +38,12 @@
|
||||
|
||||
#include "common/alhelpers.h"
|
||||
|
||||
#include "win_main_utf8.h"
|
||||
|
||||
#ifndef AL_SOFT_convolution_reverb
|
||||
#define AL_SOFT_convolution_reverb
|
||||
#define AL_EFFECT_CONVOLUTION_REVERB_SOFT 0xA000
|
||||
|
||||
#ifndef AL_SOFT_convolution_effect
|
||||
#define AL_SOFT_convolution_effect
|
||||
#define AL_EFFECT_CONVOLUTION_SOFT 0xA000
|
||||
#endif
|
||||
|
||||
|
||||
@@ -88,11 +90,11 @@ static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv;
|
||||
/* This stuff defines a simple streaming player object, the same as alstream.c.
|
||||
* Comments are removed for brevity, see alstream.c for more details.
|
||||
*/
|
||||
#define NUM_BUFFERS 4
|
||||
#define BUFFER_SAMPLES 8192
|
||||
enum { NumBuffers = 4 };
|
||||
enum { BufferSamples = 8192 };
|
||||
|
||||
typedef struct StreamPlayer {
|
||||
ALuint buffers[NUM_BUFFERS];
|
||||
ALuint buffers[NumBuffers];
|
||||
ALuint source;
|
||||
|
||||
SNDFILE *sndfile;
|
||||
@@ -109,7 +111,7 @@ static StreamPlayer *NewPlayer(void)
|
||||
player = calloc(1, sizeof(*player));
|
||||
assert(player != NULL);
|
||||
|
||||
alGenBuffers(NUM_BUFFERS, player->buffers);
|
||||
alGenBuffers(NumBuffers, player->buffers);
|
||||
assert(alGetError() == AL_NO_ERROR && "Could not create buffers");
|
||||
|
||||
alGenSources(1, &player->source);
|
||||
@@ -138,11 +140,11 @@ static void DeletePlayer(StreamPlayer *player)
|
||||
ClosePlayerFile(player);
|
||||
|
||||
alDeleteSources(1, &player->source);
|
||||
alDeleteBuffers(NUM_BUFFERS, player->buffers);
|
||||
alDeleteBuffers(NumBuffers, player->buffers);
|
||||
if(alGetError() != AL_NO_ERROR)
|
||||
fprintf(stderr, "Failed to delete object IDs\n");
|
||||
|
||||
memset(player, 0, sizeof(*player));
|
||||
memset(player, 0, sizeof(*player)); /* NOLINT(clang-analyzer-security.insecureAPI.*) */
|
||||
free(player);
|
||||
}
|
||||
|
||||
@@ -184,7 +186,7 @@ static int OpenPlayerFile(StreamPlayer *player, const char *filename)
|
||||
return 0;
|
||||
}
|
||||
|
||||
frame_size = (size_t)(BUFFER_SAMPLES * player->sfinfo.channels) * sizeof(float);
|
||||
frame_size = (size_t)(BufferSamples * player->sfinfo.channels) * sizeof(float);
|
||||
player->membuf = malloc(frame_size);
|
||||
|
||||
return 1;
|
||||
@@ -197,9 +199,9 @@ static int StartPlayer(StreamPlayer *player)
|
||||
alSourceRewind(player->source);
|
||||
alSourcei(player->source, AL_BUFFER, 0);
|
||||
|
||||
for(i = 0;i < NUM_BUFFERS;i++)
|
||||
for(i = 0;i < NumBuffers;i++)
|
||||
{
|
||||
sf_count_t slen = sf_readf_float(player->sndfile, player->membuf, BUFFER_SAMPLES);
|
||||
sf_count_t slen = sf_readf_float(player->sndfile, player->membuf, BufferSamples);
|
||||
if(slen < 1) break;
|
||||
|
||||
slen *= player->sfinfo.channels * (sf_count_t)sizeof(float);
|
||||
@@ -243,7 +245,7 @@ static int UpdatePlayer(StreamPlayer *player)
|
||||
alSourceUnqueueBuffers(player->source, 1, &bufid);
|
||||
processed--;
|
||||
|
||||
slen = sf_readf_float(player->sndfile, player->membuf, BUFFER_SAMPLES);
|
||||
slen = sf_readf_float(player->sndfile, player->membuf, BufferSamples);
|
||||
if(slen > 0)
|
||||
{
|
||||
slen *= player->sfinfo.channels * (sf_count_t)sizeof(float);
|
||||
@@ -278,21 +280,21 @@ static int UpdatePlayer(StreamPlayer *player)
|
||||
}
|
||||
|
||||
|
||||
/* CreateEffect creates a new OpenAL effect object with a convolution reverb
|
||||
* type, and returns the new effect ID.
|
||||
/* CreateEffect creates a new OpenAL effect object with a convolution type, and
|
||||
* returns the new effect ID.
|
||||
*/
|
||||
static ALuint CreateEffect(void)
|
||||
{
|
||||
ALuint effect = 0;
|
||||
ALenum err;
|
||||
|
||||
printf("Using Convolution Reverb\n");
|
||||
printf("Using Convolution\n");
|
||||
|
||||
/* Create the effect object and set the convolution reverb effect type. */
|
||||
/* Create the effect object and set the convolution effect type. */
|
||||
alGenEffects(1, &effect);
|
||||
alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_CONVOLUTION_REVERB_SOFT);
|
||||
alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_CONVOLUTION_SOFT);
|
||||
|
||||
/* Check if an error occured, and clean up if so. */
|
||||
/* Check if an error occurred, and clean up if so. */
|
||||
err = alGetError();
|
||||
if(err != AL_NO_ERROR)
|
||||
{
|
||||
@@ -359,10 +361,10 @@ static ALuint LoadSound(const char *filename)
|
||||
}
|
||||
|
||||
namepart = strrchr(filename, '/');
|
||||
if(namepart || (namepart=strrchr(filename, '\\')))
|
||||
namepart++;
|
||||
else
|
||||
namepart = filename;
|
||||
if(!namepart) namepart = strrchr(filename, '\\');
|
||||
if(!namepart) namepart = filename;
|
||||
else namepart++;
|
||||
|
||||
printf("Loading: %s (%s, %dhz, %" PRId64 " samples / %.2f seconds)\n", namepart,
|
||||
FormatName(format), sfinfo.samplerate, sfinfo.frames,
|
||||
(double)sfinfo.frames / sfinfo.samplerate);
|
||||
@@ -391,7 +393,7 @@ static ALuint LoadSound(const char *filename)
|
||||
free(membuf);
|
||||
sf_close(sndfile);
|
||||
|
||||
/* Check if an error occured, and clean up if so. */
|
||||
/* Check if an error occurred, and clean up if so. */
|
||||
err = alGetError();
|
||||
if(err != AL_NO_ERROR)
|
||||
{
|
||||
@@ -423,10 +425,10 @@ int main(int argc, char **argv)
|
||||
if(InitAL(&argv, &argc) != 0)
|
||||
return 1;
|
||||
|
||||
if(!alIsExtensionPresent("AL_SOFTX_convolution_reverb"))
|
||||
if(!alIsExtensionPresent("AL_SOFTX_convolution_effect"))
|
||||
{
|
||||
CloseAL();
|
||||
fprintf(stderr, "Error: Convolution revern not supported\n");
|
||||
fprintf(stderr, "Error: Convolution effect not supported\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -500,11 +502,11 @@ int main(int argc, char **argv)
|
||||
alGenAuxiliaryEffectSlots(1, &slot);
|
||||
|
||||
/* Set the impulse response sound buffer on the effect slot. This allows
|
||||
* effects to access it as needed. In this case, convolution reverb uses it
|
||||
* as the filter source. NOTE: Unlike the effect object, the buffer *is*
|
||||
* kept referenced and may not be changed or deleted as long as it's set,
|
||||
* just like with a source. When another buffer is set, or the effect slot
|
||||
* is deleted, the buffer reference is released.
|
||||
* effects to access it as needed. In this case, convolution uses it as the
|
||||
* filter source. NOTE: Unlike the effect object, the buffer *is* kept
|
||||
* referenced and may not be changed or deleted as long as it's set, just
|
||||
* like with a source. When another buffer is set, or the effect slot is
|
||||
* deleted, the buffer reference is released.
|
||||
*
|
||||
* The effect slot's gain is reduced because the impulse responses I've
|
||||
* tested with result in excessively loud reverb. Is that normal? Even with
|
||||
@@ -555,10 +557,9 @@ int main(int argc, char **argv)
|
||||
continue;
|
||||
|
||||
namepart = strrchr(argv[i], '/');
|
||||
if(namepart || (namepart=strrchr(argv[i], '\\')))
|
||||
namepart++;
|
||||
else
|
||||
namepart = argv[i];
|
||||
if(!namepart) namepart = strrchr(argv[i], '\\');
|
||||
if(!namepart) namepart = argv[i];
|
||||
else namepart++;
|
||||
|
||||
printf("Playing: %s (%s, %dhz)\n", namepart, FormatName(player->format),
|
||||
player->sfinfo.samplerate);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -40,6 +40,8 @@
|
||||
|
||||
#include "common/alhelpers.h"
|
||||
|
||||
#include "win_main_utf8.h"
|
||||
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI (3.14159265358979323846)
|
||||
@@ -121,7 +123,7 @@ static ALuint LoadSound(const char *filename)
|
||||
free(membuf);
|
||||
sf_close(sndfile);
|
||||
|
||||
/* Check if an error occured, and clean up if so. */
|
||||
/* Check if an error occurred, and clean up if so. */
|
||||
err = alGetError();
|
||||
if(err != AL_NO_ERROR)
|
||||
{
|
||||
|
||||
@@ -37,6 +37,8 @@
|
||||
|
||||
#include "common/alhelpers.h"
|
||||
|
||||
#include "win_main_utf8.h"
|
||||
|
||||
|
||||
static LPALSOURCEDSOFT alSourcedSOFT;
|
||||
static LPALSOURCE3DSOFT alSource3dSOFT;
|
||||
@@ -124,7 +126,7 @@ static ALuint LoadSound(const char *filename)
|
||||
free(membuf);
|
||||
sf_close(sndfile);
|
||||
|
||||
/* Check if an error occured, and clean up if so. */
|
||||
/* Check if an error occurred, and clean up if so. */
|
||||
err = alGetError();
|
||||
if(err != AL_NO_ERROR)
|
||||
{
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define SDL_MAIN_HANDLED
|
||||
#include "SDL.h"
|
||||
#include "SDL_audio.h"
|
||||
#include "SDL_error.h"
|
||||
@@ -117,7 +118,7 @@ static ALuint CreateSineWave(void)
|
||||
alGenBuffers(1, &buffer);
|
||||
alBufferData(buffer, AL_FORMAT_MONO16, data, sizeof(data), 44100);
|
||||
|
||||
/* Check if an error occured, and clean up if so. */
|
||||
/* Check if an error occurred, and clean up if so. */
|
||||
err = alGetError();
|
||||
if(err != AL_NO_ERROR)
|
||||
{
|
||||
@@ -141,6 +142,8 @@ int main(int argc, char *argv[])
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
SDL_SetMainReady();
|
||||
|
||||
/* Print out error if extension is missing. */
|
||||
if(!alcIsExtensionPresent(NULL, "ALC_SOFT_loopback"))
|
||||
{
|
||||
@@ -197,6 +200,10 @@ int main(int argc, char *argv[])
|
||||
attrs[3] = ALC_UNSIGNED_SHORT_SOFT;
|
||||
else if(obtained.format == AUDIO_S16SYS)
|
||||
attrs[3] = ALC_SHORT_SOFT;
|
||||
else if(obtained.format == AUDIO_S32SYS)
|
||||
attrs[3] = ALC_INT_SOFT;
|
||||
else if(obtained.format == AUDIO_F32SYS)
|
||||
attrs[3] = ALC_FLOAT_SOFT;
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Unhandled SDL format: 0x%04x\n", obtained.format);
|
||||
|
||||
@@ -47,6 +47,8 @@
|
||||
|
||||
#include "common/alhelpers.h"
|
||||
|
||||
#include "win_main_utf8.h"
|
||||
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
@@ -106,7 +108,8 @@ static int LoadEffect(ALuint effect, const EFXEAXREVERBPROPERTIES *reverb)
|
||||
* the needed panning vectors).
|
||||
*/
|
||||
alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_EAXREVERB);
|
||||
if((err=alGetError()) != AL_NO_ERROR)
|
||||
err = alGetError();
|
||||
if(err != AL_NO_ERROR)
|
||||
{
|
||||
fprintf(stderr, "Failed to set EAX Reverb: %s (0x%04x)\n", alGetString(err), err);
|
||||
return 0;
|
||||
@@ -137,8 +140,9 @@ static int LoadEffect(ALuint effect, const EFXEAXREVERBPROPERTIES *reverb)
|
||||
alEffectf(effect, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR, reverb->flRoomRolloffFactor);
|
||||
alEffecti(effect, AL_EAXREVERB_DECAY_HFLIMIT, reverb->iDecayHFLimit);
|
||||
|
||||
/* Check if an error occured, and return failure if so. */
|
||||
if((err=alGetError()) != AL_NO_ERROR)
|
||||
/* Check if an error occurred, and return failure if so. */
|
||||
err = alGetError();
|
||||
if(err != AL_NO_ERROR)
|
||||
{
|
||||
fprintf(stderr, "Error setting up reverb: %s\n", alGetString(err));
|
||||
return 0;
|
||||
@@ -210,7 +214,7 @@ static ALuint LoadSound(const char *filename)
|
||||
free(membuf);
|
||||
sf_close(sndfile);
|
||||
|
||||
/* Check if an error occured, and clean up if so. */
|
||||
/* Check if an error occurred, and clean up if so. */
|
||||
err = alGetError();
|
||||
if(err != AL_NO_ERROR)
|
||||
{
|
||||
@@ -493,7 +497,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
if(argc < 1)
|
||||
{
|
||||
fprintf(stderr, "No filename spacified.\n");
|
||||
fprintf(stderr, "No filename specified.\n");
|
||||
CloseAL();
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -37,19 +37,31 @@
|
||||
|
||||
#include "common/alhelpers.h"
|
||||
|
||||
#include "win_main_utf8.h"
|
||||
|
||||
|
||||
enum FormatType {
|
||||
Int16,
|
||||
Float,
|
||||
IMA4,
|
||||
MSADPCM
|
||||
};
|
||||
|
||||
/* LoadBuffer loads the named audio file into an OpenAL buffer object, and
|
||||
* returns the new buffer ID.
|
||||
*/
|
||||
static ALuint LoadSound(const char *filename)
|
||||
{
|
||||
enum FormatType sample_format = Int16;
|
||||
ALint byteblockalign = 0;
|
||||
ALint splblockalign = 0;
|
||||
sf_count_t num_frames;
|
||||
ALenum err, format;
|
||||
ALuint buffer;
|
||||
ALsizei num_bytes;
|
||||
SNDFILE *sndfile;
|
||||
SF_INFO sfinfo;
|
||||
short *membuf;
|
||||
sf_count_t num_frames;
|
||||
ALsizei num_bytes;
|
||||
ALuint buffer;
|
||||
void *membuf;
|
||||
|
||||
/* Open the audio file and check that it's usable. */
|
||||
sndfile = sf_open(filename, SFM_READ, &sfinfo);
|
||||
@@ -58,28 +70,151 @@ static ALuint LoadSound(const char *filename)
|
||||
fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
|
||||
return 0;
|
||||
}
|
||||
if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
|
||||
if(sfinfo.frames < 1)
|
||||
{
|
||||
fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
|
||||
sf_close(sndfile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Get the sound format, and figure out the OpenAL format */
|
||||
/* Detect a suitable format to load. Formats like Vorbis and Opus use float
|
||||
* natively, so load as float to avoid clipping when possible. Formats
|
||||
* larger than 16-bit can also use float to preserve a bit more precision.
|
||||
*/
|
||||
switch((sfinfo.format&SF_FORMAT_SUBMASK))
|
||||
{
|
||||
case SF_FORMAT_PCM_24:
|
||||
case SF_FORMAT_PCM_32:
|
||||
case SF_FORMAT_FLOAT:
|
||||
case SF_FORMAT_DOUBLE:
|
||||
case SF_FORMAT_VORBIS:
|
||||
case SF_FORMAT_OPUS:
|
||||
case SF_FORMAT_ALAC_20:
|
||||
case SF_FORMAT_ALAC_24:
|
||||
case SF_FORMAT_ALAC_32:
|
||||
case 0x0080/*SF_FORMAT_MPEG_LAYER_I*/:
|
||||
case 0x0081/*SF_FORMAT_MPEG_LAYER_II*/:
|
||||
case 0x0082/*SF_FORMAT_MPEG_LAYER_III*/:
|
||||
if(alIsExtensionPresent("AL_EXT_FLOAT32"))
|
||||
sample_format = Float;
|
||||
break;
|
||||
case SF_FORMAT_IMA_ADPCM:
|
||||
/* ADPCM formats require setting a block alignment as specified in the
|
||||
* file, which needs to be read from the wave 'fmt ' chunk manually
|
||||
* since libsndfile doesn't provide it in a format-agnostic way.
|
||||
*/
|
||||
if(sfinfo.channels <= 2 && (sfinfo.format&SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV
|
||||
&& alIsExtensionPresent("AL_EXT_IMA4")
|
||||
&& alIsExtensionPresent("AL_SOFT_block_alignment"))
|
||||
sample_format = IMA4;
|
||||
break;
|
||||
case SF_FORMAT_MS_ADPCM:
|
||||
if(sfinfo.channels <= 2 && (sfinfo.format&SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV
|
||||
&& alIsExtensionPresent("AL_SOFT_MSADPCM")
|
||||
&& alIsExtensionPresent("AL_SOFT_block_alignment"))
|
||||
sample_format = MSADPCM;
|
||||
break;
|
||||
}
|
||||
|
||||
if(sample_format == IMA4 || sample_format == MSADPCM)
|
||||
{
|
||||
/* For ADPCM, lookup the wave file's "fmt " chunk, which is a
|
||||
* WAVEFORMATEX-based structure for the audio format.
|
||||
*/
|
||||
SF_CHUNK_INFO inf = { "fmt ", 4, 0, NULL };
|
||||
SF_CHUNK_ITERATOR *iter = sf_get_chunk_iterator(sndfile, &inf);
|
||||
|
||||
/* If there's an issue getting the chunk or block alignment, load as
|
||||
* 16-bit and have libsndfile do the conversion.
|
||||
*/
|
||||
if(!iter || sf_get_chunk_size(iter, &inf) != SF_ERR_NO_ERROR || inf.datalen < 14)
|
||||
sample_format = Int16;
|
||||
else
|
||||
{
|
||||
ALubyte *fmtbuf = calloc(inf.datalen, 1);
|
||||
inf.data = fmtbuf;
|
||||
if(sf_get_chunk_data(iter, &inf) != SF_ERR_NO_ERROR)
|
||||
sample_format = Int16;
|
||||
else
|
||||
{
|
||||
/* Read the nBlockAlign field, and convert from bytes- to
|
||||
* samples-per-block (verifying it's valid by converting back
|
||||
* and comparing to the original value).
|
||||
*/
|
||||
byteblockalign = fmtbuf[12] | (fmtbuf[13]<<8);
|
||||
if(sample_format == IMA4)
|
||||
{
|
||||
splblockalign = (byteblockalign/sfinfo.channels - 4)/4*8 + 1;
|
||||
if(splblockalign < 1
|
||||
|| ((splblockalign-1)/2 + 4)*sfinfo.channels != byteblockalign)
|
||||
sample_format = Int16;
|
||||
}
|
||||
else
|
||||
{
|
||||
splblockalign = (byteblockalign/sfinfo.channels - 7)*2 + 2;
|
||||
if(splblockalign < 2
|
||||
|| ((splblockalign-2)/2 + 7)*sfinfo.channels != byteblockalign)
|
||||
sample_format = Int16;
|
||||
}
|
||||
}
|
||||
free(fmtbuf);
|
||||
}
|
||||
}
|
||||
|
||||
if(sample_format == Int16)
|
||||
{
|
||||
splblockalign = 1;
|
||||
byteblockalign = sfinfo.channels * 2;
|
||||
}
|
||||
else if(sample_format == Float)
|
||||
{
|
||||
splblockalign = 1;
|
||||
byteblockalign = sfinfo.channels * 4;
|
||||
}
|
||||
|
||||
/* Figure out the OpenAL format from the file and desired sample type. */
|
||||
format = AL_NONE;
|
||||
if(sfinfo.channels == 1)
|
||||
format = AL_FORMAT_MONO16;
|
||||
{
|
||||
if(sample_format == Int16)
|
||||
format = AL_FORMAT_MONO16;
|
||||
else if(sample_format == Float)
|
||||
format = AL_FORMAT_MONO_FLOAT32;
|
||||
else if(sample_format == IMA4)
|
||||
format = AL_FORMAT_MONO_IMA4;
|
||||
else if(sample_format == MSADPCM)
|
||||
format = AL_FORMAT_MONO_MSADPCM_SOFT;
|
||||
}
|
||||
else if(sfinfo.channels == 2)
|
||||
format = AL_FORMAT_STEREO16;
|
||||
{
|
||||
if(sample_format == Int16)
|
||||
format = AL_FORMAT_STEREO16;
|
||||
else if(sample_format == Float)
|
||||
format = AL_FORMAT_STEREO_FLOAT32;
|
||||
else if(sample_format == IMA4)
|
||||
format = AL_FORMAT_STEREO_IMA4;
|
||||
else if(sample_format == MSADPCM)
|
||||
format = AL_FORMAT_STEREO_MSADPCM_SOFT;
|
||||
}
|
||||
else if(sfinfo.channels == 3)
|
||||
{
|
||||
if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
|
||||
format = AL_FORMAT_BFORMAT2D_16;
|
||||
{
|
||||
if(sample_format == Int16)
|
||||
format = AL_FORMAT_BFORMAT2D_16;
|
||||
else if(sample_format == Float)
|
||||
format = AL_FORMAT_BFORMAT2D_FLOAT32;
|
||||
}
|
||||
}
|
||||
else if(sfinfo.channels == 4)
|
||||
{
|
||||
if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
|
||||
format = AL_FORMAT_BFORMAT3D_16;
|
||||
{
|
||||
if(sample_format == Int16)
|
||||
format = AL_FORMAT_BFORMAT3D_16;
|
||||
else if(sample_format == Float)
|
||||
format = AL_FORMAT_BFORMAT3D_FLOAT32;
|
||||
}
|
||||
}
|
||||
if(!format)
|
||||
{
|
||||
@@ -88,10 +223,27 @@ static ALuint LoadSound(const char *filename)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Decode the whole audio file to a buffer. */
|
||||
membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
|
||||
if(sfinfo.frames/splblockalign > (sf_count_t)(INT_MAX/byteblockalign))
|
||||
{
|
||||
fprintf(stderr, "Too many samples in %s (%" PRId64 ")\n", filename, sfinfo.frames);
|
||||
sf_close(sndfile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
|
||||
/* Decode the whole audio file to a buffer. */
|
||||
membuf = malloc((size_t)(sfinfo.frames / splblockalign * byteblockalign));
|
||||
|
||||
if(sample_format == Int16)
|
||||
num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
|
||||
else if(sample_format == Float)
|
||||
num_frames = sf_readf_float(sndfile, membuf, sfinfo.frames);
|
||||
else
|
||||
{
|
||||
sf_count_t count = sfinfo.frames / splblockalign * byteblockalign;
|
||||
num_frames = sf_read_raw(sndfile, membuf, count);
|
||||
if(num_frames > 0)
|
||||
num_frames = num_frames / byteblockalign * splblockalign;
|
||||
}
|
||||
if(num_frames < 1)
|
||||
{
|
||||
free(membuf);
|
||||
@@ -99,19 +251,24 @@ static ALuint LoadSound(const char *filename)
|
||||
fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
|
||||
return 0;
|
||||
}
|
||||
num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
|
||||
num_bytes = (ALsizei)(num_frames / splblockalign * byteblockalign);
|
||||
|
||||
printf("Loading: %s (%s, %dhz)\n", filename, FormatName(format), sfinfo.samplerate);
|
||||
fflush(stdout);
|
||||
|
||||
/* Buffer the audio data into a new buffer object, then free the data and
|
||||
* close the file.
|
||||
*/
|
||||
buffer = 0;
|
||||
alGenBuffers(1, &buffer);
|
||||
if(splblockalign > 1)
|
||||
alBufferi(buffer, AL_UNPACK_BLOCK_ALIGNMENT_SOFT, splblockalign);
|
||||
alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
|
||||
|
||||
free(membuf);
|
||||
sf_close(sndfile);
|
||||
|
||||
/* Check if an error occured, and clean up if so. */
|
||||
/* Check if an error occurred, and clean up if so. */
|
||||
err = alGetError();
|
||||
if(err != AL_NO_ERROR)
|
||||
{
|
||||
|
||||
@@ -139,7 +139,7 @@ int main(int argc, char **argv)
|
||||
char *end;
|
||||
if(strcmp(argv[0], "--") == 0)
|
||||
break;
|
||||
else if(strcmp(argv[0], "--channels") == 0 || strcmp(argv[0], "-c") == 0)
|
||||
if(strcmp(argv[0], "--channels") == 0 || strcmp(argv[0], "-c") == 0)
|
||||
{
|
||||
if(argc < 2)
|
||||
{
|
||||
|
||||
@@ -40,6 +40,8 @@
|
||||
|
||||
#include "common/alhelpers.h"
|
||||
|
||||
#include "win_main_utf8.h"
|
||||
|
||||
|
||||
/* Effect object functions */
|
||||
static LPALGENEFFECTS alGenEffects;
|
||||
@@ -132,7 +134,7 @@ static ALuint LoadEffect(const EFXEAXREVERBPROPERTIES *reverb)
|
||||
alEffecti(effect, AL_REVERB_DECAY_HFLIMIT, reverb->iDecayHFLimit);
|
||||
}
|
||||
|
||||
/* Check if an error occured, and clean up if so. */
|
||||
/* Check if an error occurred, and clean up if so. */
|
||||
err = alGetError();
|
||||
if(err != AL_NO_ERROR)
|
||||
{
|
||||
@@ -219,7 +221,7 @@ static ALuint LoadSound(const char *filename)
|
||||
free(membuf);
|
||||
sf_close(sndfile);
|
||||
|
||||
/* Check if an error occured, and clean up if so. */
|
||||
/* Check if an error occurred, and clean up if so. */
|
||||
err = alGetError();
|
||||
if(err != AL_NO_ERROR)
|
||||
{
|
||||
|
||||
@@ -37,22 +37,35 @@
|
||||
|
||||
#include "common/alhelpers.h"
|
||||
|
||||
#include "win_main_utf8.h"
|
||||
|
||||
|
||||
/* Define the number of buffers and buffer size (in milliseconds) to use. 4
|
||||
* buffers with 8192 samples each gives a nice per-chunk size, and lets the
|
||||
* queue last for almost one second at 44.1khz. */
|
||||
#define NUM_BUFFERS 4
|
||||
#define BUFFER_SAMPLES 8192
|
||||
* buffers at 200ms each gives a nice per-chunk size, and lets the queue last
|
||||
* for almost one second.
|
||||
*/
|
||||
enum { NumBuffers = 4 };
|
||||
enum { BufferMillisec = 200 };
|
||||
|
||||
typedef enum SampleType {
|
||||
Int16, Float, IMA4, MSADPCM
|
||||
} SampleType;
|
||||
|
||||
typedef struct StreamPlayer {
|
||||
/* These are the buffers and source to play out through OpenAL with */
|
||||
ALuint buffers[NUM_BUFFERS];
|
||||
/* These are the buffers and source to play out through OpenAL with. */
|
||||
ALuint buffers[NumBuffers];
|
||||
ALuint source;
|
||||
|
||||
/* Handle for the audio file */
|
||||
SNDFILE *sndfile;
|
||||
SF_INFO sfinfo;
|
||||
short *membuf;
|
||||
void *membuf;
|
||||
|
||||
/* The sample type and block/frame size being read for the buffer. */
|
||||
SampleType sample_type;
|
||||
int byteblockalign;
|
||||
int sampleblockalign;
|
||||
int block_count;
|
||||
|
||||
/* The format of the output stream (sample rate is in sfinfo) */
|
||||
ALenum format;
|
||||
@@ -67,7 +80,8 @@ static int UpdatePlayer(StreamPlayer *player);
|
||||
|
||||
/* Creates a new player object, and allocates the needed OpenAL source and
|
||||
* buffer objects. Error checking is simplified for the purposes of this
|
||||
* example, and will cause an abort if needed. */
|
||||
* example, and will cause an abort if needed.
|
||||
*/
|
||||
static StreamPlayer *NewPlayer(void)
|
||||
{
|
||||
StreamPlayer *player;
|
||||
@@ -76,7 +90,7 @@ static StreamPlayer *NewPlayer(void)
|
||||
assert(player != NULL);
|
||||
|
||||
/* Generate the buffers and source */
|
||||
alGenBuffers(NUM_BUFFERS, player->buffers);
|
||||
alGenBuffers(NumBuffers, player->buffers);
|
||||
assert(alGetError() == AL_NO_ERROR && "Could not create buffers");
|
||||
|
||||
alGenSources(1, &player->source);
|
||||
@@ -99,11 +113,11 @@ static void DeletePlayer(StreamPlayer *player)
|
||||
ClosePlayerFile(player);
|
||||
|
||||
alDeleteSources(1, &player->source);
|
||||
alDeleteBuffers(NUM_BUFFERS, player->buffers);
|
||||
alDeleteBuffers(NumBuffers, player->buffers);
|
||||
if(alGetError() != AL_NO_ERROR)
|
||||
fprintf(stderr, "Failed to delete object IDs\n");
|
||||
|
||||
memset(player, 0, sizeof(*player));
|
||||
memset(player, 0, sizeof(*player)); /* NOLINT(clang-analyzer-security.insecureAPI.*) */
|
||||
free(player);
|
||||
}
|
||||
|
||||
@@ -112,7 +126,7 @@ static void DeletePlayer(StreamPlayer *player)
|
||||
* it will be closed first. */
|
||||
static int OpenPlayerFile(StreamPlayer *player, const char *filename)
|
||||
{
|
||||
size_t frame_size;
|
||||
int byteblockalign=0, splblockalign=0;
|
||||
|
||||
ClosePlayerFile(player);
|
||||
|
||||
@@ -124,20 +138,151 @@ static int OpenPlayerFile(StreamPlayer *player, const char *filename)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Get the sound format, and figure out the OpenAL format */
|
||||
/* Detect a suitable format to load. Formats like Vorbis and Opus use float
|
||||
* natively, so load as float to avoid clipping when possible. Formats
|
||||
* larger than 16-bit can also use float to preserve a bit more precision.
|
||||
*/
|
||||
switch((player->sfinfo.format&SF_FORMAT_SUBMASK))
|
||||
{
|
||||
case SF_FORMAT_PCM_24:
|
||||
case SF_FORMAT_PCM_32:
|
||||
case SF_FORMAT_FLOAT:
|
||||
case SF_FORMAT_DOUBLE:
|
||||
case SF_FORMAT_VORBIS:
|
||||
case SF_FORMAT_OPUS:
|
||||
case SF_FORMAT_ALAC_20:
|
||||
case SF_FORMAT_ALAC_24:
|
||||
case SF_FORMAT_ALAC_32:
|
||||
case 0x0080/*SF_FORMAT_MPEG_LAYER_I*/:
|
||||
case 0x0081/*SF_FORMAT_MPEG_LAYER_II*/:
|
||||
case 0x0082/*SF_FORMAT_MPEG_LAYER_III*/:
|
||||
if(alIsExtensionPresent("AL_EXT_FLOAT32"))
|
||||
player->sample_type = Float;
|
||||
break;
|
||||
case SF_FORMAT_IMA_ADPCM:
|
||||
/* ADPCM formats require setting a block alignment as specified in the
|
||||
* file, which needs to be read from the wave 'fmt ' chunk manually
|
||||
* since libsndfile doesn't provide it in a format-agnostic way.
|
||||
*/
|
||||
if(player->sfinfo.channels <= 2
|
||||
&& (player->sfinfo.format&SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV
|
||||
&& alIsExtensionPresent("AL_EXT_IMA4")
|
||||
&& alIsExtensionPresent("AL_SOFT_block_alignment"))
|
||||
player->sample_type = IMA4;
|
||||
break;
|
||||
case SF_FORMAT_MS_ADPCM:
|
||||
if(player->sfinfo.channels <= 2
|
||||
&& (player->sfinfo.format&SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV
|
||||
&& alIsExtensionPresent("AL_SOFT_MSADPCM")
|
||||
&& alIsExtensionPresent("AL_SOFT_block_alignment"))
|
||||
player->sample_type = MSADPCM;
|
||||
break;
|
||||
}
|
||||
|
||||
if(player->sample_type == IMA4 || player->sample_type == MSADPCM)
|
||||
{
|
||||
/* For ADPCM, lookup the wave file's "fmt " chunk, which is a
|
||||
* WAVEFORMATEX-based structure for the audio format.
|
||||
*/
|
||||
SF_CHUNK_INFO inf = { "fmt ", 4, 0, NULL };
|
||||
SF_CHUNK_ITERATOR *iter = sf_get_chunk_iterator(player->sndfile, &inf);
|
||||
|
||||
/* If there's an issue getting the chunk or block alignment, load as
|
||||
* 16-bit and have libsndfile do the conversion.
|
||||
*/
|
||||
if(!iter || sf_get_chunk_size(iter, &inf) != SF_ERR_NO_ERROR || inf.datalen < 14)
|
||||
player->sample_type = Int16;
|
||||
else
|
||||
{
|
||||
ALubyte *fmtbuf = calloc(inf.datalen, 1);
|
||||
inf.data = fmtbuf;
|
||||
if(sf_get_chunk_data(iter, &inf) != SF_ERR_NO_ERROR)
|
||||
player->sample_type = Int16;
|
||||
else
|
||||
{
|
||||
/* Read the nBlockAlign field, and convert from bytes- to
|
||||
* samples-per-block (verifying it's valid by converting back
|
||||
* and comparing to the original value).
|
||||
*/
|
||||
byteblockalign = fmtbuf[12] | (fmtbuf[13]<<8);
|
||||
if(player->sample_type == IMA4)
|
||||
{
|
||||
splblockalign = (byteblockalign/player->sfinfo.channels - 4)/4*8 + 1;
|
||||
if(splblockalign < 1
|
||||
|| ((splblockalign-1)/2 + 4)*player->sfinfo.channels != byteblockalign)
|
||||
player->sample_type = Int16;
|
||||
}
|
||||
else
|
||||
{
|
||||
splblockalign = (byteblockalign/player->sfinfo.channels - 7)*2 + 2;
|
||||
if(splblockalign < 2
|
||||
|| ((splblockalign-2)/2 + 7)*player->sfinfo.channels != byteblockalign)
|
||||
player->sample_type = Int16;
|
||||
}
|
||||
}
|
||||
free(fmtbuf);
|
||||
}
|
||||
}
|
||||
|
||||
if(player->sample_type == Int16)
|
||||
{
|
||||
player->sampleblockalign = 1;
|
||||
player->byteblockalign = player->sfinfo.channels * 2;
|
||||
}
|
||||
else if(player->sample_type == Float)
|
||||
{
|
||||
player->sampleblockalign = 1;
|
||||
player->byteblockalign = player->sfinfo.channels * 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
player->sampleblockalign = splblockalign;
|
||||
player->byteblockalign = byteblockalign;
|
||||
}
|
||||
|
||||
/* Figure out the OpenAL format from the file and desired sample type. */
|
||||
player->format = AL_NONE;
|
||||
if(player->sfinfo.channels == 1)
|
||||
player->format = AL_FORMAT_MONO16;
|
||||
{
|
||||
if(player->sample_type == Int16)
|
||||
player->format = AL_FORMAT_MONO16;
|
||||
else if(player->sample_type == Float)
|
||||
player->format = AL_FORMAT_MONO_FLOAT32;
|
||||
else if(player->sample_type == IMA4)
|
||||
player->format = AL_FORMAT_MONO_IMA4;
|
||||
else if(player->sample_type == MSADPCM)
|
||||
player->format = AL_FORMAT_MONO_MSADPCM_SOFT;
|
||||
}
|
||||
else if(player->sfinfo.channels == 2)
|
||||
player->format = AL_FORMAT_STEREO16;
|
||||
{
|
||||
if(player->sample_type == Int16)
|
||||
player->format = AL_FORMAT_STEREO16;
|
||||
else if(player->sample_type == Float)
|
||||
player->format = AL_FORMAT_STEREO_FLOAT32;
|
||||
else if(player->sample_type == IMA4)
|
||||
player->format = AL_FORMAT_STEREO_IMA4;
|
||||
else if(player->sample_type == MSADPCM)
|
||||
player->format = AL_FORMAT_STEREO_MSADPCM_SOFT;
|
||||
}
|
||||
else if(player->sfinfo.channels == 3)
|
||||
{
|
||||
if(sf_command(player->sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
|
||||
player->format = AL_FORMAT_BFORMAT2D_16;
|
||||
{
|
||||
if(player->sample_type == Int16)
|
||||
player->format = AL_FORMAT_BFORMAT2D_16;
|
||||
else if(player->sample_type == Float)
|
||||
player->format = AL_FORMAT_BFORMAT2D_FLOAT32;
|
||||
}
|
||||
}
|
||||
else if(player->sfinfo.channels == 4)
|
||||
{
|
||||
if(sf_command(player->sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
|
||||
player->format = AL_FORMAT_BFORMAT3D_16;
|
||||
{
|
||||
if(player->sample_type == Int16)
|
||||
player->format = AL_FORMAT_BFORMAT3D_16;
|
||||
else if(player->sample_type == Float)
|
||||
player->format = AL_FORMAT_BFORMAT3D_FLOAT32;
|
||||
}
|
||||
}
|
||||
if(!player->format)
|
||||
{
|
||||
@@ -147,8 +292,9 @@ static int OpenPlayerFile(StreamPlayer *player, const char *filename)
|
||||
return 0;
|
||||
}
|
||||
|
||||
frame_size = (size_t)(BUFFER_SAMPLES * player->sfinfo.channels) * sizeof(short);
|
||||
player->membuf = malloc(frame_size);
|
||||
player->block_count = player->sfinfo.samplerate / player->sampleblockalign;
|
||||
player->block_count = player->block_count * BufferMillisec / 1000;
|
||||
player->membuf = malloc((size_t)player->block_count * (size_t)player->byteblockalign);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -162,6 +308,15 @@ static void ClosePlayerFile(StreamPlayer *player)
|
||||
|
||||
free(player->membuf);
|
||||
player->membuf = NULL;
|
||||
|
||||
if(player->sampleblockalign > 1)
|
||||
{
|
||||
ALsizei i;
|
||||
for(i = 0;i < NumBuffers;i++)
|
||||
alBufferi(player->buffers[i], AL_UNPACK_BLOCK_ALIGNMENT_SOFT, 0);
|
||||
player->sampleblockalign = 0;
|
||||
player->byteblockalign = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -175,13 +330,37 @@ static int StartPlayer(StreamPlayer *player)
|
||||
alSourcei(player->source, AL_BUFFER, 0);
|
||||
|
||||
/* Fill the buffer queue */
|
||||
for(i = 0;i < NUM_BUFFERS;i++)
|
||||
for(i = 0;i < NumBuffers;i++)
|
||||
{
|
||||
/* Get some data to give it to the buffer */
|
||||
sf_count_t slen = sf_readf_short(player->sndfile, player->membuf, BUFFER_SAMPLES);
|
||||
if(slen < 1) break;
|
||||
sf_count_t slen;
|
||||
|
||||
/* Get some data to give it to the buffer */
|
||||
if(player->sample_type == Int16)
|
||||
{
|
||||
slen = sf_readf_short(player->sndfile, player->membuf,
|
||||
(sf_count_t)player->block_count * player->sampleblockalign);
|
||||
if(slen < 1) break;
|
||||
slen *= player->byteblockalign;
|
||||
}
|
||||
else if(player->sample_type == Float)
|
||||
{
|
||||
slen = sf_readf_float(player->sndfile, player->membuf,
|
||||
(sf_count_t)player->block_count * player->sampleblockalign);
|
||||
if(slen < 1) break;
|
||||
slen *= player->byteblockalign;
|
||||
}
|
||||
else
|
||||
{
|
||||
slen = sf_read_raw(player->sndfile, player->membuf,
|
||||
(sf_count_t)player->block_count * player->byteblockalign);
|
||||
if(slen > 0) slen -= slen%player->byteblockalign;
|
||||
if(slen < 1) break;
|
||||
}
|
||||
|
||||
if(player->sampleblockalign > 1)
|
||||
alBufferi(player->buffers[i], AL_UNPACK_BLOCK_ALIGNMENT_SOFT,
|
||||
player->sampleblockalign);
|
||||
|
||||
slen *= player->sfinfo.channels * (sf_count_t)sizeof(short);
|
||||
alBufferData(player->buffers[i], player->format, player->membuf, (ALsizei)slen,
|
||||
player->sfinfo.samplerate);
|
||||
}
|
||||
@@ -227,10 +406,27 @@ static int UpdatePlayer(StreamPlayer *player)
|
||||
|
||||
/* Read the next chunk of data, refill the buffer, and queue it
|
||||
* back on the source */
|
||||
slen = sf_readf_short(player->sndfile, player->membuf, BUFFER_SAMPLES);
|
||||
if(player->sample_type == Int16)
|
||||
{
|
||||
slen = sf_readf_short(player->sndfile, player->membuf,
|
||||
(sf_count_t)player->block_count * player->sampleblockalign);
|
||||
if(slen > 0) slen *= player->byteblockalign;
|
||||
}
|
||||
else if(player->sample_type == Float)
|
||||
{
|
||||
slen = sf_readf_float(player->sndfile, player->membuf,
|
||||
(sf_count_t)player->block_count * player->sampleblockalign);
|
||||
if(slen > 0) slen *= player->byteblockalign;
|
||||
}
|
||||
else
|
||||
{
|
||||
slen = sf_read_raw(player->sndfile, player->membuf,
|
||||
(sf_count_t)player->block_count * player->byteblockalign);
|
||||
if(slen > 0) slen -= slen%player->byteblockalign;
|
||||
}
|
||||
|
||||
if(slen > 0)
|
||||
{
|
||||
slen *= player->sfinfo.channels * (sf_count_t)sizeof(short);
|
||||
alBufferData(bufid, player->format, player->membuf, (ALsizei)slen,
|
||||
player->sfinfo.samplerate);
|
||||
alSourceQueueBuffers(player->source, 1, &bufid);
|
||||
@@ -292,10 +488,9 @@ int main(int argc, char **argv)
|
||||
|
||||
/* Get the name portion, without the path, for display. */
|
||||
namepart = strrchr(argv[i], '/');
|
||||
if(namepart || (namepart=strrchr(argv[i], '\\')))
|
||||
namepart++;
|
||||
else
|
||||
namepart = argv[i];
|
||||
if(!namepart) namepart = strrchr(argv[i], '\\');
|
||||
if(!namepart) namepart = argv[i];
|
||||
else namepart++;
|
||||
|
||||
printf("Playing: %s (%s, %dhz)\n", namepart, FormatName(player->format),
|
||||
player->sfinfo.samplerate);
|
||||
|
||||
@@ -24,12 +24,12 @@
|
||||
|
||||
/* This file contains a streaming audio player using a callback buffer. */
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
@@ -44,6 +44,8 @@
|
||||
|
||||
#include "common/alhelpers.h"
|
||||
|
||||
#include "win_main_utf8.h"
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -56,10 +58,16 @@ struct StreamPlayer {
|
||||
/* A lockless ring-buffer (supports single-provider, single-consumer
|
||||
* operation).
|
||||
*/
|
||||
std::unique_ptr<ALbyte[]> mBufferData;
|
||||
size_t mBufferDataSize{0};
|
||||
std::vector<ALbyte> mBufferData;
|
||||
std::atomic<size_t> mReadPos{0};
|
||||
std::atomic<size_t> mWritePos{0};
|
||||
size_t mSamplesPerBlock{1};
|
||||
size_t mBytesPerBlock{1};
|
||||
|
||||
enum class SampleType {
|
||||
Int16, Float, IMA4, MSADPCM
|
||||
};
|
||||
SampleType mSampleFormat{SampleType::Int16};
|
||||
|
||||
/* The buffer to get the callback, and source to play with. */
|
||||
ALuint mBuffer{0}, mSource{0};
|
||||
@@ -71,15 +79,15 @@ struct StreamPlayer {
|
||||
size_t mDecoderOffset{0};
|
||||
|
||||
/* The format of the callback samples. */
|
||||
ALenum mFormat;
|
||||
ALenum mFormat{};
|
||||
|
||||
StreamPlayer()
|
||||
{
|
||||
alGenBuffers(1, &mBuffer);
|
||||
if(ALenum err{alGetError()})
|
||||
if(alGetError() != AL_NO_ERROR)
|
||||
throw std::runtime_error{"alGenBuffers failed"};
|
||||
alGenSources(1, &mSource);
|
||||
if(ALenum err{alGetError()})
|
||||
if(alGetError() != AL_NO_ERROR)
|
||||
{
|
||||
alDeleteBuffers(1, &mBuffer);
|
||||
throw std::runtime_error{"alGenSources failed"};
|
||||
@@ -95,6 +103,9 @@ struct StreamPlayer {
|
||||
|
||||
void close()
|
||||
{
|
||||
if(mSamplesPerBlock > 1)
|
||||
alBufferi(mBuffer, AL_UNPACK_BLOCK_ALIGNMENT_SOFT, 0);
|
||||
|
||||
if(mSndfile)
|
||||
{
|
||||
alSourceRewind(mSource);
|
||||
@@ -116,20 +127,129 @@ struct StreamPlayer {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch((mSfInfo.format&SF_FORMAT_SUBMASK))
|
||||
{
|
||||
case SF_FORMAT_PCM_24:
|
||||
case SF_FORMAT_PCM_32:
|
||||
case SF_FORMAT_FLOAT:
|
||||
case SF_FORMAT_DOUBLE:
|
||||
case SF_FORMAT_VORBIS:
|
||||
case SF_FORMAT_OPUS:
|
||||
case SF_FORMAT_ALAC_20:
|
||||
case SF_FORMAT_ALAC_24:
|
||||
case SF_FORMAT_ALAC_32:
|
||||
case 0x0080/*SF_FORMAT_MPEG_LAYER_I*/:
|
||||
case 0x0081/*SF_FORMAT_MPEG_LAYER_II*/:
|
||||
case 0x0082/*SF_FORMAT_MPEG_LAYER_III*/:
|
||||
if(alIsExtensionPresent("AL_EXT_FLOAT32"))
|
||||
mSampleFormat = SampleType::Float;
|
||||
break;
|
||||
case SF_FORMAT_IMA_ADPCM:
|
||||
if(mSfInfo.channels <= 2 && (mSfInfo.format&SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV
|
||||
&& alIsExtensionPresent("AL_EXT_IMA4")
|
||||
&& alIsExtensionPresent("AL_SOFT_block_alignment"))
|
||||
mSampleFormat = SampleType::IMA4;
|
||||
break;
|
||||
case SF_FORMAT_MS_ADPCM:
|
||||
if(mSfInfo.channels <= 2 && (mSfInfo.format&SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV
|
||||
&& alIsExtensionPresent("AL_SOFT_MSADPCM")
|
||||
&& alIsExtensionPresent("AL_SOFT_block_alignment"))
|
||||
mSampleFormat = SampleType::MSADPCM;
|
||||
break;
|
||||
}
|
||||
|
||||
int splblocksize{}, byteblocksize{};
|
||||
if(mSampleFormat == SampleType::IMA4 || mSampleFormat == SampleType::MSADPCM)
|
||||
{
|
||||
SF_CHUNK_INFO inf{ "fmt ", 4, 0, nullptr };
|
||||
SF_CHUNK_ITERATOR *iter = sf_get_chunk_iterator(mSndfile, &inf);
|
||||
if(!iter || sf_get_chunk_size(iter, &inf) != SF_ERR_NO_ERROR || inf.datalen < 14)
|
||||
mSampleFormat = SampleType::Int16;
|
||||
else
|
||||
{
|
||||
auto fmtbuf = std::vector<ALubyte>(inf.datalen);
|
||||
inf.data = fmtbuf.data();
|
||||
if(sf_get_chunk_data(iter, &inf) != SF_ERR_NO_ERROR)
|
||||
mSampleFormat = SampleType::Int16;
|
||||
else
|
||||
{
|
||||
byteblocksize = fmtbuf[12] | (fmtbuf[13]<<8u);
|
||||
if(mSampleFormat == SampleType::IMA4)
|
||||
{
|
||||
splblocksize = (byteblocksize/mSfInfo.channels - 4)/4*8 + 1;
|
||||
if(splblocksize < 1
|
||||
|| ((splblocksize-1)/2 + 4)*mSfInfo.channels != byteblocksize)
|
||||
mSampleFormat = SampleType::Int16;
|
||||
}
|
||||
else
|
||||
{
|
||||
splblocksize = (byteblocksize/mSfInfo.channels - 7)*2 + 2;
|
||||
if(splblocksize < 2
|
||||
|| ((splblocksize-2)/2 + 7)*mSfInfo.channels != byteblocksize)
|
||||
mSampleFormat = SampleType::Int16;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(mSampleFormat == SampleType::Int16)
|
||||
{
|
||||
mSamplesPerBlock = 1;
|
||||
mBytesPerBlock = static_cast<size_t>(mSfInfo.channels) * 2;
|
||||
}
|
||||
else if(mSampleFormat == SampleType::Float)
|
||||
{
|
||||
mSamplesPerBlock = 1;
|
||||
mBytesPerBlock = static_cast<size_t>(mSfInfo.channels) * 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
mSamplesPerBlock = static_cast<size_t>(splblocksize);
|
||||
mBytesPerBlock = static_cast<size_t>(byteblocksize);
|
||||
}
|
||||
|
||||
mFormat = AL_NONE;
|
||||
if(mSfInfo.channels == 1)
|
||||
mFormat = AL_FORMAT_MONO_FLOAT32;
|
||||
{
|
||||
if(mSampleFormat == SampleType::Int16)
|
||||
mFormat = AL_FORMAT_MONO16;
|
||||
else if(mSampleFormat == SampleType::Float)
|
||||
mFormat = AL_FORMAT_MONO_FLOAT32;
|
||||
else if(mSampleFormat == SampleType::IMA4)
|
||||
mFormat = AL_FORMAT_MONO_IMA4;
|
||||
else if(mSampleFormat == SampleType::MSADPCM)
|
||||
mFormat = AL_FORMAT_MONO_MSADPCM_SOFT;
|
||||
}
|
||||
else if(mSfInfo.channels == 2)
|
||||
mFormat = AL_FORMAT_STEREO_FLOAT32;
|
||||
{
|
||||
if(mSampleFormat == SampleType::Int16)
|
||||
mFormat = AL_FORMAT_STEREO16;
|
||||
else if(mSampleFormat == SampleType::Float)
|
||||
mFormat = AL_FORMAT_STEREO_FLOAT32;
|
||||
else if(mSampleFormat == SampleType::IMA4)
|
||||
mFormat = AL_FORMAT_STEREO_IMA4;
|
||||
else if(mSampleFormat == SampleType::MSADPCM)
|
||||
mFormat = AL_FORMAT_STEREO_MSADPCM_SOFT;
|
||||
}
|
||||
else if(mSfInfo.channels == 3)
|
||||
{
|
||||
if(sf_command(mSndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
|
||||
mFormat = AL_FORMAT_BFORMAT2D_FLOAT32;
|
||||
if(sf_command(mSndfile, SFC_WAVEX_GET_AMBISONIC, nullptr, 0) == SF_AMBISONIC_B_FORMAT)
|
||||
{
|
||||
if(mSampleFormat == SampleType::Int16)
|
||||
mFormat = AL_FORMAT_BFORMAT2D_16;
|
||||
else if(mSampleFormat == SampleType::Float)
|
||||
mFormat = AL_FORMAT_BFORMAT2D_FLOAT32;
|
||||
}
|
||||
}
|
||||
else if(mSfInfo.channels == 4)
|
||||
{
|
||||
if(sf_command(mSndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
|
||||
mFormat = AL_FORMAT_BFORMAT3D_FLOAT32;
|
||||
if(sf_command(mSndfile, SFC_WAVEX_GET_AMBISONIC, nullptr, 0) == SF_AMBISONIC_B_FORMAT)
|
||||
{
|
||||
if(mSampleFormat == SampleType::Int16)
|
||||
mFormat = AL_FORMAT_BFORMAT3D_16;
|
||||
else if(mSampleFormat == SampleType::Float)
|
||||
mFormat = AL_FORMAT_BFORMAT3D_FLOAT32;
|
||||
}
|
||||
}
|
||||
if(!mFormat)
|
||||
{
|
||||
@@ -141,8 +261,9 @@ struct StreamPlayer {
|
||||
}
|
||||
|
||||
/* Set a 1s ring buffer size. */
|
||||
mBufferDataSize = static_cast<ALuint>(mSfInfo.samplerate*mSfInfo.channels) * sizeof(float);
|
||||
mBufferData.reset(new ALbyte[mBufferDataSize]);
|
||||
size_t numblocks{(static_cast<ALuint>(mSfInfo.samplerate) + mSamplesPerBlock-1)
|
||||
/ mSamplesPerBlock};
|
||||
mBufferData.resize(static_cast<ALuint>(numblocks * mBytesPerBlock));
|
||||
mReadPos.store(0, std::memory_order_relaxed);
|
||||
mWritePos.store(0, std::memory_order_relaxed);
|
||||
mDecoderOffset = 0;
|
||||
@@ -155,9 +276,9 @@ struct StreamPlayer {
|
||||
* but it allows the callback implementation to have a nice 'this' pointer
|
||||
* with normal member access.
|
||||
*/
|
||||
static ALsizei AL_APIENTRY bufferCallbackC(void *userptr, void *data, ALsizei size)
|
||||
static ALsizei AL_APIENTRY bufferCallbackC(void *userptr, void *data, ALsizei size) noexcept
|
||||
{ return static_cast<StreamPlayer*>(userptr)->bufferCallback(data, size); }
|
||||
ALsizei bufferCallback(void *data, ALsizei size)
|
||||
ALsizei bufferCallback(void *data, ALsizei size) noexcept
|
||||
{
|
||||
/* NOTE: The callback *MUST* be real-time safe! That means no blocking,
|
||||
* no allocations or deallocations, no I/O, no page faults, or calls to
|
||||
@@ -182,7 +303,7 @@ struct StreamPlayer {
|
||||
* that case, otherwise read up to the write offset. Also limit the
|
||||
* amount to copy given how much is remaining to write.
|
||||
*/
|
||||
size_t todo{((woffset < roffset) ? mBufferDataSize : woffset) - roffset};
|
||||
size_t todo{((woffset < roffset) ? mBufferData.size() : woffset) - roffset};
|
||||
todo = std::min<size_t>(todo, static_cast<ALuint>(size-got));
|
||||
|
||||
/* Copy from the ring buffer to the provided output buffer. Wrap
|
||||
@@ -194,7 +315,7 @@ struct StreamPlayer {
|
||||
got += static_cast<ALsizei>(todo);
|
||||
|
||||
roffset += todo;
|
||||
if(roffset == mBufferDataSize)
|
||||
if(roffset == mBufferData.size())
|
||||
roffset = 0;
|
||||
}
|
||||
/* Finally, store the updated read offset, and return how many bytes
|
||||
@@ -207,6 +328,8 @@ struct StreamPlayer {
|
||||
|
||||
bool prepare()
|
||||
{
|
||||
if(mSamplesPerBlock > 1)
|
||||
alBufferi(mBuffer, AL_UNPACK_BLOCK_ALIGNMENT_SOFT, static_cast<int>(mSamplesPerBlock));
|
||||
alBufferCallbackSOFT(mBuffer, mFormat, mSfInfo.samplerate, bufferCallbackC, this);
|
||||
alSourcei(mSource, AL_BUFFER, static_cast<ALint>(mBuffer));
|
||||
if(ALenum err{alGetError()})
|
||||
@@ -224,22 +347,22 @@ struct StreamPlayer {
|
||||
alGetSourcei(mSource, AL_SAMPLE_OFFSET, &pos);
|
||||
alGetSourcei(mSource, AL_SOURCE_STATE, &state);
|
||||
|
||||
const size_t frame_size{static_cast<ALuint>(mSfInfo.channels) * sizeof(float)};
|
||||
size_t woffset{mWritePos.load(std::memory_order_acquire)};
|
||||
if(state != AL_INITIAL)
|
||||
{
|
||||
const size_t roffset{mReadPos.load(std::memory_order_relaxed)};
|
||||
const size_t readable{((woffset >= roffset) ? woffset : (mBufferDataSize+woffset)) -
|
||||
const size_t readable{((woffset >= roffset) ? woffset : (mBufferData.size()+woffset)) -
|
||||
roffset};
|
||||
/* For a stopped (underrun) source, the current playback offset is
|
||||
* the current decoder offset excluding the readable buffered data.
|
||||
* For a playing/paused source, it's the source's offset including
|
||||
* the playback offset the source was started with.
|
||||
*/
|
||||
const size_t curtime{((state==AL_STOPPED) ? (mDecoderOffset-readable) / frame_size
|
||||
: (static_cast<ALuint>(pos) + mStartOffset/frame_size))
|
||||
const size_t curtime{((state == AL_STOPPED)
|
||||
? (mDecoderOffset-readable) / mBytesPerBlock * mSamplesPerBlock
|
||||
: (static_cast<ALuint>(pos) + mStartOffset/mBytesPerBlock*mSamplesPerBlock))
|
||||
/ static_cast<ALuint>(mSfInfo.samplerate)};
|
||||
printf("\r%3zus (%3zu%% full)", curtime, readable * 100 / mBufferDataSize);
|
||||
printf("\r%3zus (%3zu%% full)", curtime, readable * 100 / mBufferData.size());
|
||||
}
|
||||
else
|
||||
fputs("Starting...", stdout);
|
||||
@@ -256,15 +379,33 @@ struct StreamPlayer {
|
||||
* at the read offset would be interpreted as being empty
|
||||
* instead of full.
|
||||
*/
|
||||
const size_t writable{roffset-woffset-1};
|
||||
if(writable < frame_size) break;
|
||||
const size_t writable{(roffset-woffset-1) / mBytesPerBlock};
|
||||
if(!writable) break;
|
||||
|
||||
sf_count_t num_frames{sf_readf_float(mSndfile,
|
||||
reinterpret_cast<float*>(&mBufferData[woffset]),
|
||||
static_cast<sf_count_t>(writable/frame_size))};
|
||||
if(num_frames < 1) break;
|
||||
if(mSampleFormat == SampleType::Int16)
|
||||
{
|
||||
sf_count_t num_frames{sf_readf_short(mSndfile,
|
||||
reinterpret_cast<short*>(&mBufferData[woffset]),
|
||||
static_cast<sf_count_t>(writable*mSamplesPerBlock))};
|
||||
if(num_frames < 1) break;
|
||||
read_bytes = static_cast<size_t>(num_frames) * mBytesPerBlock;
|
||||
}
|
||||
else if(mSampleFormat == SampleType::Float)
|
||||
{
|
||||
sf_count_t num_frames{sf_readf_float(mSndfile,
|
||||
reinterpret_cast<float*>(&mBufferData[woffset]),
|
||||
static_cast<sf_count_t>(writable*mSamplesPerBlock))};
|
||||
if(num_frames < 1) break;
|
||||
read_bytes = static_cast<size_t>(num_frames) * mBytesPerBlock;
|
||||
}
|
||||
else
|
||||
{
|
||||
sf_count_t numbytes{sf_read_raw(mSndfile, &mBufferData[woffset],
|
||||
static_cast<sf_count_t>(writable*mBytesPerBlock))};
|
||||
if(numbytes < 1) break;
|
||||
read_bytes = static_cast<size_t>(numbytes);
|
||||
}
|
||||
|
||||
read_bytes = static_cast<size_t>(num_frames) * frame_size;
|
||||
woffset += read_bytes;
|
||||
}
|
||||
else
|
||||
@@ -274,18 +415,36 @@ struct StreamPlayer {
|
||||
* data can fit, and calculate how much can go in front before
|
||||
* wrapping.
|
||||
*/
|
||||
const size_t writable{!roffset ? mBufferDataSize-woffset-1 :
|
||||
(mBufferDataSize-woffset)};
|
||||
if(writable < frame_size) break;
|
||||
const size_t writable{(!roffset ? mBufferData.size()-woffset-1 :
|
||||
(mBufferData.size()-woffset)) / mBytesPerBlock};
|
||||
if(!writable) break;
|
||||
|
||||
sf_count_t num_frames{sf_readf_float(mSndfile,
|
||||
reinterpret_cast<float*>(&mBufferData[woffset]),
|
||||
static_cast<sf_count_t>(writable/frame_size))};
|
||||
if(num_frames < 1) break;
|
||||
if(mSampleFormat == SampleType::Int16)
|
||||
{
|
||||
sf_count_t num_frames{sf_readf_short(mSndfile,
|
||||
reinterpret_cast<short*>(&mBufferData[woffset]),
|
||||
static_cast<sf_count_t>(writable*mSamplesPerBlock))};
|
||||
if(num_frames < 1) break;
|
||||
read_bytes = static_cast<size_t>(num_frames) * mBytesPerBlock;
|
||||
}
|
||||
else if(mSampleFormat == SampleType::Float)
|
||||
{
|
||||
sf_count_t num_frames{sf_readf_float(mSndfile,
|
||||
reinterpret_cast<float*>(&mBufferData[woffset]),
|
||||
static_cast<sf_count_t>(writable*mSamplesPerBlock))};
|
||||
if(num_frames < 1) break;
|
||||
read_bytes = static_cast<size_t>(num_frames) * mBytesPerBlock;
|
||||
}
|
||||
else
|
||||
{
|
||||
sf_count_t numbytes{sf_read_raw(mSndfile, &mBufferData[woffset],
|
||||
static_cast<sf_count_t>(writable*mBytesPerBlock))};
|
||||
if(numbytes < 1) break;
|
||||
read_bytes = static_cast<size_t>(numbytes);
|
||||
}
|
||||
|
||||
read_bytes = static_cast<size_t>(num_frames) * frame_size;
|
||||
woffset += read_bytes;
|
||||
if(woffset == mBufferDataSize)
|
||||
if(woffset == mBufferData.size())
|
||||
woffset = 0;
|
||||
}
|
||||
mWritePos.store(woffset, std::memory_order_release);
|
||||
@@ -300,7 +459,7 @@ struct StreamPlayer {
|
||||
* what's available.
|
||||
*/
|
||||
const size_t roffset{mReadPos.load(std::memory_order_relaxed)};
|
||||
const size_t readable{((woffset >= roffset) ? woffset : (mBufferDataSize+woffset)) -
|
||||
const size_t readable{((woffset >= roffset) ? woffset : (mBufferData.size()+woffset)) -
|
||||
roffset};
|
||||
if(readable == 0)
|
||||
return false;
|
||||
@@ -363,7 +522,8 @@ int main(int argc, char **argv)
|
||||
|
||||
/* Get the name portion, without the path, for display. */
|
||||
const char *namepart{strrchr(argv[i], '/')};
|
||||
if(namepart || (namepart=strrchr(argv[i], '\\')))
|
||||
if(!namepart) namepart = strrchr(argv[i], '\\');
|
||||
if(namepart)
|
||||
++namepart;
|
||||
else
|
||||
namepart = argv[i];
|
||||
|
||||
@@ -79,63 +79,72 @@ static inline ALuint dither_rng(ALuint *seed)
|
||||
return *seed;
|
||||
}
|
||||
|
||||
static void ApplySin(ALfloat *data, ALdouble g, ALuint srate, ALuint freq)
|
||||
static void ApplySin(ALfloat *data, ALuint length, ALdouble g, ALuint srate, ALuint freq)
|
||||
{
|
||||
ALdouble smps_per_cycle = (ALdouble)srate / freq;
|
||||
ALdouble cycles_per_sample = (ALdouble)freq / srate;
|
||||
ALuint i;
|
||||
for(i = 0;i < srate;i++)
|
||||
for(i = 0;i < length;i++)
|
||||
{
|
||||
ALdouble ival;
|
||||
data[i] += (ALfloat)(sin(modf(i/smps_per_cycle, &ival) * 2.0*M_PI) * g);
|
||||
data[i] += (ALfloat)(sin(modf(i*cycles_per_sample, &ival) * 2.0*M_PI) * g);
|
||||
}
|
||||
}
|
||||
|
||||
/* Generates waveforms using additive synthesis. Each waveform is constructed
|
||||
* by summing one or more sine waves, up to (and excluding) nyquist.
|
||||
*/
|
||||
static ALuint CreateWave(enum WaveType type, ALuint freq, ALuint srate, ALfloat gain)
|
||||
static ALuint CreateWave(enum WaveType type, ALuint seconds, ALuint freq, ALuint srate,
|
||||
ALfloat gain)
|
||||
{
|
||||
ALuint seed = 22222;
|
||||
ALuint num_samples;
|
||||
ALuint data_size;
|
||||
ALfloat *data;
|
||||
ALuint buffer;
|
||||
ALenum err;
|
||||
ALuint i;
|
||||
|
||||
data_size = (ALuint)(srate * sizeof(ALfloat));
|
||||
if(seconds > INT_MAX / srate / sizeof(ALfloat))
|
||||
{
|
||||
fprintf(stderr, "Too many seconds: %u * %u * %zu > %d\n", seconds, srate, sizeof(ALfloat),
|
||||
INT_MAX);
|
||||
return 0;
|
||||
}
|
||||
|
||||
num_samples = seconds * srate;
|
||||
|
||||
data_size = (ALuint)(num_samples * sizeof(ALfloat));
|
||||
data = calloc(1, data_size);
|
||||
switch(type)
|
||||
{
|
||||
case WT_Sine:
|
||||
ApplySin(data, 1.0, srate, freq);
|
||||
ApplySin(data, num_samples, 1.0, srate, freq);
|
||||
break;
|
||||
case WT_Square:
|
||||
for(i = 1;freq*i < srate/2;i+=2)
|
||||
ApplySin(data, 4.0/M_PI * 1.0/i, srate, freq*i);
|
||||
ApplySin(data, num_samples, 4.0/M_PI * 1.0/i, srate, freq*i);
|
||||
break;
|
||||
case WT_Sawtooth:
|
||||
for(i = 1;freq*i < srate/2;i++)
|
||||
ApplySin(data, 2.0/M_PI * ((i&1)*2 - 1.0) / i, srate, freq*i);
|
||||
ApplySin(data, num_samples, 2.0/M_PI * ((i&1)*2 - 1.0) / i, srate, freq*i);
|
||||
break;
|
||||
case WT_Triangle:
|
||||
for(i = 1;freq*i < srate/2;i+=2)
|
||||
ApplySin(data, 8.0/(M_PI*M_PI) * (1.0 - (i&2)) / (i*i), srate, freq*i);
|
||||
ApplySin(data, num_samples, 8.0/(M_PI*M_PI) * (1.0 - (i&2)) / (i*i), srate, freq*i);
|
||||
break;
|
||||
case WT_Impulse:
|
||||
/* NOTE: Impulse isn't handled using additive synthesis, and is
|
||||
* instead just a non-0 sample at a given rate. This can still be
|
||||
* useful to test (other than resampling, the ALSOFT_DEFAULT_REVERB
|
||||
* environment variable can prove useful here to test the reverb
|
||||
* response).
|
||||
* instead just a non-0 sample. This can be useful to test (other
|
||||
* than resampling, the ALSOFT_DEFAULT_REVERB environment variable
|
||||
* can test the reverb response).
|
||||
*/
|
||||
for(i = 0;i < srate;i++)
|
||||
data[i] = (i%(srate/freq)) ? 0.0f : 1.0f;
|
||||
data[0] = 1.0f;
|
||||
break;
|
||||
case WT_WhiteNoise:
|
||||
/* NOTE: WhiteNoise is just uniform set of uncorrelated values, and
|
||||
* is not influenced by the waveform frequency.
|
||||
*/
|
||||
for(i = 0;i < srate;i++)
|
||||
for(i = 0;i < num_samples;i++)
|
||||
{
|
||||
ALuint rng0 = dither_rng(&seed);
|
||||
ALuint rng1 = dither_rng(&seed);
|
||||
@@ -146,7 +155,7 @@ static ALuint CreateWave(enum WaveType type, ALuint freq, ALuint srate, ALfloat
|
||||
|
||||
if(gain != 1.0f)
|
||||
{
|
||||
for(i = 0;i < srate;i++)
|
||||
for(i = 0;i < num_samples;i++)
|
||||
data[i] *= gain;
|
||||
}
|
||||
|
||||
@@ -156,7 +165,7 @@ static ALuint CreateWave(enum WaveType type, ALuint freq, ALuint srate, ALfloat
|
||||
alBufferData(buffer, AL_FORMAT_MONO_FLOAT32, data, (ALsizei)data_size, (ALsizei)srate);
|
||||
free(data);
|
||||
|
||||
/* Check if an error occured, and clean up if so. */
|
||||
/* Check if an error occurred, and clean up if so. */
|
||||
err = alGetError();
|
||||
if(err != AL_NO_ERROR)
|
||||
{
|
||||
@@ -175,8 +184,8 @@ int main(int argc, char *argv[])
|
||||
enum WaveType wavetype = WT_Sine;
|
||||
const char *appname = argv[0];
|
||||
ALuint source, buffer;
|
||||
ALint last_pos, num_loops;
|
||||
ALint max_loops = 4;
|
||||
ALint last_pos;
|
||||
ALint seconds = 4;
|
||||
ALint srate = -1;
|
||||
ALint tone_freq = 1000;
|
||||
ALCint dev_rate;
|
||||
@@ -217,10 +226,13 @@ int main(int argc, char *argv[])
|
||||
CloseAL();
|
||||
return 1;
|
||||
}
|
||||
else if(i+1 < argc && strcmp(argv[i], "-t") == 0)
|
||||
|
||||
if(i+1 < argc && strcmp(argv[i], "-t") == 0)
|
||||
{
|
||||
i++;
|
||||
max_loops = atoi(argv[i]) - 1;
|
||||
seconds = atoi(argv[i]);
|
||||
if(seconds <= 0)
|
||||
seconds = 4;
|
||||
}
|
||||
else if(i+1 < argc && (strcmp(argv[i], "--waveform") == 0 || strcmp(argv[i], "-w") == 0))
|
||||
{
|
||||
@@ -281,7 +293,7 @@ int main(int argc, char *argv[])
|
||||
srate = dev_rate;
|
||||
|
||||
/* Load the sound into a buffer. */
|
||||
buffer = CreateWave(wavetype, (ALuint)tone_freq, (ALuint)srate, gain);
|
||||
buffer = CreateWave(wavetype, (ALuint)seconds, (ALuint)tone_freq, (ALuint)srate, gain);
|
||||
if(!buffer)
|
||||
{
|
||||
CloseAL();
|
||||
@@ -289,7 +301,7 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
printf("Playing %dhz %s-wave tone with %dhz sample rate and %dhz output, for %d second%s...\n",
|
||||
tone_freq, GetWaveTypeName(wavetype), srate, dev_rate, max_loops+1, max_loops?"s":"");
|
||||
tone_freq, GetWaveTypeName(wavetype), srate, dev_rate, seconds, (seconds!=1)?"s":"");
|
||||
fflush(stdout);
|
||||
|
||||
/* Create the source to play the sound with. */
|
||||
@@ -299,21 +311,18 @@ int main(int argc, char *argv[])
|
||||
assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
|
||||
|
||||
/* Play the sound for a while. */
|
||||
num_loops = 0;
|
||||
last_pos = 0;
|
||||
alSourcei(source, AL_LOOPING, (max_loops > 0) ? AL_TRUE : AL_FALSE);
|
||||
last_pos = -1;
|
||||
alSourcePlay(source);
|
||||
do {
|
||||
ALint pos;
|
||||
al_nssleep(10000000);
|
||||
alGetSourcei(source, AL_SAMPLE_OFFSET, &pos);
|
||||
alGetSourcei(source, AL_SOURCE_STATE, &state);
|
||||
if(pos < last_pos && state == AL_PLAYING)
|
||||
alGetSourcei(source, AL_SAMPLE_OFFSET, &pos);
|
||||
pos /= srate;
|
||||
|
||||
if(pos > last_pos)
|
||||
{
|
||||
++num_loops;
|
||||
if(num_loops >= max_loops)
|
||||
alSourcei(source, AL_LOOPING, AL_FALSE);
|
||||
printf("%d...\n", max_loops - num_loops + 1);
|
||||
printf("%d...\n", seconds - pos);
|
||||
fflush(stdout);
|
||||
}
|
||||
last_pos = pos;
|
||||
|
||||
@@ -111,15 +111,50 @@ const char *FormatName(ALenum format)
|
||||
case AL_FORMAT_MONO8: return "Mono, U8";
|
||||
case AL_FORMAT_MONO16: return "Mono, S16";
|
||||
case AL_FORMAT_MONO_FLOAT32: return "Mono, Float32";
|
||||
case AL_FORMAT_MONO_MULAW: return "Mono, muLaw";
|
||||
case AL_FORMAT_MONO_ALAW_EXT: return "Mono, aLaw";
|
||||
case AL_FORMAT_MONO_IMA4: return "Mono, IMA4 ADPCM";
|
||||
case AL_FORMAT_MONO_MSADPCM_SOFT: return "Mono, MS ADPCM";
|
||||
case AL_FORMAT_STEREO8: return "Stereo, U8";
|
||||
case AL_FORMAT_STEREO16: return "Stereo, S16";
|
||||
case AL_FORMAT_STEREO_FLOAT32: return "Stereo, Float32";
|
||||
case AL_FORMAT_STEREO_MULAW: return "Stereo, muLaw";
|
||||
case AL_FORMAT_STEREO_ALAW_EXT: return "Stereo, aLaw";
|
||||
case AL_FORMAT_STEREO_IMA4: return "Stereo, IMA4 ADPCM";
|
||||
case AL_FORMAT_STEREO_MSADPCM_SOFT: return "Stereo, MS ADPCM";
|
||||
case AL_FORMAT_QUAD8: return "Quadraphonic, U8";
|
||||
case AL_FORMAT_QUAD16: return "Quadraphonic, S16";
|
||||
case AL_FORMAT_QUAD32: return "Quadraphonic, Float32";
|
||||
case AL_FORMAT_QUAD_MULAW: return "Quadraphonic, muLaw";
|
||||
case AL_FORMAT_51CHN8: return "5.1 Surround, U8";
|
||||
case AL_FORMAT_51CHN16: return "5.1 Surround, S16";
|
||||
case AL_FORMAT_51CHN32: return "5.1 Surround, Float32";
|
||||
case AL_FORMAT_51CHN_MULAW: return "5.1 Surround, muLaw";
|
||||
case AL_FORMAT_61CHN8: return "6.1 Surround, U8";
|
||||
case AL_FORMAT_61CHN16: return "6.1 Surround, S16";
|
||||
case AL_FORMAT_61CHN32: return "6.1 Surround, Float32";
|
||||
case AL_FORMAT_61CHN_MULAW: return "6.1 Surround, muLaw";
|
||||
case AL_FORMAT_71CHN8: return "7.1 Surround, U8";
|
||||
case AL_FORMAT_71CHN16: return "7.1 Surround, S16";
|
||||
case AL_FORMAT_71CHN32: return "7.1 Surround, Float32";
|
||||
case AL_FORMAT_71CHN_MULAW: return "7.1 Surround, muLaw";
|
||||
case AL_FORMAT_BFORMAT2D_8: return "B-Format 2D, U8";
|
||||
case AL_FORMAT_BFORMAT2D_16: return "B-Format 2D, S16";
|
||||
case AL_FORMAT_BFORMAT2D_FLOAT32: return "B-Format 2D, Float32";
|
||||
case AL_FORMAT_BFORMAT2D_MULAW: return "B-Format 2D, muLaw";
|
||||
case AL_FORMAT_BFORMAT3D_8: return "B-Format 3D, U8";
|
||||
case AL_FORMAT_BFORMAT3D_16: return "B-Format 3D, S16";
|
||||
case AL_FORMAT_BFORMAT3D_FLOAT32: return "B-Format 3D, Float32";
|
||||
case AL_FORMAT_BFORMAT3D_MULAW: return "B-Format 3D, muLaw";
|
||||
case AL_FORMAT_UHJ2CHN8_SOFT: return "UHJ 2-channel, U8";
|
||||
case AL_FORMAT_UHJ2CHN16_SOFT: return "UHJ 2-channel, S16";
|
||||
case AL_FORMAT_UHJ2CHN_FLOAT32_SOFT: return "UHJ 2-channel, Float32";
|
||||
case AL_FORMAT_UHJ3CHN8_SOFT: return "UHJ 3-channel, U8";
|
||||
case AL_FORMAT_UHJ3CHN16_SOFT: return "UHJ 3-channel, S16";
|
||||
case AL_FORMAT_UHJ3CHN_FLOAT32_SOFT: return "UHJ 3-channel, Float32";
|
||||
case AL_FORMAT_UHJ4CHN8_SOFT: return "UHJ 4-channel, U8";
|
||||
case AL_FORMAT_UHJ4CHN16_SOFT: return "UHJ 4-channel, S16";
|
||||
case AL_FORMAT_UHJ4CHN_FLOAT32_SOFT: return "UHJ 4-channel, Float32";
|
||||
}
|
||||
return "Unknown Format";
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
/* Some helper functions to get the name from the format enums. */
|
||||
const char *FormatName(ALenum type);
|
||||
const char *FormatName(ALenum format);
|
||||
|
||||
/* Easy device init/deinit functions. InitAL returns 0 on success. */
|
||||
int InitAL(char ***argv, int *argc);
|
||||
|
||||
Reference in New Issue
Block a user