Item Of the Day. Playing .net using FMOD

So lets say you want to connect to a website to stream their music through your app, but the problem is they dont give you a direct link only a connectstream from How do you play this with fmod? well after much trial and error and understanding the concept i have found the solution.

Im assuming you already know how to setup FMOD so i wont go through that.

1) you will get your stream and you must know the length. You will have to rely on the content length send over the http stream since you cant get the length of a when using http.

2) This is the fun part. You will need to take that stream and load it into a byte array. If you want your data in real time and dont want to load the entire buffer(dowload the entire file) you will need to load the data in a thread. Then you will need to trigger for the orginal thread to continue once the initial buffer length of the data has been loaded into the new byte array.

3) once the initial data has been loaded into the byte array just pass that byte data to FMOD createStream function then OPENMEMORY Flag and also create a CREATESOUNDEXINFO and pass those items in and then play your sound. The thread will still be loading the data in the background while the app is playing the sound or music.

There is some extra code in SaveMemTask that gets the pointer to the mem but it is not needed since that approach didnt work too good for me.

Check out the code below.

 Sub PlaySong(ByRef songStream As System.IO.Stream, ByVal streamLength As UInt32, ByVal stitle As String)

        Dim result As FMOD.RESULT
        If channel IsNot Nothing Then channel.stop()
        If sound IsNot Nothing Then sound.release()

        sinfo.cbsize = Runtime.InteropServices.Marshal.SizeOf(sinfo)
        sinfo.length = streamLength
        sinfo.format = FMOD.SOUND_FORMAT.MPEG

        Dim streamptr As IntPtr = StreamToByteArray(songStream, streamLength)

        result = sys.createStream(rawData, (FMOD.MODE._2D Or FMOD.MODE.HARDWARE Or FMOD.MODE.CREATESTREAM Or FMOD.MODE.OPENMEMORY), sinfo, sound)

        result = sys.playSound(FMOD.CHANNELINDEX.FREE, sound, False, channel)
End Sub
Protected Function StreamToByteArray(ByRef songstream As IO.Stream, ByVal streamLength As UInt32) As IntPtr
        WaitForThread = True
        Me.streamLength = streamLength
        AddHandler InitDataRecived, AddressOf InitDataReceivedHandler

        trd = New System.Threading.Thread(AddressOf SaveMemTask)
        trd.IsBackground = True
        Me.songstream = songstream
        Me.saveTo = saveTo

        While WaitForThread = True
        End While

        Return MyStreamPtr
    End Function
 'Global Variables     Protected rawData() As Byte
    Dim streamLength As UInt32
    Private Sub SaveMemTask()

        ReDim rawData(streamLength)
        Dim ran As Boolean = False
        Dim sLength As Integer = 1024
        Dim buffer(sLength) As Byte
        Dim bytesRead As Integer = songstream.Read(Buffer, 0, sLength)
        Dim bpos As Integer = 0

        While (bytesRead > 0)

            For i As Integer = 0 To bytesRead - 1
                rawData(i + bpos) = buffer(i)

            bpos += bytesRead
            If ran = False Then
                ran = True
                rawDataHandle = GCHandle.Alloc(rawData, GCHandleType.Pinned)
                address = rawDataHandle.AddrOfPinnedObject()
                RaiseEvent InitDataRecived(address)

            End If

            bytesRead = songstream.Read(Buffer, 0, sLength)
        End While

    End Sub
    Private Sub InitDataReceivedHandler(ByVal ptr As IntPtr)
        MyStreamPtr = ptr
        WaitForThread = False
    End Sub
    Private songstream As IO.Stream
    Private saveTo As String
    Private WaitForThread As Boolean = True
    Private MyStreamPtr As IntPtr
    Protected Event InitDataRecived(ByVal ptr As IntPtr)
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

