/* 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 cc -Wall -s -O2 -o ./ringtone ./ringtone.c -lm */ #include #include #include #include #include #include #ifdef _WIN32 #include #include #endif const char* info = "" "ringtone [options...] [duration...]\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.\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" ""; 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 ); } void write_int16_le(FILE* w, int16_t v) { putc((v >> 0) & 0b11111111, w); putc((v >> 8) & 0b11111111, w); } void write_uint16_le(FILE* w, uint16_t v) { putc((v >> 0) & 0b11111111, w); putc((v >> 8) & 0b11111111, w); } void write_uint32_le(FILE* w, uint32_t v) { putc((v >> 0) & 0b11111111, w); putc((v >> 8) & 0b11111111, w); putc((v >> 16) & 0b11111111, w); putc((v >> 24) & 0b11111111, 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, uint32_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; const uint32_t data_size = byte_rate * samples; const uint32_t total_size = data_size + 44; 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 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 // handle any of the help options, if given if (argc > 1 && is_help_option(argv[1])) { puts(info); return 0; } double seconds = 1.0; if (argc > 1) { char* end; const double n = strtod(argv[1], &end); if (*end == 0) { seconds = n >= 0 ? n : 1.0; } } if (seconds < 0) { seconds = 0; } const uint64_t samples = (uint64_t)(seconds * sample_rate); if (samples >= 0xffffffff) { const char* m = "the duration given exceeds the WAV-format maximum"; fprintf(stderr, "\x1b[31m%s\x1b[0m\n", m); return 1; } // enable full/block-buffering for standard output setvbuf(stdout, NULL, _IOFBF, 0); 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 v = sin(2024 * tau * t) * exp(-50 * fmod(t, 0.1)); write_int16_le(stdout, (int16_t)(32767 * v)); // check if the standard output was closed only occasionally if ((i % (16 * 1024) == 0) && feof(stdout)) { return 0; } } return 0; }