1

I'm currently working on an audiobackend for my application, which runs on Mono/.NET. Therefore I found, that SFML is plattformindependent, so it suits. But there is no mp3 support because of licensing.

So I wanted to add up the mp3 capability for myself in C#. I use SFML.Net 2.1 with Mp3Sharp (http://www.robburke.net/mle/mp3sharp/). At first I tried this:

Mp3Stream stream = new Mp3Stream("D:\\tmp\\test.mp3");
Music music = new Music(stream);
music.play();

But that didn't work. Exception:

AudioCuesheetEditor.AudioBackend.AudioManagerSFML: FATAL | SFML.LoadingFailedException: Failed to load music from memory
at SFML.Audio.Music..ctor(Stream stream)
at AudioCuesheetEditor.AudioBackend.AudioManagerSFML.setMusic() in d:\tmp\AudioCuesheetEditor\src\AudioCuesheetEditor\AudioBackend\AudioManagerSFML.cs

So I thought about doing as it is done in the examples (https://github.com/LaurentGomila/SFML/wiki/Source:-MP3-Player):

I made a class SFMLMp3Stream which extends SoundStream:

public class SFMLMp3Stream : SoundStream
{
    private Mp3Stream stream;

    public SFMLMp3Stream()
    {
        stream = null;
    }

    public Boolean openFromFile(String _filename)
    {
        //TODO:Check file existence
        stream = new Mp3Stream(_filename);
        Initialize((uint)stream.ChannelCount,(uint) stream.Frequency);
        return true;
    }

    #region implemented abstract members of SoundStream

    protected override bool OnGetData(out short[] samples)
    {
        if (stream != null)
        {
            byte[] buffer = new byte[512];
            short[] retVal = new short[512];
            stream.Read(buffer,0,buffer.Length);
            Buffer.BlockCopy(buffer,0,retVal,0,buffer.Length);
            samples = retVal;
            return true;
        }
        else
        {
            samples = null;
            return false;
        }
    }

    protected override void OnSeek(TimeSpan timeOffset)
    {
        if (stream != null)
        {
            stream.Seek((long)timeOffset.TotalMilliseconds, SeekOrigin.Current);
        }
    }

    #endregion
}

When I try now this:

musicStream = new SFMLMp3Stream();
musicStream.openFromFile(this.objProgram.getObjCuesheet().getAudiofilePath(true));
music = new Music(musicStream);

I get the compiler error, that Music can not be initiatet from SFMLMp3Stream (because it is no string and no System.IO.Stream. How can I extend SFML for Mp3 usage? Has anybody done this so far?

Thanks for your help Sven

Sven
  • 447
  • 1
  • 6
  • 22
  • If Music() constructor requires System.IO.Stream as argument why not to try implement Stream interface in your SFMLMp3Stream class, pointing Stream interface methods to appropriate Mp3Stream class methods? – Sergey Zhukov Jun 04 '14 at 08:02
  • Probably unrelated to the actual issue, but is there any particular reason you're using a 10 year old mp3 decoder? You might try NLayer (http://nlayer.codeplex.com/) instead. – ioctlLR Jun 04 '14 at 12:32
  • @user128048 That is a good idea, thanks, I'll try this. – Sven Jun 05 '14 at 06:06
  • @ioctlLR NLayer depends on NAudio, which is not plattformindependend (somebody said it doesn't work with Mono). But if you have another Framework, I'm interested ;). – Sven Jun 05 '14 at 06:07
  • Only NLayer.NAudioSupport depends on NAudio. NLayer itself has no dependencies beyond the base class library. – ioctlLR Jun 05 '14 at 12:50
  • Well that sounds nice, but what are the base class library dependencies? There is almost no documentation. – Sven Jun 05 '14 at 18:56

0 Answers0