Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows pipes in binary mode

I wrote a program in windows that will play binary audio sent to it over stdin, I called it aplay(like the linux program). I then wrote a seperate program as follows

FILE * f = popen("aplay.exe", "wb");
FILE * song = fopen("C:/Users/Ray/Dropbox/DJ Automica 2/Debug/raymonster 5.wav", "rb");
while(1)
{
    byte buff[4096];
    fread(buff, 4, 1024, song);
    fwrite(buff, 4, 1024, f);
}

For some reason, the pipe doesn't seem to be working in binary mode, because the audio is coming out all messed up. If I change my aplay to open the wave file by itself in text mode it sounds the same as when I do it through the pipe, if I open the wave file in binary mode it plays perfectly. Does anyone know how I can fix this?

like image 439
Ramónster Avatar asked Oct 09 '22 15:10

Ramónster


1 Answers

If you include the header files

#include <fcntl.h>
#include <io.h>

you can switch modes with

_setmode(_fileno(stdin), _O_BINARY);
like image 87
Dennis Avatar answered Oct 13 '22 11:10

Dennis