locked
Combining audio files with silence

    Question

  • How can I combine multiple audio files into a single file with silence (or no data) between them?
    Wednesday, April 10, 2013 12:38 PM

Answers

  • Hi Stephen,

    Here is the reply I got:

    MFCopy would be the closest thing, but not complete. You would have to implement another source reader to pick up the second file when the first one is complete. As well, there are some gotchas like  make sure the timestamps are set correctly when it gets to the second file. At least it would demonstrate how to process media samples and processing the output to the SinkWriter.

    http://blogs.msdn.com/b/mf/archive/2009/12/16/mfcopy.aspx

    With the SinkWriter, if you don’t want to re-encode and only want to mux the compressed samples from both files to a new file container, then you just don’t call SetInputType. You have to really be careful of this since the actual sample data has to be right or it could fall apart around the transition point.

    -Jeff


    Jeff Sanders (MSFT)

    Friday, April 12, 2013 8:27 PM
    Moderator

All replies

  • Hi Stephen,

    You will need a 3rd party library or combine the files yourself (this would require you understand the file format you are reading and writing to).

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, April 10, 2013 2:05 PM
    Moderator
  • I have the code below. What I'd like to do is take multiple audio input streams and write them out to a single stream before adding them to the sinkwriter (because AFAIK sinkwriter will only accept one audio track).

    Given I know the input type (from GetCurrentMediaType()) and I know the output type (which in my case I'll keep the same) why do I need to understand the file format to create a new output file? 

    void VideoWriter::AddAudioTrack(IRandomAccessStream^ inputStream)
    {
        ComPtr<IMFMediaType> _spMFTypeIn;

        // Convert the IRandomAccessStream into a MediaFoundation IMFByteStream
        // Note: _spMFByteStream has class-scope
        CHK(MFCreateMFByteStreamOnStreamEx((IUnknown*)inputStream, &_spMFAudioByteStream));

        // Read the Audio from the stream
        // IMFSourceReader _spMFAudioReader has class-scope
        CHK(MFCreateSourceReaderFromByteStream(_spMFAudioByteStream.Get(), nullptr, &_spMFAudioReader));

        CHK(_spMFAudioReader->SetStreamSelection(MF_SOURCE_READER_FIRST_AUDIO_STREAM, TRUE));
        CHK(_spMFAudioReader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_AUDIO_STREAM, &_spMFTypeIn));

        // Add the input mediatype directly to the writer (with no changes)
        CHK(_spSinkWriter->AddStream(_spMFTypeIn.Get(), &_streamIndexAudio));

        // Set the inputtype of the output audio stream
        CHK(_spSinkWriter->SetInputMediaType( _streamIndexAudio, _spMFTypeIn.Get(), NULL ) );

        _spMFTypeIn = nullptr;
    }

    Wednesday, April 10, 2013 11:46 PM
  • Hi Stephen,

    You did not specify you were streaming!  You will need to create your own stream classes and when you have streamed that last byte of the first audio file, then start streaming the new data.

    I don't believe there is any specific sample for this!

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, April 11, 2013 8:17 PM
    Moderator
  • Hey Jeff, I'm just writing data out to a disk file. Maybe I'm using the wrong code (there doesn't seem to be a lot of same code about for writing audio and video for store apps). However, the above code works when I have a single piece of audio (like a music track) which I'm adding to a video track.
    Thursday, April 11, 2013 11:01 PM
  • Hi Stephen,

    Yes it is much more complicated if you are going to combine audio into one file.  You would need to SinkWriter and have to manually write this out.  This is not a function of Windows Store apps but part of understanding the Media Format architecture.

    You would be remixing - http://msdn.microsoft.com/en-us/library/windows/desktop/ff485861(v=vs.85).aspx

    Again, I don't know of any samples that show this however.  I will ask my co-workers to reply to this thread if they are aware of any.

    -Jeff


    Jeff Sanders (MSFT)

    Friday, April 12, 2013 7:16 PM
    Moderator
  • Hi Stephen,

    Here is the reply I got:

    MFCopy would be the closest thing, but not complete. You would have to implement another source reader to pick up the second file when the first one is complete. As well, there are some gotchas like  make sure the timestamps are set correctly when it gets to the second file. At least it would demonstrate how to process media samples and processing the output to the SinkWriter.

    http://blogs.msdn.com/b/mf/archive/2009/12/16/mfcopy.aspx

    With the SinkWriter, if you don’t want to re-encode and only want to mux the compressed samples from both files to a new file container, then you just don’t call SetInputType. You have to really be careful of this since the actual sample data has to be right or it could fall apart around the transition point.

    -Jeff


    Jeff Sanders (MSFT)

    Friday, April 12, 2013 8:27 PM
    Moderator
  • When writing my original video/audio sinkwriter code I'd discounted MFCopy because I couldn't get the sample to run on Win8/RT - but maybe some of the code maybe useful to examine for my latest scenario.

    At a high level, here is what I think I need to do:

    1. Create IMFSinkWriter in my desired format
    2. Create IMFSourceReader with first/next audio file
    3. Get first stream from source (ignore multiple streams)
    4. Get audio sample
    5. Add timing offset if not first audio file
    6. Possibly transcode if they don't match SinkWriter format - not sure about this step
    7. Write sample to SinkWriter
    8. Repeat 4-7 until end of audio file
    9. If gap to next audio, create new audio sample with no audio but a duration (not sure about this step)
    10. Notify SinkWriter of the gap with SinkWriter->SendStreamTick()
    11. Repeat 2-10 for the remaining audio files
    12. Finalize the SinkWriter

    Any thoughts about this / am I missing anything?

    Saturday, April 13, 2013 3:38 AM
  • Hi Stephen

    Finally got a video mixing and audio if so could you send me an email to eduardo_13_8@hotmail.com.

    Thank you very much for your time and sorry for my English


    Thursday, May 15, 2014 5:28 PM