Studio script to export audio sizes

Hey,

I’m struggling with the lack of documentation and more detailed examples.

I’m trying to generate a report of all the Assets in our FMOD Studio project, and exporting some information like original asset size, compressed asset size and channel count.

Does anyone have a script that does this, or atleast parts of it? I can’t find a way to iterate over assets before even getting to sizes.

Thanks in advance,
Janko

Hi!

The simplest way to iterate over all assets is to use either:

var audioFiles = studio.project.model.AudioFile.findInstances();
audioFiles.forEach(function(audioFile){
    // your code here
});

or:

studio.project.workspace.masterAssetFolder.assets.forEach(function(asset) {
    if (asset.isOfExactType("AudioFile")) {
        // your code here
    }
});

Here’s a script that iterates over all AudioFile objects in your project and prints some basic info on each of them:

studio.menu.addMenuItem({
    name: "Generate Assets Report",
    execute: function() {
        writeLenToFile("AssetsReport");
    }
});

var tab = "\t";
var tabNum = 0;

function writeLenToFile(objectType){

    // Get project path and custom build directory if one is set, and define txt file output path
    var projectPath = studio.project.filePath;
    var projectName = projectPath.substr(projectPath.lastIndexOf("/") + 1, projectPath.length);
    if(typeof studio.project.workspace.builtBanksOutputDirectory == 'undefined'){
        outputPath = projectPath.substr(0, projectPath.lastIndexOf("/") + 1) + objectType + ".txt";
    } else {
        outputPath = studio.project.workspace.builtBanksOutputDirectory + objectType+ ".txt";
    }
    
    // Open txt file at output path, returning early if file cannot be opened
    var textFile = studio.system.getFile(outputPath);
    if (!textFile.open(studio.system.openMode.WriteOnly)) {    
        alert("Failed to open file {0}\n\nCheck the file is not read-only.".format(outputPath));
        console.error("Failed to open file {0}.".format(outputPath));
        return;
    }

    // Write description of info contained in file
    textFile.writeText(objectType + " for '" + projectName +"'\r\n\r\n");

    IterateAudioFiles(textFile);
    
    textFile.close();
    console.log("File containing " + objectType + " successfully created at: " + outputPath);
}

function IterateAudioFiles(textFile){
    var audioFiles = studio.project.model.AudioFile.findInstances();
    audioFiles.forEach(function(audioFile){

        // Write asset path
        textFile.writeText(
            RepeatTab(tabNum) + audioFile.getAssetPath() + "\r\n"
        );

        tabNum++;
        
        // Write asset size
        textFile.writeText(
            RepeatTab(tabNum) + "Asset size:    " + (audioFile.frequencyInKHz * audioFile.channelCount * audioFile.length) + "\r\n"
        );

        // Write channel count
        textFile.writeText(
            RepeatTab(tabNum) + "Channels:      " + audioFile.channelCount + "\r\n"
        );

        tabNum--;
    });
}

function RepeatTab(n){
    var str = "";
    var i = 0;
    while (i < n){
        str += tab;
        i++
    }
        
    return str;
}

You can get additional info on AudioFile’s members using studio.project.model.AudioFile.document(), or by selecting an asset in the Assets tab and running studio.window.browserCurrent().dump().

That said, unfortunately it currently isn’t possible to retrieve the compressed size of assets via the scripting API. I’ve noted your interest in this feature on our feature tracker.

Thanks for the detailed script.

Later I found the dump() function to atleast get an insight into whats available. Through the getAbsoluteAssetPath I can get the original asset, but the compressed version is still elusive.

Is the “./FMODStudio.cache\fsbcache\Mobile” perhaps something that I could map an asset to? The sizes seem to be smaller than expected, perhaps the compressed cached version of assets?

Not sure how to map an audiofile to names of files in the cache.

Thanks

I found a workaround, it’s very hackish. If I clear the cache and write a script that iterates over all file and focuses them, Fmod Studio will generate a cache file. I can then rescan the whole folder to find which file was created and I know the mapping.

Please still consider adding this function, as it’s quite critical for any game developer to find the most problematic assets from compression side. If it’s not easy to add the API, then make a workaround so we can get the cached filename, or if that is not possible, just add an export option that generates the a csv report for all assets. There’s a few fallbacks you can consider, that won’t require waiting for the next major release of FMOD.

Thanks