#include #include #include #include #include #define BUFFER_SIZE 1024 #define SAMPLE_RATE 11025 #define CHANNELS 1 #define FORMAT SND_PCM_FORMAT_U8 int main() { snd_pcm_t *pcm_handle; int pipe_fd; char *fifo = "/dev/dsp"; short buffer[BUFFER_SIZE]; // Create named pipe (FIFO) mkfifo(fifo, 0666); // Open the ALSA PCM device if (snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0) { fprintf(stderr, "Error opening PCM device\n"); return 1; } // Set PCM parameters snd_pcm_set_params(pcm_handle, FORMAT, SND_PCM_ACCESS_RW_INTERLEAVED, CHANNELS, SAMPLE_RATE, 1, 500000); // Open the named pipe for reading pipe_fd = open(fifo, O_RDONLY); // Read from the pipe and play back the audio while (1) { ssize_t bytesRead = read(pipe_fd, buffer, sizeof(buffer)); if (bytesRead == -1) { perror("Error reading from pipe"); break; } // Write the audio data to the ALSA PCM device if (snd_pcm_writei(pcm_handle, buffer, bytesRead / sizeof(short)) < 0) { snd_pcm_prepare(pcm_handle); fprintf(stderr, "Error playing audio\n"); } } // Close the PCM device and named pipe snd_pcm_close(pcm_handle); close(pipe_fd); // Remove the named pipe unlink(fifo); return 0; }