Working with pipes in C.

This is a discussion on Working with pipes in C. within the Linux Programming forums, part of the Platform Specific Boards category; I'm currently looking at pipes as a way to open an audio file in a program I am working on. ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    Working with pipes in C.

    I'm currently looking at pipes as a way to open an audio file in a program I am working on.

    For example:

    Code:
    FILE *bgmusic
    
    bgmusic = popen(player filename.ext | aplay -f S16_LE -r 32000 -c 2", "r");
    Calling pclose(bgmusic); will not work until I send a line feed to the pipe in order to stop the music as it runs on a continuous loop.

    Now my question is what would be the best way to send a line feed to the pipe?

  2. #2
    Registered User t3chn0n3rd's Avatar
    Join Date
    Dec 2007
    Location
    kansas city
    Posts
    25

    great code

    that looks like great code I dont see a problem

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    pclose() performs a wait4() on the process forked by popen(), however the process will not terminiate until a line feed has been sent to it, which puts me in a bit of a spot as I am rather new to pipes and not sure if you can communicate with it intermediately.

    wait4() is telling it to wait for a return value (0) before it closes the stream, but until the line feed is sent, my music program sends a continuous loop to aplay.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by RemmyLee View Post
    Now my question is what would be the best way to send a line feed to the pipe?
    There is no way to do that using popen(). The communication is one-way. In this case because you are reading, you cannot also write. You must either resort to a non-portable technique (popen() isn't very portable to begin with), or try to figure out a way to cause the "player" program to not require you to press the enter key.

    Does the man page give an option to disable the looping?

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    It's actually a little player I wrote myself wrapped around the openspc library which permits interpretation of SPC700 sound files on a PC. It sends the converted output to stdout where aplay was grabbing it. The format was intended to loop continuously, so I tried to implement it that way.

    Thanks for the info. I am completely new to pipes and was taking the lazy way out by using it. I'll just take a look at aplay's source and see if I can't hack together something that uses it directly to avoid the problem. Thanks again for the info.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by RemmyLee View Post
    It's actually a little player I wrote myself wrapped around the openspc library which permits interpretation of SPC700 sound files on a PC. It sends the converted output to stdout where aplay was grabbing it. The format was intended to loop continuously, so I tried to implement it that way.
    If it's a program you control, you could add a program option which tells it not to loop. Then pass this option when you call it inside popen().

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Do you actually need the output of aplay? Because if not, you can pass "w" to popen() and just write the newline.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  2. Help with forking & arrays of pipes
    By andyroo in forum C Programming
    Replies: 1
    Last Post: 03-02-2005, 04:47 AM
  3. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 07:35 PM
  4. the MAIL command in UNIX and Pipes
    By RoshanX in forum Linux Programming
    Replies: 19
    Last Post: 10-21-2003, 10:40 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21