Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a gain argument to altonegen #502

Merged
merged 2 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion alc/backends/opensl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ int OpenSLPlayback::mixerProc()
}
if(SL_RESULT_SUCCESS != result)
{
mDevice->handleDisconnect("Failed to start platback: 0x%08x", result);
mDevice->handleDisconnect("Failed to start playback: 0x%08x", result);
break;
}

Expand Down
28 changes: 24 additions & 4 deletions examples/altonegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@

#include "common/alhelpers.h"

#include "win_main_utf8.h"
#ifdef _WIN32
# include "win_main_utf8.h"
#endif
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this was put behind an ifdef, when the header already checks for Windows?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a mistake compiling the examples, leaving out an include directory path. But for me I prefer having the logic that include or excludes platform specific files where I can see it rather than going to the header to check. So my steps went something like, “compilation on Mac failed, why? Saw the Windows specific include, ifdef’ed it out, compilation succeeded. Link failed with code in alhelpers.c missing. Found alhelpers, added the directory, compiled and linked altonegen. Didn’t go back to undo ifdefs cuz the code feels more readable that way.

I shall revert if if you prefer.


#ifndef M_PI
#define M_PI (3.14159265358979323846)
Expand Down Expand Up @@ -93,7 +95,7 @@ static void ApplySin(ALfloat *data, ALdouble g, ALuint srate, ALuint freq)
/* 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)
static ALuint CreateWave(enum WaveType type, ALuint freq, ALuint srate, ALfloat gain)
{
ALuint seed = 22222;
ALuint data_size;
Expand Down Expand Up @@ -144,6 +146,10 @@ static ALuint CreateWave(enum WaveType type, ALuint freq, ALuint srate)
break;
}

if (gain != 1.0)
for(i = 0;i < srate;i++)
data[i] *= gain;

/* Buffer the audio data into a new buffer object. */
buffer = 0;
alGenBuffers(1, &buffer);
Expand Down Expand Up @@ -175,6 +181,7 @@ int main(int argc, char *argv[])
ALint tone_freq = 1000;
ALCint dev_rate;
ALenum state;
ALfloat gain = 1.0f;
int i;

argv++; argc--;
Expand All @@ -190,7 +197,9 @@ int main(int argc, char *argv[])

for(i = 0;i < argc;i++)
{
if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
if (strcmp(argv[i], "-h") == 0
|| strcmp(argv[i], "-?") == 0
|| strcmp(argv[i], "--help") == 0)
{
fprintf(stderr, "OpenAL Tone Generator\n"
"\n"
Expand All @@ -202,6 +211,7 @@ int main(int argc, char *argv[])
" --waveform/-w <type> Waveform type: sine (default), square, sawtooth,\n"
" triangle, impulse, noise\n"
" --freq/-f <hz> Tone frequency (default 1000 hz)\n"
" --gain/-g <gain> gain 0.0 to 1 (default 1)\n"
" --srate/-s <sample rate> Sampling rate (default output rate)\n",
appname
);
Expand Down Expand Up @@ -241,6 +251,16 @@ int main(int argc, char *argv[])
tone_freq = 1;
}
}
else if(i+1 < argc && (strcmp(argv[i], "--gain") == 0 || strcmp(argv[i], "-g") == 0))
{
i++;
gain = atof(argv[i]);
if (gain < 0.0f || gain > 1.0f)
{
fprintf(stderr, "Invalid gain: %s (min: 0.0, max 1.0)\n", argv[i]);
gain = 1.0f;
}
}
else if(i+1 < argc && (strcmp(argv[i], "--srate") == 0 || strcmp(argv[i], "-s") == 0))
{
i++;
Expand All @@ -262,7 +282,7 @@ int main(int argc, char *argv[])
srate = dev_rate;

/* Load the sound into a buffer. */
buffer = CreateWave(wavetype, (ALuint)tone_freq, (ALuint)srate);
buffer = CreateWave(wavetype, (ALuint)tone_freq, (ALuint)srate, gain);
if(!buffer)
{
CloseAL();
Expand Down