# loadBank and resource package files

**URL:** https://qa.fmod.com/t/loadbank-and-resource-package-files/10980
**Category:** FMOD Studio
**Created:** [May 28, 2013, 8:52am UTC](https://qa.fmod.com/t/loadbank-and-resource-package-files/10980 "2013-05-28T08:52:06Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![salvofid](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@salvofid](https://qa.fmod.com/u/salvofid)
#### Post date: [May 28, 2013, 8:52am UTC](https://qa.fmod.com/t/loadbank-and-resource-package-files/10980/1 "2013-05-28T08:52:06Z")

</div>

Hi all,  
the fmod studio system provide a method to load a bank file using the filename as input, so, we can not put the bank file in our resource package file. There is any method to pass a file descriptor or a “memory file” to load the bank?  
Thanks, Salvo

---

<div class="post-metadata">

### Author: ![Guest](https://avatars.discourse-cdn.com/v4/letter/g/8dc957/32.png) [@Guest](https://qa.fmod.com/u/Guest)
#### Post date: [May 28, 2013, 10:32am UTC](https://qa.fmod.com/t/loadbank-and-resource-package-files/10980/2 "2013-05-28T10:32:56Z")

</div>

We do plan to support loading from memory, but unfortunately we haven’t had time to implement it yet. Is this a major obstacle for you at the moment?

---

<div class="post-metadata">

### Author: ![salvofid](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@salvofid](https://qa.fmod.com/u/salvofid)
#### Post date: [May 28, 2013, 12:42pm UTC](https://qa.fmod.com/t/loadbank-and-resource-package-files/10980/3 "2013-05-28T12:42:11Z")

</div>

Thanks for the reply… at the moment is not a problem, I’ll wait for the support.  
Bye, Salvo

---

<div class="post-metadata">

### Author: ![Guest](https://avatars.discourse-cdn.com/v4/letter/g/8dc957/32.png) [@Guest](https://qa.fmod.com/u/Guest)
#### Post date: [May 29, 2013, 1:37am UTC](https://qa.fmod.com/t/loadbank-and-resource-package-files/10980/4 "2013-05-29T01:37:27Z")

</div>

OK, we’ll let you know when it’s implemented.

---

<div class="post-metadata">

### Author: ![gdkid1412](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gdkid1412](https://qa.fmod.com/u/gdkid1412)
#### Post date: [August 9, 2013, 3:23am UTC](https://qa.fmod.com/t/loadbank-and-resource-package-files/10980/5 "2013-08-09T03:23:35Z")

</div>

This problem is bothering me too,Is there anybody found a way to solve the problem?

---

<div class="post-metadata">

### Author: ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)
#### Post date: [August 13, 2013, 12:22am UTC](https://qa.fmod.com/t/loadbank-and-resource-package-files/10980/6 "2013-08-13T00:22:32Z")

</div>

Have you tried overriding the low level file system with System::setFileSystem?

---

<div class="post-metadata">

### Author: ![gdkid1412](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gdkid1412](https://qa.fmod.com/u/gdkid1412)
#### Post date: [August 14, 2013, 1:05am UTC](https://qa.fmod.com/t/loadbank-and-resource-package-files/10980/7 "2013-08-14T01:05:15Z")

</div>

System::setFileSystem is useful for ordinary file such as wav, but is not work for bank file.  
I have received an email from your company,tell me that in the 1.2 version will have this function.  
Thanks anyway.

---

<div class="post-metadata">

### Author: ![Guest](https://avatars.discourse-cdn.com/v4/letter/g/8dc957/32.png) [@Guest](https://qa.fmod.com/u/Guest)
#### Post date: [August 22, 2013, 6:13am UTC](https://qa.fmod.com/t/loadbank-and-resource-package-files/10980/8 "2013-08-22T06:13:12Z")

</div>

This use case is supported in Studio API 1.2, which should be released soon. The System::loadBank function has been renamed to loadBankFile, and two new functions have been added: loadBankMemory and loadBankCustom.

The loadBankMemory function reads the bank from a memory buffer. You can specify FMOD\_STUDIO\_LOAD\_MEMORY to make FMOD copy the buffer internally, or FMOD\_STUDIO\_LOAD\_MEMORY\_POINT to make FMOD just point to your buffer (in which case you need to keep the buffer around as long as the bank is loaded so that sample data can be read from it).

The loadBankCustom function allows you to specify a set of file callbacks for loading the bank, and a userdata pointer that will be passed to those file callbacks (for example, this could be a package entry descriptor, a path string, or a file descriptor). This gives you extra control over the loading process, and is a good alternative to loadBankMemory if you want to avoid the cost of reading the whole bank file into memory at once.

---

<div class="post-metadata">

### Author: ![salvofid](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@salvofid](https://qa.fmod.com/u/salvofid)
#### Post date: [November 12, 2013, 10:43am UTC](https://qa.fmod.com/t/loadbank-and-resource-package-files/10980/9 "2013-11-12T10:43:05Z")

</div>

Thanks for the “loadBankMemory” :-), but how I could handle stream banks?  
I mean, I have a bank with a lot of music inside (stream activated in FMod Studio) and if I use “loadBankFile” I will see that allocations are small. The problem arise if I use the loadBankMemory, in this case I have to read the entire file (and using FMOD\_STUDIO\_LOAD\_MEMORY, consequently, Fmod will do another “big” allocation). Any method to avoid this?  
Bye, Salvo

