locked
Trying to play ogg music combining NVorbis and XNA SoundEffect RRS feed

  • Question

  • Hello,
    I try to play a ogg music file using NVorbis and XNA SoundEffect (I have downloaded NVorbis from https://nvorbis.codeplex.com and rebuild it as Windows phone class to reference it in my main app).

    here is the code:

                    

        SoundEffect toto;
            SoundEffectInstance toti;

    string _audioPath = "music/rubberduckbassbat2.ogg"; using (var vorbis = new NVorbis.VorbisReader(_audioPath)) { int channels = vorbis.Channels; int sampleRate = vorbis.SampleRate; var duration = vorbis.TotalTime; var buffer = new float[vorbis.TotalSamples]; int count; while ((count = vorbis.ReadSamples(buffer, 0, buffer.Length)) > 0) { } var byteArray = new byte[buffer.Length * 4]; Buffer.BlockCopy(buffer, 0, byteArray, 0, byteArray.Length); MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); WriteWave(writer, 2, 44100, byteArray); stream.Position = 0; toto = SoundEffect.FromStream(stream); toti = toto.CreateInstance(); toti.Play(); }

    the WriteWave is a function that write the wav header (I tried using it and also not using it, by giving directly the byteArray to the SoundEffect constructor.

           private static void WriteWave(BinaryWriter writer, int channels, int rate, byte[] data)
            {
                writer.Write(new char[4] { 'R', 'I', 'F', 'F' });
                writer.Write((int)(36 + data.Length));
                writer.Write(new char[4] { 'W', 'A', 'V', 'E' });
    
                writer.Write(new char[4] { 'f', 'm', 't', ' ' });
                writer.Write((int)16);
                writer.Write((short)1);
                writer.Write((short)channels);
                writer.Write((int)rate);
                writer.Write((int)(rate * ((16 * channels) / 8)));
                writer.Write((short)((16 * channels) / 8));
                writer.Write((short)16);
    
                writer.Write(new char[4] { 'd', 'a', 't', 'a' });
                writer.Write((int)data.Length);
                writer.Write(data);
            }

    The informations looks ok (size, channels, bite rate, duration...)

    but when I play it, all I hear is a Noisy noise instead of the music.

    Any idea of whats going wrong?

    thanks a lot for any help.

    Maxime

    <cite class="_Rm"></cite>

    Friday, December 12, 2014 1:51 PM

All replies

  • Hello,

    Maybe I'm missing something but the value of 16 after the "fmt " chunk doesn't look right to me. Your wave format ex structure looks okay but you also don't supply a size for the wave format structure.

    That said the fact that it is plying at all suggests that the data chunk is being parsed. You are likely just off by a couple of bytes.

    I hope this helps,

    James


    Windows SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Saturday, December 13, 2014 2:09 AM
  • Hi James and thanks for your help,
    how do you think my function WriteWave should be to work?I got that function from the web as I don't know so much how to write a wave header.thank!

    Saturday, December 13, 2014 2:20 PM
  • Hello,

    I am using now sharpdx to create wave header:

                    string _audioPath = "music/2test.ogg";
                    NVorbis.Ogg.ContainerReader gg = new NVorbis.Ogg.ContainerReader(_audioPath);
                    using (var vorbis = new NVorbis.VorbisReader(gg))
                    {
                        int channels = vorbis.Channels;
                        int sampleRate = vorbis.SampleRate;
                        var duration = vorbis.TotalTime;
    
                        var buffer = new float[vorbis.TotalSamples];
                        
                        int count;
                        count = vorbis.ReadSamples(buffer, 0, buffer.Length);
                      /*  while ((count = vorbis.ReadSamples(buffer, 0, buffer.Length)) > 0)
                        {
                        }*/
                        var byteArray = new byte[buffer.Length * 4];
               
                        Buffer.BlockCopy(buffer, 0, byteArray, 0, byteArray.Length);
    
    
                        MemoryStream stream = new MemoryStream();
                       // BinaryWriter writer = new BinaryWriter(stream);
                        
                        SharpDX.Multimedia.WaveFormat wf = new SharpDX.Multimedia.WaveFormat(48000,16, 2);
                        SharpDX.Multimedia.WavWriter w = new SharpDX.Multimedia.WavWriter(stream);
                         
                        w.Begin(wf);
                        w.AppendData(buffer);
                        w.End();
                        //SharpDX.Multimedia.RiffParser p = new SharpDX.Multimedia.RiffParser(stream);
    
                        stream.Position = 0;
                        toto = SoundEffect.FromStream(stream);
                        toti = toto.CreateInstance();
                        toti.Play();
    I can hear the right music when it starts playing but there is some Noisy noise also, the sound is horrible and not clean, I really don't know what I'm missing.

    Monday, December 15, 2014 2:14 PM
  • Hello,

    I don't see anything obvious in the code you posted and unfortunately I'm not familiar with SharpDX. Maybe someone from the SharpDX community can help.

    -James


    Windows SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Wednesday, December 17, 2014 12:15 AM