/* The MIT License (MIT) Copyright (c) 2026 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 -O2 -march=native -mtune=native -flto -o ./ringtone ./ringtone.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 = "" "ringtone [options...] [duration...] [volume...]\n" "\n" "\n" "Emit a ringtone as a wave-sound, lasting the number of seconds given, or 1\n" "second by default. The audio output format is RIFF WAVE (wav) sampled at\n" "48khz. There's also an optional volume argument, which is 1 by default.\n" "\n" "By default the sound is played directly, but there are options to emit the\n" "binary data for the sound instead.\n" "\n" "\n" "Options, all of which can start with either 1 or 2 dashes:\n" "\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" " -out output ringtone to standard output as WAV-format data\n" " -output output ringtone to standard output as WAV-format data\n" ""; const uint64_t sample_rate = 48000; const double dt = 1.0 / sample_rate; // 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, "-out") == 0 || strcmp(s, "-output") == 0 || strcmp(s, "--o") == 0 || strcmp(s, "--out") == 0 || strcmp(s, "--output") == 0 ); } static inline void set_int16_le(unsigned char* buf, int16_t v) { buf[0] = (v >> 0); buf[1] = (v >> 8); } 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); } // 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_ringtone(double seconds, double volume) { 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); const double tau = 2 * M_PI; for (uint64_t i = 0; i < samples; i++) { const double t = (double)i * dt; const double u = fmod(t, 0.1); const double v = volume * sin(2048 * tau * t) * exp(-50 * u); 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; } // 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_ringtone(double seconds, double volume) { 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 double tau = 2 * M_PI; 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 u = fmod(t, 0.1); const double v = volume * sin(2048 * tau * t) * exp(-50 * u); 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; } 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 > 1 && is_help_option(argv[1])) { printf("%s", info); return 0; } size_t start_args = 1; bool output = false; if (argc > start_args && is_output_option(argv[start_args])) { start_args++; output = true; } 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++; } } if (argc > start_args && is_output_option(argv[start_args])) { start_args++; output = true; } 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 (argc > start_args && is_output_option(argv[start_args])) { output = true; } if (output) { // enable full/block-buffering for standard output setvbuf(stdout, NULL, _IOFBF, 0); } const double s = seconds; const double v = volume; return output ? emit_wav_ringtone(s, v) : play_ringtone(s, v); }