---

<div class="post-metadata">

### Author: ![Guest](https://avatars.discourse-cdn.com/v4/letter/g/8dc957/32.png) [@Guest](https://qa.fmod.com/u/Guest)
#### Post date: [November 12, 2013, 11:04pm UTC](https://qa.fmod.com/t/loadbank-and-resource-package-files/10980/10 "2013-11-12T23:04:33Z")

</div>

> [@salvofid](#):
>
> Thanks for the “loadBankMemory” :-), but how I could handle stream banks?  
> I mean, I have a bank with a lot of music inside (stream activated in FMod Studio) and if I use “loadBankFile” I will see that allocations are small. The problem arise if I use the loadBankMemory, in this case I have to read the entire file (and using FMOD\_STUDIO\_LOAD\_MEMORY, consequently, Fmod will do another “big” allocation). Any method to avoid this?

This is what loadBankCustom is designed for. You can specify file open, close, read and seek callbacks for your bank by setting them in the FMOD\_STUDIO\_BANK\_INFO struct, and have them read from inside your package file (you can store information about which package file to read from in FMOD\_STUDIO\_BANK\_INFO.userData). FMOD will use your callbacks to read the event metadata from the bank. When the time comes to load the streaming music, FMOD will once again use your callbacks to read the audio sample data.

---

<div class="post-metadata">

### Author: ![Guest](https://avatars.discourse-cdn.com/v4/letter/g/8dc957/32.png) [@Guest](https://qa.fmod.com/u/Guest)
#### Post date: [November 12, 2013, 11:43pm UTC](https://qa.fmod.com/t/loadbank-and-resource-package-files/10980/11 "2013-11-12T23:43:49Z")

</div>

Here’s a code sample demonstrating the usage of loadBankCustom. Please note that this is completely untested code with very little error checking, so use at your own risk!

