views:

1237

answers:

5

Hey,

I'm working on a streaming server that will be capable of broadcasting targetted ads. Basically listeners hear the same music, but every, say, 30 minutes comes a block of ads and every listener has his/her own block. Implementing such streaming server poses various problems and this question is about one of them.

The server will work in a manner similar to Icecast, i.e. it will read the stream over the network from some stream generator and relay it to every listener. When it's time to broadcast ads, the server stops fetching the stream from the generator, reads ads from files and inserts them into each listener's buffer, transmits them and resumes on relaying stream from the generator.

When the server switches from relaying stream to broadcasting ads, it has to concatenate two MP3 streams (we broadcast in MP3). My concern is that simply appending one piece of data after another may produce some audible artifacts. Can it be done seamlessly?

I've already figured out this: - I can make the server be aware of MP3 frames to avoid sync errors. - I'm thinking about appending MP3 frames from the ad file after MP3 frames from the stream. - Since ad is loaded from properly encoded MP3 file, I circumvent the problem of byte reservoir, because the first frame from the file can't use it.

But my concern is the way MDCT works. Listeners have no idea of what my server will do, so their MP3 decoders may produce some artifacts because incorrect MDCT data will be placed one after another in the stream they download. Will zero-padding at the beginning of the file with the ad compensate for this?

Do you know any libraries/tools (open source if possible) that can seamlessly join two MP3 files without decompressing them?

Can you point any good resources describing MP3 format? I searched Internet a lot, found lots of information, but I still miss the overall picture.

Maybe you know that this would be easier if I used another codec like OGG/Vorbis, AAC?

Regards,

Mike

PS. This question is not a duplicate of What is the best way to merge mp3 files?. mp3wrap and tools alike are not an option for me.

A: 

If you're on Windows, the Microsoft DirectShow API may be the way to go. You should find that is is capable of doing things with audio and video both statically and streaming, in a variety of formats (you only need the necessary codecs, and the interface is virtually the same for all).

Saying this, DirectShow is unfortunately designed in a horribly intricate way, and has a steep learning curve, but the power it offers in unparallel if you're going to be doing audio/video manipulation on Windows. There are however a great number of samples and tutorials on how to use it, so it may not be so painful in the end. Also, if you're using the .NET Framework, there is a managed wrapped by the name of DirectShow.NET. It's not going to be an easy task whatever you do, unless there's something out there than I'm not aware of. Good luck with it anyway!

Noldorin
Such an API could be too computationally expensive. The radio station I work at already has 5k users/server peak traffic. Even if it's only one second of music that I have to process for each listeners, that's over an hour of music to decompress/compress in no time...
Jasiu
I'm not sure it would necessarily be... you should really do some more investigating into this, as DirectShow is *the* way to go for media stuff on Windows.
Noldorin
+1  A: 

I believe MP3s can be merged by simply concatenating the files. In some quick testing (cat file1.mp3 file2.mp3 > merged.mp3; mplayer merged.mp3) it seems to work as expected. Streaming from a web server probably will work just as well.

How are you going to handle switching the current input file? You can simply treat the advertisements as short tracks to play.

John Millikin
Yes, that's the way I'd like to go, but are you sure it works and there are no circumstances under which an audible glitch will be produced?
Jasiu
Most likey this won't work... there are multiple mp3 formats... You can have a constant bit rate mp3 set with some constant frame size (so many bits per sample), or a variable bit rate mp3 that fluctuates... they are not compatible. Also simply concatenating would put the header and id3 tags in the middle of the file, so media files would have issues playing the file.If you want to do this the correct way you will either need to use software that does this, or convert both audit files to a single format and then concatenate the audio streams and save in a new file.
uzbones
Let's say I don't have any ID3 tags and I use constant bitrate.
Jasiu
If you have mp3s with the same constant bitrate and sampling speed (khz) then the above should work for concatenating the files. As others mentioned the tags won't work in the media player but should be skipped over.
uzbones
I can't guarantee "no circumstances" -- there might be audio players that will react poorly to concatenated MP3s. However, it should work for most cases. If you're concerned, try the command I posted on some local files and see if it works.
John Millikin
A: 

I approached a very similar problem, and after asking the right questions at various sources came up with the following...

Any worthy decoder will skip "bad" data until it hits a valid frame header. This is what ID3v2 relies upon to inject additional information into mp3 data. At the server, I'd go with analysis of source MP3 files to only serve valid MP3 frames. If you serve a few silent frames (about 7 should do it), the decoder should have time to settle before ramping up for the next load of (unassociated) MP3 data, avoiding the artefacts you (correctly) assume when concatenating frames from different encoding sessions.

More problematic is the possible switching of MP3 attributes (1/2 channels, output sample rate etc) between one frame to the next. Some decoders get quite upset when confronted with such a stream, resulting in 1/2 speed playback and the like. So, you need to ensure that all your source material is encoded to the same output attributes otherwise you may come unstuck.

You may have seen this already, but if not:

http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=79&printer=t

spender
A: 

I don't see why you would want to concatenate the files. Why don't you use some sort of play list system and just change which file your sending. I would think this would allow more flexibility in the long run, and you wouldn't end up with large MP3 files.

uzbones
I'm not sure if I understand what you're saying, but cant see how your idea enables targetted ads. My radio uses SHOUTcast/Icy protocol, there are various players, so I can't do anything on the client side. I'm talking about files, because it doesn't matter for the sake of this question, but in reality I'll use MP3 streams generated on the fly.
Jasiu
It would all be server side... Basically the server would treat the ads as songs, except you would alternate between songs and ads. I'm assuming your not concatenating all of the songs together when you put them in the stream...
uzbones
+2  A: 

You should be able to concatenate mp3 files of both CBR and VBR formats. MP3 files do not have a main header (disregarding ID3 and Xing). The audio data is stored as chunks where every chunk includes its own header. The header contains the necessary information (bitrate, sample frequency, stereo, etc) for the decoding of the audio data in that chunk.

This is one of the reasons why it is difficult to determine the duration of a mp3 file.

Another way of looking at it is, if you concatenate a CBR MP3 file with a VBR file, the end result is the same as one long VBR file with the first section of Audio at a constant bitrate.

The issue is that some MP3 players may be strict and expect a Xing header for a VBR MP3 file. This however was never the specification for the MP3 format but it is now assumed to be true.

theahuramazda