16 Responses to Item Of the Day. Playing .net using FMOD

  1. Fleta says:

    Ho ho, who wdolua thunk it, right?

    • Bobs says:

      Phenomenal braekdown of the topic, you should write for me too!

      • Musef says:

        lol thanks, go to my wiebste and you can download the sourcecode (my wiebste is in the video description) I dont have the actual .exe to give out right now, but if you have a compiler you can compile the source code and run it. You’re also going to need to download the fmod api (for sounds) or else you wont be able to compile it.

        • Junesh says:

          Well, SFML can be very easily exetdned as the library itself is very cleanly designed even from internal implementation point of view, not just regarding to the provided interface.Documentation comes in multiple types. First, there are tutorials, next, the API documentation is actually the code itself.Unfortunately there are no tutorials yet for the latest development branch but only for the latest release, however, most of the tutorials are still valid and the best thing in the SFML code is that it contains huge amounts of comments that almost make any kind of documentation unnecessary.By the way, I still praise SDL as it is a proven toolset, but anyway, I would strongly suggest you to check out SFML as I ended up with much less burden about different things since I use it.SFML does not really have plugins as it has a far smaller user base yet compared to SDL, but it is kind of an end-to-end solution for most of the tasks needed. It even contains some things like sprite based graphics that most probably I’ll never use, but it allows me not to link that part of the library with my application if I don’t need it.

  2. Nicole says:

    The blog is cool

    • Embiye says:

      @ViProFL lol I might just add a link to it sometime, a lot of ploepe have asked me for the code and stuff, its just very terrible Since it was my first real c++ game I worked on, its very hacked together and its coded very badly. So it’s not the best project to learn from what so ever lol

  3. Classifieds says:

    I think that everything said was actually very logical. But, what about this? suppose you were to write a awesome post title? I mean, I don’t want to tell you how to run your website, however what if you added a headline that grabbed a person’s attention? I mean Item Of the Day. Playing .net using FMOD | Programming Life is a little vanilla. You might peek at Yahoo’s home page and watch how they create news headlines to grab people to open the links. You might add a related video or a picture or two to get readers interested about everything’ve written. In my opinion, it might bring your posts a little bit more interesting.

  4. Aubrey says:

    I do not even know how I ended up here, but I thought this post was great. I do not know who you are but certainly you’re going to a famous blogger if you aren’t already 😉 Cheers!

    • diko says:

      No problem. It’s Assembly thoguh, assembler would be the compiler itself. You can ask Intel for the IA32 manuals, they’re free. Or just download the PDFs from their site.The code is almost entirely taken from a manual. First we load X and Y onto the stack, We then loop until the control flag is cleared. So if the result of TEST is not zero (JNZ), we jump onto the label that we previously declared. We finally pop and we return.FSTSW is simply used to get the condition bits into AX.You may test the routine like so:For i=0 To 19Debug fmod i*0.1, 0.5 NextCheers.

  5. miglior conto deposito says:

    I simply want to tell you that I’m newbie to blogging and certainly savored this page. Very likely I’m want to bookmark your website . You actually have incredible well written articles. Cheers for sharing your webpage.

  6. I always emailed this weblog post page to all my contacts, since if like to read it
    afterward my contacts will too.

  7. Hi there friends, how is everything, and what you desire to say regarding this
    piece of writing, in my view its in fact amazing for me.

  8. Hi there to every one, it’s actually a pleasant for me
    to pay a visit this web page, it includes useful Information.

  9. Currently it sounds like BlogEngine is the preferred blogging platform available right now.
    (from what I’ve read) Is that what you are using on your blog?

  10. First off I would like to say awesome blog! I had a quick question which I’d like to ask
    if you don’t mind. I was interested to find out how you center yourself and clear
    your mind before writing. I have had a hard time clearing
    my mind in getting my ideas out there. I do enjoy writing
    however it just seems like the first 10 to 15 minutes are usually lost simply just trying to
    figure out how to begin. Any recommendations or tips?

  11. I was frustrated because I was afraid of speaking at all.
    Pizza Box ‘ The boxes are recyclable but only
    if they are not greased and carrying leftover of the pizza you had.
    To balance the ratio of functionality and quality, homeowners rely on the experience and expertise of interior
    decorators and architects.

    My website: cheap kitchen remodeling ideas

Leave a Reply to diko Cancel reply

Your email address will not be published. Required fields are marked *