```auto
// structure describing a chunk inside a package file
struct PackageEntry
{
    const char* packageFilePath;
    size_t offset;
    size_t size;
};

// callbacks for reading from within a package file
FMOD_RESULT F_CALLBACK fileOpen(const char *name, int unicode,
    unsigned int *filesize, void **handle, void *userdata)
{
    const PackageEntry* entry = reinterpret_cast<const PackageEntry*>(userdata);

    FILE* file = fopen(entry->packageFilePath, "rb");
    if (!file)
    {
        return FMOD_ERR_FILE_NOTFOUND;
    }

    fseek(file, entry->offset, SEEK_SET);

    *handle = file;
    *filesize = entry->size;

    return FMOD_OK;
}

FMOD_RESULT F_CALLBACK fileClose(void *handle, void *userdata)
{
    FILE* file = reinterpret_cast<FILE*>(handle);

    fclose(file);

    return FMOD_OK;
}

FMOD_RESULT F_CALLBACK fileRead(void *handle, void *buffer,
    unsigned int sizebytes, unsigned int *bytesread, void *userdata)
{
    const PackageEntry* entry = reinterpret_cast<const PackageEntry*>(userdata);

    FILE* file = reinterpret_cast<FILE*>(handle);

    const size_t seekPosition = ftell(file) - entry->offset;
    const size_t bytesRemaining = entry->size - seekPosition;

    size_t clampedSize = sizebytes;

    if (clampedSize > bytesRemaining)
    {
        // avoid reading past the end of the package entry
        clampedSize = bytesRemaining;
    }

    size_t readCount = fread(buffer, 1, clampedSize, file);

    *bytesread = readCount;

    if (readCount < sizebytes)
    {
        // FMOD requested more bytes than we could provide
        return FMOD_ERR_FILE_EOF;
    }

    return FMOD_OK;
}

FMOD_RESULT F_CALLBACK fileSeek(void *handle, unsigned int pos, void *userdata)
{
    const PackageEntry* entry = reinterpret_cast<const PackageEntry*>(userdata);

    if (pos > entry->size)
    {
        // avoid seeking past the end of the package entry
        return FMOD_ERR_FILE_EOF;
    }

    FILE* file = reinterpret_cast<FILE*>(handle);

    fseek(file, pos + entry->offset, SEEK_SET);

    return FMOD_OK;
}

/* ... */

    // loading a bank from within a package file
    PackageEntry packageEntry;
    packageEntry.packageFilePath = "main.pak";
    packageEntry.offset = 54135;
    packageEntry.size = 624264;

    FMOD_STUDIO_BANK_INFO bankInfo;
    memset(&bankInfo, 0, sizeof(bankInfo));
    bankInfo.size = sizeof(bankInfo);
    bankInfo.userData = &packageEntry;
    // make FMOD copy packageEntry internally, rather than pointing to a stack variable
    bankInfo.userDataLength = sizeof(packageEntry);
    bankInfo.openCallback = fileOpen;
    bankInfo.closeCallback = fileClose;
    bankInfo.readCallback = fileRead;
    bankInfo.seekCallback = fileSeek;
    
    FMOD::Studio::Bank bank;
    ERRCHECK( system.loadBankCustom(&bankInfo, &bank) );
```

---

<div class="post-metadata">

### Author: ![salvofid](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@salvofid](https://qa.fmod.com/u/salvofid)
#### Post date: [November 13, 2013, 5:57pm UTC](https://qa.fmod.com/t/loadbank-and-resource-package-files/10980/12 "2013-11-13T17:57:08Z")

</div>

Great :-)!  
It is working (I am loading only a “streaming bank”), but, with the same bank, I have more openCallback than closeCallback…how it is possible?  
Thanks, Salvo.

---

<div class="post-metadata">

### Author: ![Guest](https://avatars.discourse-cdn.com/v4/letter/g/8dc957/32.png) [@Guest](https://qa.fmod.com/u/Guest)
#### Post date: [November 14, 2013, 3:48am UTC](https://qa.fmod.com/t/loadbank-and-resource-package-files/10980/13 "2013-11-14T03:48:50Z")

</div>

> [@salvofid](#):
>
> It is working (I am loading only a “streaming bank”), but, with the same bank, I have more openCallback than closeCallback…how it is possible?

Thanks for reporting this. It was actually a bug where the Studio API was creating unused streams during the preloading stage when you create an event instance. You should get matching close callbacks when you release the event instance, as that releases the unused streams. This bug has now been fixed for the next release.
