How to piece together multiple sound files into one streamed .wav or .mp3 file?

This is a discussion on How to piece together multiple sound files into one streamed .wav or .mp3 file? within the C Programming forums, part of the General Programming Boards category; My friend is trying to build an FM transmitter and wants to broadcast a DNA sequence over it (mitochondria). So, ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    How to piece together multiple sound files into one streamed .wav or .mp3 file?

    My friend is trying to build an FM transmitter and wants to broadcast a DNA sequence over it (mitochondria).

    So, to help him out I want to help him making the .wav or .mp3 file to broadcast this sequence with.

    What I have is a bunch of .wav files of a voice recorded saying a letter such as 'A'

    I also have a text file with a string of about 18000 characters.

    What I want to do is import all of these .wav files into my program and then randomly associate one of the files to each individual character in the string.

    What I'm trying to figure out is how to import these .wav files, randomize them to the character buffer, one char at a time and then output a single .wav or .mp3 file that has the recorded voices saying the DNA sequence.


    Anyone know how to do this and is it possible? Let me know if you need more details. I wasn't really sure what language to do this in but picked C as it is the one I am currently learning. i'm willing to use something else if C won't work though.

    Any sort of direction on where to start would help a lot. I'm not concerned about how to do the randomizing or how to handle the string or anything. mostly i'm concerned with how to output all these .wav files into one .wav file

  2. #2
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Is your nick supposed to mean "Hagbard Celine"? In this case, I'd say "Think for yourself, schmuck!" ;-)

    Assembling a .wav file that contains 18000 spoken characters is not a good idea. It will be huge, and it's far more elegant to write a program that reads the file and produces the corresponding sound on-the-fly:

    Code:
    char file[10];
    
    while((c = fgetc(fp)) != EOF) {
            sprintf(file, "%c.wav", c);
            call_external_program("my_wavplayer %s", file);
    }
    If you just want to let the world know about the DNA sequence of a mitochondrion, it's probably more convenient to assemble a MIDI file where each instrument corresponds to a nucleoside. It will be much less boring to listen to, and it's easier to remember ;-)

    MP3 is a streaming format, so simply appending two mp3 files should also work. On my system, I tested it with "cat a.mp3 b.mp3 >> c.mp3 && mpg123 c.mp3" and it worked perfectly. Don't forget to remove the ID3 tag from a.mp3 (i.e. the last 128 bytes), if it has one. You can easily do this with a hex editor, because the first three bytes of the ID3 tag are 'T', 'A' and 'G', if I remember correctly.

    If you still insist on generating .wav files, I'd suggest using a library that reads and understands .wav files. Maybe SDL is a good starting point, but actually I don't know anything about graphics and sound programming. If this is supposed to be a short hack, I'd use Perl and whatever module that implements .wav support.

    Greets,
    Philip
    Last edited by Snafuist; 03-22-2009 at 07:43 PM.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    First, yes my nick is supposed to mean Hagbard Celine

    The output file doesn't need to be .wav. mp3 will work just fine, or anything that will play on an ipod.

    I normally would just run the program and have it output the sound on the fly but he's going to be playing this through his ipod, so i'd like it on a single, or multiple mp3 files or other suitable format.

    I guess I should mention that I'm relatively in the dark about how sound files work. So, I don't know what an ID3 tag is. (though i will look it up)

    Also keep in mind i'm doing this more as a learning tool than anything else. I'm trying just to get more experience with using tools like linux, GNU, perl, C, etc to put stuff together.


    I'm interested in this paragraph:

    "MP3 is a streaming format, so simply appending two mp3 files should also work. On my system, I tested it with "cat a.mp3 b.mp3 >> c.mp3 && mpg123 c.mp3" and it worked perfectly. Don't forget to remove the ID3 tag from a.mp3 (i.e. the last 128 bytes), if it has one. You can easily do this with a hex editor, because the first three bytes of the ID3 tag are 'T', 'A' and 'G', if I remember correctly."

    So are you saying it would be easier to convert the .wav into .mp3 and then append the mp3's together? I don't understand what the && mpg123 part means though.

  4. #4
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    mpg123 is a linux mp3 player.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  5. #5
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by hagbardC View Post
    So are you saying it would be easier to convert the .wav into .mp3 and then append the mp3's together?
    Exactly. MP3 files store information in small "frames", which are independent of each other. You can cut a MP3 file in two halves and play each of them separately (which is necessary for streaming). It also works the other way around: you can concatenate two MP3 files and it will play as it were one MP3 file.

    I don't understand what the && mpg123 part means though.
    You don't need to. Anyway, in most Unix shells, "cmd1 && cmd2" means "execute cmd2 iff cmd1 succeeded". Here, mpg123 is a popular console MP3 player.


    I think the easiest way to obtain an iPod-playable sound file is to build a small program which produces a shell script (or batch file, if you're using Windows) which will produce the MP3 file.

    Under Unix, the shell script would probably look like this:

    Code:
    #!/bin/sh
    
    # delete output file if it already exists
    rm output.mp3
    
    # concatenate the contents of <x>.mp3 to output.mp3
    cat a.mp3 >> output.mp3
    cat t.mp3 >> output.mp3
    ...
    Writing a program that will generate the above shell script is fairly easy. If your primary intent is to learn more about C, you may as well write a program which concatenates the files directly (you don't need to know anything about the MP3 format).

    I don't know the equivalent of "cat" in the Windows command prompt, maybe "type" works. Also, I'm not sure whether the Windows stuff supports output redirection. Of course, it's easy to write a command which simulates the intended behavior, but I'm sure someone else on this forum can tell you how it is done natively.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  6. #6
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Code:
    # delete output file if it already exists
    rm output.mp3
    
    # concatenate the contents of <x>.mp3 to output.mp3
    cat a.mp3 >> output.mp3
    cat t.mp3 >> output.mp3
    . . . could be replaced by something like . . .

    Code:
    cat a.mp3 > output.mp3
    cat t.mp3 >> output.mp3
    . . . which is a little bit more efficient and smaller.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  7. #7
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by Nightowl View Post
    . . . which is a little bit more efficient and smaller.
    This depends on your notion of efficiency. Your solution involves an if-statement in the loop that generates the shell script (i.e. 18000 wrong branch predictions), or having the first iteration unrolled manually. Either way, it needs more brain time, which tends to be more expensive than CPU time. If the "rm" should turn out to be an efficiency issue, it can be optimized away later ;P

    The best choice is probably to remove the "rm" as well as the ">" and let the user delete the file himself if necessary.

    Nitpicking again,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Searching Binary Files for a Pattern
    By CaptainMorgan in forum C Programming
    Replies: 16
    Last Post: 06-17-2007, 07:04 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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