/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* You can build this command-line app by running sudo apt install libpulse-dev cc -s -O3 -march=native -mtune=native -flto -o ./sboard ./sboard.c \ -lm -lpulse -lpulse-simple */ #include #include #include #include #include #include #include #ifdef _WIN32 #include #include #endif #include #ifdef RED_ERRORS #define ERROR_STYLE "\x1b[38;2;204;0;0m" #ifdef __APPLE__ #define ERROR_STYLE "\x1b[31m" #endif #define RESET_STYLE "\x1b[0m" #else #define ERROR_STYLE #define RESET_STYLE #endif #define ERROR_LINE(MSG) (ERROR_STYLE MSG RESET_STYLE "\n") const char* info = "" "sboard [options...] [sound...] [duration...] [volume...]\n" "\n" "\n" "Sound BOARD plays the sound given by name, lasting the number of seconds\n" "given, or 1 second by default. The audio output is 16-bit samples at 48khz.\n" "There's also an optional volume argument, which is 1 by default.\n" "\n" "Options, all of which can start with either 1 or 2 dashes:\n" "\n" " -h show this help message\n" " -help show this help message\n" "\n" " -o output ringtone to standard output as WAV-format data\n" ""; const double tau = 2 * M_PI; const uint64_t sample_rate = 48000; const double dt = 1.0 / sample_rate; double beeper(double t) { return sin(2000 * tau * t) * (fmod(t, 0.5) < 0.0625); } double bell(double t) { const double u = fmod(t, 1); return sin(880 * tau * u) * exp(-10 * u); } // busy-phone tone double busy(double t) { const double u = fmod(t, 1); const double k = fmin(1, exp(-90 * (u - 0.5))); return k * (sin(480 * tau * t) + sin(620 * tau * t)) / 2; } double door_ajar(double t) { const double u = fmod(t, 1); return sin(660 * tau * u) * exp(-10 * u); } double heartbeat(double t) { double sum = 0.0; double u = fmod(t, 1); sum += sin(12 * tau * exp(-20 * u)) * exp(-2 * u); u = fmax(0, u - 0.25); sum += sin(8 * tau * exp(-20 * u)) * exp(-2 * u); return 0.5 * sum; } // a once-a-seconds stereotypical laser sound double laser(double t) { const double u = fmod(t, 1); return sin(100 * tau * exp(-40 * u)); } // flat/uniform random noise double noise(double t) { const double v = (double)rand() / RAND_MAX; return 2 * v - 1; } // ready-phone tone double ready(double t) { return 0.5 * sin(350 * tau * t) + 0.5 * sin(450 * tau * t); } // annoying ringtone double ringtone(double t) { const double u = fmod(t, 0.1); return sin(2048 * tau * t) * exp(-50 * u); } double thud(double t) { double u = fmod(t, 1); return sin(12 * tau * exp(-20 * u)) * exp(-2 * u); } // 440hz tuning tone double tone(double t) { return sin(440 * tau * t); } double woo_woah_wow(double t) { // const double period = 1.3; const double period = 1.25; const double u = fmod(t, period); return sin(tau * (260 * sin(tau * u)) * u); } // functions used by fancier tunes double kick(double t, double f, double k, double p) { return sin(tau * f * pow(p, t)) * exp(-k * t); } double default_kick(double t, double f, double k) { return kick(t, f, k, 0.085); } // flat/uniform random noise double random_uniform() { const double v = (double)rand() / RAND_MAX; return 2 * v - 1; } double hit_hi_hat(double t, double k) { return random_uniform() * exp(-k * t); } double schedule(double t, double delay, double period) { return fmod(t + (1 - delay) * period, period); } double arp(double x, double y, double z, double k, double t) { const double u = fmod(t / 2, k); return sin(x * (exp(-y * u))) * exp(-z * u); } double linterp(double x, double y, double k) { return k * x + (1 - k) * y; } double power_synth(double t, double freq) { // function linspace(a, b, n) { // const y = new Array(n); // const incr = (b - a) / (n + 1); // for (let i = 0; i < n; i++) // y[i] = a + incr * i; // return y; // } // bass_powers = linspace(1e-5, 1, 10).map(x => -0.05 * Math.log(x)); const double powers[] = { 0.5756462732485115, 0.11988976388990187, 0.08523515466254475, 0.06496281589095718, 0.05057917059158016, 0.03942226802181348, 0.030306373513585217, 0.022598970473683477, 0.015922499056278294, 0.010033423662119907, }; const size_t n = sizeof(powers) / sizeof(powers[0]); double res = 0.0; for (size_t i = 0; i < n; i++) { res += powers[i] * cos(tau * (i + 1) * freq * t); } return res; } double bass_envelope(double t) { const double u = fmod(t, 1); return (u < 0.05) ? 15 * u : exp(-7 * u); } // fancier tunes double bust_a_move(double t) { const double period = 4.09; const double freqs[] = { 50, 75, 50, 50, 75, 50, 50, 50, 75, 50, 50, 50, 50, 50, 75, 50 }; // const delays = [ // 0, 0.52, 0.77, 1.28, 1.54, 1.79, // 2.04, 2.3, 2.56, 2.67, // 3.05, 3.07, 3.2, 3.45, 3.57, 3.82 // ].map(x => x / period); const double delays[] = { 0.0000000000000000, 0.1271393643031785, 0.1882640586797066, 0.31295843520782396, 0.3765281173594132, 0.43765281173594134, 0.49877750611246946, 0.5623471882640586, 0.6259168704156479, 0.6528117359413202, 0.745721271393643, 0.7506112469437652, 0.78239608801956, 0.8435207823960881, 0.8728606356968215, 0.9339853300733496, }; const double u = fmod(t, period); const size_t half = sizeof(freqs) / sizeof(freqs[0]) / 2; const size_t start = u < 2.5 ? 0 : half; double kicks = 0.0; for (size_t i = 0; i < half; i++) { const double d = delays[start + i]; const double f = freqs[start + i]; kicks += default_kick(schedule(t, d, period), f, 50); } double hi_hats = 0.0; for (size_t i = 0; i < half; i++) { hi_hats += hit_hi_hat(schedule(t, i / half, period / half), 25); } return 0.9 * kicks + 1.0 / 32 * hi_hats; } double crazy(double t) { const double snares[] = { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; const double kicks[] = { 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, }; const double bass_speed[] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, }; double seq(const double* d, double s, double t) { return d[(size_t)floor(t / s / 2) % sizeof(kicks)]; } t *= 148.0 / 120; // const double period = 6.5; // const double u = fmod(t, period); // const double v = fmod(t, 2*period); const double rand = linterp(random(), 1, 1.0 / 2); const double anticlip = fmax(-log(.75 * fmod(8 * t, 1) + 1.0 / M_E), 0); // const double anticlip = 1; const double k = anticlip * arp(60, 40, 20, 1.0 / 16, t) * seq(kicks, 1.0 / 16, t); const double s = 0.3 * arp(60, 80, 3, 1.0 / 16, t) * rand * seq(snares, 1.0 / 16, t); const double su = seq(bass_speed, 1.0 / 16, t); const double b1 = bass_envelope(su * t) * power_synth(su * t, 50.0 / su); const double b2 = bass_envelope(su * (t + 0.5 * 6.5)) * power_synth(su * (t + 0.5 * 6.5), 60.0 / su); const double b = b1 + b2; // return s; return 0.9 * k + 0.7 * s + 0.8 * b; } double piano(double t, double n) { const double p = (n - 49) / 12; const double f = 440 * pow(2, p); return sin(tau * f * t); } double piano_loop(double t) { const double period = 1.025; const double cutoff = 12; const double p = period; double y = 0; y += piano(t, 49) * exp(-cutoff * fmod(t, period)); y += piano(t + 0.25 * p, 50) * exp(-cutoff * fmod(t + 0.25 * p, p)); y += piano(t + 0.50 * p, 54) * exp(-cutoff * fmod(t + 0.50 * p, p)); y += piano(t + 0.75 * p, 51) * exp(-cutoff * fmod(t + 0.75 * p, p)); return 0.75 * y; } double walk_this_way(double t) { const double period = 2.2; const double freqs[] = {50, 70, 50, 50, 50, 70}; const double delays[] = {0.000, 0.250, 0.450, 0.500, 0.625, 0.750}; const size_t n = sizeof(delays) / sizeof(delays[0]); double kicks = 0; for (size_t i = 0; i < n; i++) { kicks += default_kick(schedule(t, delays[i], period), freqs[i], 50); } double hi_hats = 1.2 * hit_hi_hat(schedule(t, 0, period), 7); for (size_t i = 1; i < n; i++) { hi_hats += hit_hi_hat(schedule(t, i / 8, period / 8), 30); } // return 1 * kicks + 1.0 / 24 * hi_hats; return 1 * kicks + 1.0 / 48 * hi_hats; } const char* aliases[] = { "beeper", "beeper", "beeps", "beeper", "bell", "bells", "bells", "bells", "busy", "busy", "door-ajar", "door-ajar", "doorajar", "door-ajar", "heart", "heart", "heart-beat", "heart", "heart-beats", "heart", "heartbeat", "heart", "heartbeats", "heart", "laser", "laser", "noise", "noise", "ready", "ready", "ring-tone", "ringtone", "ringtone", "ringtone", "thud", "thud", "440", "tone", "440hz", "tone", "tone", "tone", "woo-woah-wow", "woo-woah-wow", "woo", "woo-woah-wow", "woah", "woo-woah-wow", "wow", "woo-woah-wow", "bust-a-move", "bust-a-move", "bustamove", "bust-a-move", "crazy", "crazy", "piano", "piano-loop", "piano-loop", "piano-loop", "pianoloop", "piano-loop", "walk-this-way", "walk-this-way", "walkthisway", "walk-this-way", }; const char* dealias(const char* s) { const size_t n = sizeof(aliases) / sizeof(aliases[0]); for (size_t i = 0; i < n; i += 2) { if (strcmp(s, aliases[i]) == 0) { return aliases[i + 1]; } } return NULL; } typedef struct lookup_entry { const char* name; double (*func)(double); const char* desc; } lookup_entry; lookup_entry lookup[] = { {"beeper", beeper, "beep some TV-related devices used to make"}, {"bells", bell, "a synthetic bell"}, {"busy", busy, "a busy phone"}, {"door-ajar", door_ajar, "door-ajar warning sound"}, {"heart", heartbeat, "pairs of heart-pulses"}, {"laser", laser, "a stereotypical laser sound, once a second"}, {"noise", noise, "uniform random noise (annoying)"}, {"ready", ready, "a ready phone"}, {"ringtone", ringtone, "a slightly annoying ringtone"}, {"thud", thud, "a low beat, similar to those in nightclubs"}, {"tone", tone, "a 440hz tuning tone"}, {"woo-woah-wow", woo_woah_wow, "a crazy sound (loop: 1.25s)"}, {"bust-a-move", bust_a_move, "middle part of that song (loop: 4.09s)"}, {"crazy", crazy, "a repeating tune (NOT FULLY WORKING) (loop: 6.5s)"}, {"piano-loop", piano_loop, "a few repeating piano notes (loop: 1.025s)"}, {"walk-this-way", walk_this_way, "drums from that song (loop: 2.2s)"}, }; // is_help_option simplifies control-flow for func main bool is_help_option(const char* s) { return (s[0] == '-') && ( strcmp(s, "-h") == 0 || strcmp(s, "-help") == 0 || strcmp(s, "--h") == 0 || strcmp(s, "--help") == 0 ); } // is_output_option simplifies control-flow for func main bool is_output_option(const char* s) { return (s[0] == '-') && ( strcmp(s, "-o") == 0 || strcmp(s, "--o") == 0 ); } static inline void write_int16_le(FILE* w, int16_t v) { fputc((v >> 0), w); fputc((v >> 8), w); } static inline void write_uint16_le(FILE* w, uint16_t v) { fputc((v >> 0), w); fputc((v >> 8), w); } void write_uint32_le(FILE* w, uint32_t v) { fputc((v >> 0), w); fputc((v >> 8), w); fputc((v >> 16), w); fputc((v >> 24), w); } static inline void set_int16_le(unsigned char* buf, int16_t v) { buf[0] = (v >> 0); buf[1] = (v >> 8); } // is_big_endian checks the platform's endianness; kept for reference; not // used anymore since playback use little-endian samples on all platforms, // via function set_int16_le bool is_big_endian() { const uint8_t pair[2] = {255, 0}; return *((uint16_t*)pair) >= 256; } int play(double seconds, double volume, double (*emit)(double)) { if (seconds <= 0) { return 0; } if (volume < 0 || volume > 1 || isnan(seconds) || isinf(seconds)) { volume = 1; } const uint64_t rate = 48 * 1000; unsigned char buf[32 * 1024]; pa_simple* pa; pa_sample_spec spec; memset(&spec, 0, sizeof(spec)); // spec.format = is_big_endian() ? PA_SAMPLE_S16BE : PA_SAMPLE_S16LE; spec.format = PA_SAMPLE_S16LE; spec.rate = rate; spec.channels = 1; pa = pa_simple_new( NULL, "ringtone", PA_STREAM_PLAYBACK, NULL, "<>", &spec, NULL, NULL, &errno ); if (pa == NULL) { fprintf(stderr, ERROR_LINE("can't use pulse-audio for playback")); return 1; } uint64_t pos = 0; const uint64_t samples = (uint64_t)ceil(seconds * rate); for (uint64_t i = 0; i < samples; i++) { const double t = (double)i * dt; const double v = volume * emit(t); set_int16_le(&buf[pos], (int16_t)(32767 * v)); pos += 2; if (pos == sizeof(buf)) { if (pa_simple_write(pa, buf, pos, NULL) < 0) { pa_simple_drain(pa, NULL); pa_simple_free(pa); return 1; } pos = 0; } } if (pos > 0) { if (pa_simple_write(pa, buf, pos, NULL) < 0) { pa_simple_drain(pa, NULL); pa_simple_free(pa); return 1; } } pa_simple_drain(pa, NULL); pa_simple_free(pa); return 0; } // write_wav_intro starts a wave-format data-stream declaring a mono 16-bit // integer PCM sound for the number of samples given void write_wav_intro(FILE* w, uint64_t samples) { const uint16_t integer_pcm = 1; const uint32_t fmt_chunk_size = 16; const uint32_t channels = 1; const uint32_t bytes_per_sample = 2; const uint32_t bits_per_sample = 8 * bytes_per_sample; const uint32_t byte_rate = sample_rate * bytes_per_sample * channels; uint64_t data_size = byte_rate * samples; uint64_t total_size = data_size + 44; if (data_size > UINT32_MAX) { data_size = UINT32_MAX; total_size = UINT32_MAX; } fprintf(w, "RIFF"); write_uint32_le(w, total_size); fprintf(w, "WAVEfmt "); write_uint32_le(w, fmt_chunk_size); write_uint16_le(w, integer_pcm); write_uint16_le(w, channels); write_uint32_le(w, sample_rate); write_uint32_le(w, byte_rate); write_uint16_le(w, bytes_per_sample * channels); write_uint16_le(w, bits_per_sample); fprintf(w, "data"); write_uint32_le(w, data_size); } int emit_wav(double seconds, double volume, double (*emit)(double)) { if (seconds <= 0) { write_wav_intro(stdout, 0); return 0; } if (volume < 0 || volume > 1 || isnan(seconds) || isinf(seconds)) { volume = 1; } const uint64_t samples = (uint64_t)(seconds * sample_rate); if (samples >= UINT32_MAX) { fprintf(stderr, ERROR_LINE("duration given exceeds WAV-format max")); return 1; } write_wav_intro(stdout, samples); for (uint64_t i = 0; i < samples; i++) { const double t = (double)i * dt; const double v = volume * emit(t); write_int16_le(stdout, (int16_t)(32767 * v)); // check if the standard output was closed only occasionally if ((i % (32 * 1024) == 0) && feof(stdout)) { return 0; } } return 0; } void show_help(FILE* w) { fprintf(w, "%s", info); fprintf(w, "\n\nSound names available\n\n"); for (size_t i = 0; i < sizeof(aliases) / sizeof(aliases[0]); i += 2) { const char* name = aliases[i]; const char* dealiased = aliases[i + 1]; for (size_t j = 0; j < sizeof(lookup) / sizeof(lookup[0]); j++) { if (strcmp(lookup[j].name, dealiased) == 0) { fprintf(w, " %-14s %s\n", name, lookup[j].desc); break; } } } } int main(int argc, char** argv) { #ifdef _WIN32 setmode(fileno(stdin), O_BINARY); // ensure output lines end in LF instead of CRLF on windows setmode(fileno(stdout), O_BINARY); setmode(fileno(stderr), O_BINARY); #endif if (argc < 2 || is_help_option(argv[1])) { const bool no_args = argc < 2; show_help(no_args ? stderr : stdout); return no_args ? 1 : 0; } size_t start_args = 1; bool raw_output = false; if (is_output_option(argv[1])) { raw_output = true; start_args++; // enable full/block-buffering for standard output setvbuf(stdout, NULL, _IOFBF, 0); } const char* sound_name = ""; sound_name = argv[start_args]; start_args++; double seconds = 1.0; if (argc > start_args) { char* end; const double n = strtod(argv[start_args], &end); if (*end == 0 && argv[start_args] != end) { seconds = n >= 0 ? n : 1.0; start_args++; } } double volume = 1.0; if (argc > start_args) { char* end; const double n = strtod(argv[start_args], &end); if (*end == 0 && argv[start_args] != end) { volume = n >= 0 ? n : 1.0; } } if (sound_name[0] == 0) { fprintf(stderr, "not given a sound name\n"); return 1; } const char* dealiased = dealias(sound_name); if (dealiased == NULL) { fprintf(stderr, "sound named \"%s\" not available\n", sound_name); return 1; } const double s = seconds; const double v = volume; for (size_t i = 0; i < sizeof(lookup) / sizeof(lookup[0]); i++) { if (strcmp(lookup[i].name, dealiased) == 0) { srand(0); return (raw_output ? emit_wav : play)(s, v, lookup[i].func); } } fprintf(stderr, "sound named \"%s\" not available\n", sound_name); return 1; }