If you're having trouble compiling the sine wave generator C code on your computer, you can download the precompiled **macOS** executable below: [Download](https://www.sustainable-music.org/wp-content/uploads/sinegen.zip) To run it, open **Terminal** and navigate to the folder containing the executable. For example, if you downloaded it to your **Downloads** folder, run: ```bash cd ~/Downloads ``` Then execute the program: ```bash ./sinegen 440 2 ``` This example generates a **440 Hz** sine wave with a duration of **2 seconds**. The following is the source code for this program: ```C #include <stdio.h> #include <math.h> #include <stdlib.h> #define PI 3.14159265358979323846 // write 16-bit WAV header void writeWavHeader(FILE *f, int numSamples) { int sampleRate = 44100; int dataSize = numSamples * 2; // 16-bit = 2 bytes per sample int fileSize = 36 + dataSize; fwrite("RIFF", 1, 4, f); fwrite(&fileSize, 4, 1, f); fwrite("WAVE", 1, 4, f); fwrite("fmt ", 1, 4, f); int fmtSize = 16; short audioFormat = 1; short numChannels = 1; int byteRate = sampleRate * 2; short blockAlign = 2; short bitsPerSample = 16; fwrite(&fmtSize, 4, 1, f); fwrite(&audioFormat, 2, 1, f); fwrite(&numChannels, 2, 1, f); fwrite(&sampleRate, 4, 1, f); fwrite(&byteRate, 4, 1, f); fwrite(&blockAlign, 2, 1, f); fwrite(&bitsPerSample, 2, 1, f); fwrite("data", 1, 4, f); fwrite(&dataSize, 4, 1, f); } int main(int argc, char * argv[]) { if (argc != 3) { printf("Usage: %s <frequency> <duration>\n", argv[0]); return 1; } char *end; double frequency = strtod(argv[1], &end); double durationSeconds = strtod(argv[2], &end); if (end == argv[1]) { // No number was found at all printf("frequency should a number.\n"); return 1; } if (end == argv[2]) { // No number was found at all printf("duration should a number.\n"); return 1; } int sampleRate = 44100; int numSamples = sampleRate * durationSeconds; char filename[100]; snprintf(filename, sizeof(filename), "sine_%.2f_%.2f.wav", frequency, durationSeconds); FILE *f = fopen(filename, "wb"); writeWavHeader(f, numSamples); for (int i = 0; i < numSamples; i++) { double t = (double)i / sampleRate; double sample = sin(2.0 * PI * frequency * t); // convert [-1,1] → 16-bit PCM short intSample = (short)(sample * 32767); fwrite(&intSample, sizeof(short), 1, f); } fclose(f); return 0; } ```