aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTuomas Siipola2024-08-14 21:48:57 +0300
committerTuomas Siipola2024-08-14 21:48:57 +0300
commit77475a139496483987e784e2267587e057e67a1e (patch)
treecf2602acbdd849dedff7c92c89fc46a1ef3da85d
parent2c71776c5b41581b6607c5812460534db2eeef4a (diff)
Add volume optionHEADmaster
-rw-r--r--gtk.c2
-rw-r--r--main.c11
-rw-r--r--metronome.c3
-rw-r--r--metronome.h2
4 files changed, 13 insertions, 5 deletions
diff --git a/gtk.c b/gtk.c
index ef10c48..d2fe31c 100644
--- a/gtk.c
+++ b/gtk.c
@@ -37,7 +37,7 @@ gpointer alsa_thread(gpointer data)
if (running) {
uint16_t *buffer = NULL;
size_t size;
- metronome_generate(&buffer, &size, 48000, bpm, 4, 4, SOUND_SINE);
+ metronome_generate(&buffer, &size, 48000, bpm, 4, 4, SOUND_SINE, 1.0);
alsa_play(buffer, size);
free(buffer);
}
diff --git a/main.c b/main.c
index fc5b79d..54ed1ae 100644
--- a/main.c
+++ b/main.c
@@ -7,6 +7,7 @@
#include <stdbool.h>
#include <string.h>
#include <getopt.h>
+#include <math.h>
#include "metronome.h"
#include "alsa.h"
@@ -23,6 +24,7 @@ static void usage()
printf("Options:\n");
printf(" -o, --output=FILE export WAV file\n");
printf(" -s, --sound=SOUND play sine, square, saw or triangle\n");
+ printf(" -v, --volume set output volume (0.0 - 1.0)\n");
printf(" -h, --help display this help and exit\n");
printf(" -V, --version display version information and exit\n");
}
@@ -33,17 +35,19 @@ int main(int argc, char *argv[])
char *output = NULL;
Sound sound = SOUND_SINE;
+ float volume = 1.0;
static struct option long_options[] = {
{ "output", required_argument, NULL, 'o' },
{ "sound", required_argument, NULL, 's' },
+ { "volume", required_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 },
};
int c;
- while ((c = getopt_long(argc, argv, "o:s:hV", long_options, NULL)) != -1) {
+ while ((c = getopt_long(argc, argv, "o:s:v:hV", long_options, NULL)) != -1) {
switch (c) {
case 'o':
output = optarg;
@@ -62,6 +66,9 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
break;
+ case 'v':
+ volume = powf(atof(optarg), 4);
+ break;
case 'h':
usage();
return EXIT_SUCCESS;
@@ -105,7 +112,7 @@ int main(int argc, char *argv[])
uint16_t *buffer = NULL;
size_t size;
- metronome_generate(&buffer, &size, sample_rate, tempo, numerator, denominator, sound);
+ metronome_generate(&buffer, &size, sample_rate, tempo, numerator, denominator, sound, volume);
if (output) {
wav_write(output, sample_rate, buffer, size, 4);
diff --git a/metronome.c b/metronome.c
index 3abd499..4973a00 100644
--- a/metronome.c
+++ b/metronome.c
@@ -17,7 +17,7 @@ static double sgn(double x)
return 0;
}
-void metronome_generate(uint16_t **buffer, size_t *size, unsigned int sample_rate, long tempo, int numerator, int denominator, Sound sound)
+void metronome_generate(uint16_t **buffer, size_t *size, unsigned int sample_rate, long tempo, int numerator, int denominator, Sound sound, float volume)
{
size_t beat = 4 * 60 * sample_rate / tempo / denominator;
*size = beat * numerator;
@@ -48,6 +48,7 @@ void metronome_generate(uint16_t **buffer, size_t *size, unsigned int sample_rat
sample = 4 * fabs(fmod(i * freq / sample_rate - 0.25, 1) - 0.5) - 1;
break;
}
+ sample *= volume;
// Fade sound in and out to reduce audio pops and clicks.
if (t < 0.1) {
diff --git a/metronome.h b/metronome.h
index 1aa445d..8b2e478 100644
--- a/metronome.h
+++ b/metronome.h
@@ -14,6 +14,6 @@ typedef enum {
SOUND_TRIANGLE,
} Sound;
-extern void metronome_generate(uint16_t **buffer, size_t *size, unsigned int sample_rate, long tempo, int numerator, int denominator, Sound sound);
+extern void metronome_generate(uint16_t **buffer, size_t *size, unsigned int sample_rate, long tempo, int numerator, int denominator, Sound sound, float volume);
#endif // METRONOME_H