Redirecting stdout

Hey,

I've recently been inspired by some of Joel Yliluoma's(Bisqwit on youtube) projects.
More specifically, this one:
http://www.youtube.com/watch?v=L9KLnN0GczI

In which he "redirects stdout" to the audio driver (as I understand it) and effectively streams PCM audio data to his speakers. He addresses how he does this in the video's description, but it's a little vague for an inexperienced, non-linux user such as myself.
I had a look and freopen (http://www.cplusplus.com/reference/cstdio/freopen/) strikes me as promising. I guess my question is does anyone have experience doing this kind of stuff (streaming PCM audio data, without the help of some extensive API)? Any pointers to set me in the right direction are greatly appreciated.
(Confession: I haven't watched the video.)

On *nix, the standard streams can be treated as binary objects.
On Windows, they cannot, without first making some special adjustments.

Here's something to help:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifdef _WIN32

  #ifdef __cplusplus
    #include <cstdio>
  #else
    #include <stdio.h>
  #endif
  #include <fcntl.h>

  void std_binary_io()
    {
    #ifdef __cplusplus
    using namespace std;
    #endif

    _setmode( _fileno( stdin  ), _O_BINARY );
    _setmode( _fileno( stdout ), _O_BINARY );
    _setmode( _fileno( stderr ), _O_BINARY );

    #ifdef __cplusplus
    cin.sync_with_stdio();
    #endif
    }

#else

  void std_binary_io() { }

#endif  
1
2
3
4
5
6
int main()
  {
  std_binary_io();
  // now you can read/write binary data from cin/cout/cerr
  ...
  }

Hope this helps.
Have you thought about using CYGWIN?

I'd not tried it before, but CYGWIN supports /dev/dsp ; I've just tried

$ cat tada.wav > /dev/dsp


and heard the ta-da sound!

Andy
Last edited on
Thanks Duoas! You've set me in the right direction.

@andywestken after looking, Bisqwit actually has an example using cygwin for this kind of thing. I feel a little silly now :).
I guess that's the way I'm gonna go, thanks again for the feedback!
Topic archived. No new replies allowed.