3

I have a library of different words/phrases, and in order to build sentences at present I add a combination of these phrases into a playlist to make a sentence. Unfortunately if the user is running CPU intensive applications (which most of my users are) there can be a lag of a few seconds mid-sentence (in between phrases).

In order to combat this I was thinking of an approach which will merge the right combination of MP3 files on the fly into an appropriate phrase, save this in the %temp% directory, and then play this 1 MP3 file which should overcome the problem I'm experiencing with gaps.

What is the easiest way to do this in C#? Is there an easy way to do this? The files are fairly small, 3-4 seconds long each, and a sentence can consist of 3-20ish phrases.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
James
  • 1,397
  • 3
  • 21
  • 30

4 Answers4

6

here's how you can concatenate MP3 files using NAudio:

public static void Combine(string[] inputFiles, Stream output)
{
    foreach (string file in inputFiles)
    {
        Mp3FileReader reader = new Mp3FileReader(file);
        if ((output.Position == 0) && (reader.Id3v2Tag != null))
        {
            output.Write(reader.Id3v2Tag.RawData, 0, reader.Id3v2Tag.RawData.Length);
        }
        Mp3Frame frame;
        while ((frame = reader.ReadNextFrame()) != null)
        {
            output.Write(frame.RawData, 0, frame.RawData.Length);
        }
    }
}

see here for more info

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • where is the rest mark ? how do you save stream output as a new mp3 file ? tell me please ty how do you call this Combine function where is the before ? – Furkan Gözükara Jan 26 '15 at 13:24
  • @MonsterMMORPG save stream to file : https://stackoverflow.com/questions/411592/how-do-i-save-a-stream-to-a-file-in-c For those of you using the `Mp3FileReader(stream)` constructor, make sure you set `stream.Position = 0;` before calling the constructor – Ambrose Leung Apr 27 '18 at 21:41
0

As MP3s are a compressed audio source, I imagine that you can't just concatenate them into a single file without decoding each one first to the wave form that it would play. This may be quite intensive. Perhaps you could cheat by using a critical section when playing back your phrase so that the CPU is not stolen from you until the phrase was complete. This isn't necessarily playing nice with other threads but might work if your phrases are short.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
  • 1
    This isn't correct. You *can* split/merge an MP3 stream, but it needs to be done very carefully due to the bit reservoir. See http://lame.sourceforge.net/tech-FAQ.txt – Brad Jul 06 '11 at 21:34
0

MP3 files consist of "frames", that each represent a short snippet (I think around 25 ms) of audio.

So yes, you can just concatenate them without a problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bart
  • 7,640
  • 3
  • 33
  • 40
  • I will mostly have control over which mp3 files are used, would there ever be a problem if different bit rates were used? – James Jul 21 '09 at 19:20
  • No problem if your audio player can handle VBR files, because that's exactly how VBR works. – bart Jul 22 '09 at 13:07
  • 3
    This is completely incorrect. You cannot simply stick frames up against each other, due to the bit reservoir. See section 4 of this: http://lame.sourceforge.net/tech-FAQ.txt – Brad Jul 06 '11 at 21:33
  • @Brad So, if I understand correctly (without reading the whole article you posted), it is perfectly fine to join existing MP3s (since they should be still able to find the previous frames - in my logic at least), just it is not OK to mix and match their frames (which are the building blocks of the complete MP3s). E.g. it is fine to stick the one after the other, but not fine to insert the one in the middle of the other. Am I getting it right? But the title of section 4 seems to imply (under my bad understanding of english) that even joining 2 whole MP3s is not OK. :( – NoOne Nov 24 '15 at 20:53
  • 1
    You are correct. As long as the two streams of MP3 data are of the same channel count and sample rate, then you can concatenate then together. They must also contain no ID3 tags or other add-ons. Section 4 is simply stating that the original streams to be spliced cannot be due to how they were cut from their own bit reservoir data. If your streams don't have this problem (because they are complete or the bit reservoir is disabled) then you can concatenate no problem. – Brad Nov 24 '15 at 21:23
  • Warning: concatenating mp3 files does not play in Google Chrome HTML5 player - it plays fine in other media players though – Ambrose Leung Apr 27 '18 at 21:11
-1

On simple option is to shell to the command line:

copy /b *.mp3 c:\new.mp3

Better would be to concatenate the streams. That's been answered here: What would be the fastest way to concatenate three files in C#?

Community
  • 1
  • 1
Jon Galloway
  • 52,327
  • 25
  • 125
  • 193
  • 1
    Concatinating the mp3 files would produce some wierd output considering that each file might have headers and tags – Niklas Arbin Apr 13 '16 at 13:26