# The right way to use loadBankCustom

**URL:** <https://qa.fmod.com/t/the-right-way-to-use-loadbankcustom/18836>\
**Category:** FMOD Engine\
**Tags:** unity, csharp\
**Created:** [June 3, 2022, 2:24pm UTC](https://qa.fmod.com/t/the-right-way-to-use-loadbankcustom/18836 "2022-06-03T14:24:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RiddleC](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@RiddleC](https://qa.fmod.com/u/RiddleC)\
**Post date:** [June 3, 2022, 2:24pm UTC](https://qa.fmod.com/t/the-right-way-to-use-loadbankcustom/18836/1 "2022-06-03T14:24:59Z")

</div>

I want use my own file system to manage FMOD bank resources. My FileSystem can provide a stream for runtimanager.  
‘loadBankCustom’ can load bank data with open, seek, read, close callbacks.But I did not really understand the correct method to use it.  
1、What does BANK\_INFO’s userdata refer to? Is a pointer to the bank resource data provided by my file system? In my case, should I convert my stream to IntPtr and assign it to UserData? When I did this, my program crashed.  
2、My understanding of each callback is to open, seek, read, or close a stream or file in a callback. Is it correct?  
3、What is the relationship between Handle and UserData in those callbacks? for example, in the FILE\_OPEN\_CALLBACK, (ref IntPtr handle, IntPtr userdata)

My example:

```auto
// get a stream from filesystem
Stream fmodStream = .........;

BANK_INFO info = new BANK_INFO();

info.opencallback = LoadedBank.OpenBankStreamCallback;
info.closecallback = LoadedBank.CloseBankStreamCallback;
info.seekcallback = LoadedBank.SeekBankStreamCallback;
info.readcallback = LoadedBank.ReadBankStreamCallback;

StreamToIntPtr(fmodStream, out info.userdata);
info.userdatalength = Marshal.SizeOf(info.userdata);

loadResult = StudioSystem.loadBankCustom(info, loadBankFlags, out bank);

#region

[MonoPInvokeCallback(typeof(FMOD.FILE_OPEN_CALLBACK))]
public static RESULT OpenBankStreamCallback(StringWrapper name, ref uint filesize, ref IntPtr handle, IntPtr userdata)
{
    handle = userdata;

    Stream stream = null;
    IntPtrToStream(handle, out stream);
    filesize = (uint)stream.Length;

    return RESULT.OK;
}

[MonoPInvokeCallback(typeof(FMOD.FILE_CLOSE_CALLBACK))]
public static RESULT CloseBankStreamCallback(IntPtr handle, IntPtr userdata)
{
    Stream stream = null;
     IntPtrToStream(handle, out stream);
     stream.Close();

     return RESULT.OK;
}

[MonoPInvokeCallback(typeof(FMOD.FILE_SEEK_CALLBACK))]
public static RESULT SeekBankStreamCallback(IntPtr handle, uint pos, IntPtr userdata)
{
    RESULT result = RESULT.ERR_FILE_NOTFOUND;
    Stream stream = null;
    IntPtrToStream(handle, out stream);
     var resPos = stream.Seek((long)pos, 0);
     if (resPos != -1)
         result = RESULT.OK;

     return result;
}

[MonoPInvokeCallback(typeof(FMOD.FILE_READ_CALLBACK))]
public static RESULT ReadBankStreamCallback(IntPtr handle, IntPtr buffer, uint sizebytes, ref uint bytesread, IntPtr userdata)
{
    RESULT result = RESULT.ERR_FILE_NOTFOUND;

     Stream stream = null;
     IntPtrToStream(handle, out stream);

      byte[] readBuffer = new byte[(int)sizebytes];
      uint bytesRead = (uint)stream.Read(readBuffer, 1, (int)sizebytes);
      buffer = ArrToPtr(readBuffer);

       bytesread = bytesRead;
           
       if (bytesRead != sizebytes)
       {
           result = RESULT.ERR_FILE_EOF;
       }
       else if (bytesRead > 0)
       {
           result = RESULT.OK;
        }

       return result;
}

#endregion

```

---

<div class="post-metadata">

**Author:** ![RiddleC](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@RiddleC](https://qa.fmod.com/u/RiddleC)\
**Post date:** [June 8, 2022, 5:13pm UTC](https://qa.fmod.com/t/the-right-way-to-use-loadbankcustom/18836/2 "2022-06-08T17:13:16Z")

</div>

I have fixed the crash problem.  
But The sizebytes in the FILE\_READ\_CALLBACK parameter is always 2048 bytes，which caused ERR\_FORMAT exception. Shouldn’t this value be the length of the required bank file？  
Anyone can help me? Thank you !  
Here is the log

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/d/d29253d9145712fbb231c40627e7d8e7a0131221.png)

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [June 10, 2022, 6:38am UTC](https://qa.fmod.com/t/the-right-way-to-use-loadbankcustom/18836/3 "2022-06-10T06:38:44Z")

</div>

2048 is the default blockalign value, which is just the size of the buffer to write to, and not a complete block of memory for holding an entire bank. You could increase the blockalign size when calling [System.setFileSystem](https://fmod.com/resources/documentation-api?version=2.02&page=core-api-system.html#system_setfilesystem) but I don’t think that would fix the issue in this case.  
Can you please share an updated code snippet so I can take a look at what might be going wrong?

---

<div class="post-metadata">

**Author:** ![RiddleC](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@RiddleC](https://qa.fmod.com/u/RiddleC)\
**Post date:** [June 13, 2022, 2:13pm UTC](https://qa.fmod.com/t/the-right-way-to-use-loadbankcustom/18836/6 "2022-06-13T14:13:18Z")

</div>

> [@RiddleC](#):
>
> Thank you for your replay!  
> I have fixed the load bank problem and inited FMOD successfully. But when I try to play audio in game, FMOD catch the exception like this:
> 
> ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/e/ea644dff626ee27cedfa181d43d62a9905c924fc.png)
> 
> ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/7/7f38713a39226ce68e1a28a897bf2698a136c846.png)
> 
> I did not use System.setFileSystem, I just used loadbankCustom. Is there any usage error here？

Otherwise, I play sound with EventInstance.

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [June 21, 2022, 4:53am UTC](https://qa.fmod.com/t/the-right-way-to-use-loadbankcustom/18836/7 "2022-06-21T04:53:14Z")

</div>

Can you please share an updated code snippet to help me reproduce this issue?
