/* * tone.c - play a sine wave tone on AIX 4.3.3 via /dev/paud0 * Claude Opus 4.5, 2026-10-13 * * Build: xlc -o tone tone.c -lm * or: gcc -o tone tone.c -lm */ /* audio.h redefines BIG_ENDIAN, conflicting with machine.h (included via param.h). * Work around: include param.h first, undef BIG_ENDIAN, then include audio.h. */ #include #undef BIG_ENDIAN #include #include #include #include #include #include #include #include #define SAMPLE_RATE 22050 #define BITS 16 #define CHANNELS 1 #define FREQ_HZ 440 #define DURATION_MS 1000 int main(int argc, char **argv) { int fd; audio_init init; int freq = FREQ_HZ; int duration = DURATION_MS; int nsamples, nbytes, i; short *buf; double phase, delta; ssize_t written; if (argc > 1) freq = atoi(argv[1]); if (argc > 2) duration = atoi(argv[2]); if (freq < 20 || freq > 20000) { fprintf(stderr, "frequency %d out of range (20-20000)\n", freq); return 1; } fd = open("/dev/paud0/1", O_WRONLY); if (fd < 0) { perror("open /dev/paud0"); return 1; } /* configure the device */ memset(&init, 0, sizeof(init)); init.srate = SAMPLE_RATE; init.bits_per_sample = BITS; init.mode = PCM; init.channels = CHANNELS; init.flags = SIGNED | TWOS_COMPLEMENT | BIG_ENDIAN | FIXED; init.operation = PLAY; if (ioctl(fd, AUDIO_INIT, &init) < 0) { perror("ioctl AUDIO_INIT"); close(fd); return 1; } if (init.rc != 0) { fprintf(stderr, "AUDIO_INIT rc=%d\n", init.rc); close(fd); return 1; } printf("AUDIO_INIT ok: srate=%ld bits=%ld bsize=%ld flags=0x%lx\n", init.srate, init.bits_per_sample, init.bsize, init.flags); /* generate samples */ nsamples = (SAMPLE_RATE * duration) / 1000; nbytes = nsamples * sizeof(short); buf = malloc(nbytes); if (!buf) { perror("malloc"); close(fd); return 1; } delta = 2.0 * M_PI * freq / SAMPLE_RATE; phase = 0.0; for (i = 0; i < nsamples; i++) { /* 16-bit signed, amplitude ~0.8 to avoid clipping */ buf[i] = (short)(sin(phase) * 26000); phase += delta; } printf("playing %d Hz for %d ms (%d samples, %d bytes)\n", freq, duration, nsamples, nbytes); /* write samples */ written = write(fd, buf, nbytes); if (written < 0) { perror("write"); } else if (written != nbytes) { fprintf(stderr, "short write: %d of %d\n", (int)written, nbytes); } /* wait for playback to complete */ if (ioctl(fd, AUDIO_WAIT, NULL) < 0) { perror("ioctl AUDIO_WAIT"); } free(buf); close(fd); printf("done\n"); return 0; }