Implementing FMOD Studio into Ren'Py for All Platforms Community Project

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
AJFLink
Newbie
Posts: 16
Joined: Mon Nov 05, 2018 2:04 am
Contact:

Implementing FMOD Studio into Ren'Py for All Platforms Community Project

#1 Post by AJFLink » Tue Apr 21, 2020 11:28 pm

I have been able to make a working implementation / import of FMOD Studio in order to allow for an audio system that allows for directional (i.e. left, right, forward, backwards, and in-between). Below is the following code (Note: This has been tested via Python 2.7.17 and has not been tested on any version of Python 3). However, before you can use this, you need to download from https://www.fmod.com/download for FMOD Studio API (version 1.10.20. You need to use this version else the python code will not work.). You will need to make an account; however, it is completely free to use.

Note: This has been done with Mac or Windows operating systems in mind. If any Linux user would like to add to this, that would be most welcomed.

You will need to install them. You do not need to install FMOD Studio for this to work. You can install only the API for your system. However, you will need FMOD Studio 1.10.20 in order to create bank files.

For your game, add a folder called python_plugins

Windows (on Windows 10):

Add all files in C:\Program Files(x86)\FMOD SoundSystem\FMOD Studio API Windows\api\lowlevel\lib to python_plugins
Add all files in C:\Program Files(x86)\FMOD SoundSystem\FMOD Studio API Windows\api\studio\lib to python_plugins
Add all files in C:\Program Files(x86)\FMOD SoundSystem\FMOD Studio API Windows\api\studio\examples\media to python_plugins

Mac:

Open DMG file that you downloaded and place the folder in a desired location, but not in your game's directory.

Add all files in FMOD Programmers API/api/lowlevel/lib
Add all files in FMOD Programmers API/studio/lib
Add all files in FMOD Programmers API/studio/examples/media

Create the following python file and have it saved in python_plugins and save it as FMOD_Studio.py or write it into a python block:

Code: Select all

import os
import platform
from sys import path
from ctypes import *

for n in range(0,len(path)):
	if path[n].find(<Name of your game>) != -1:
		if platform.system() == "Windows":
			new_dir = path[n] + "\game\python_plugins"
			if new_dir in path:
				break
		elif platform.system() == "Darwin":
			new_dir = path[n] + "/game/python_plugins"
		else:
			raise NameError("Game directory/folder could not be found.")
		path.append(new_dir)
		break

os.chdir(path[-1])

PLATFORM_SUFFIX = '64' if sizeof(c_voidp) == 8 else ""
VERSION = 0x00011020
BANK_FILES = ["Master Bank.bank","Master Bank.strings.bank","SFX.bank"]

studio_dll = None
studio_sys = None

def check_result(r):
    if r != 0:
        print("ERROR: Got FMOD_Result {0}".format(r))

def studio_init():
    print("Initializing FMOD Studio");
    global studio_dll
    global studio_sys
    # Windows
    if platform.system() == "Windows":
        studio_dll = WinDLL("fmodstudioL" + PLATFORM_SUFFIX)
        lowlevel_dll = WinDLL("fmodL" + PLATFORM_SUFFIX)
    # MacOS
    else:
        lowlevel_dll = cdll.LoadLibrary("libfmodL.dylib")
        studio_dll = cdll.LoadLibrary("libfmodstudioL.dylib")
    check_result(lowlevel_dll.FMOD_Debug_Initialize(0x00000002, 1, 2, "log.txt".encode('ascii')))
    studio_sys = c_voidp()
    check_result(studio_dll.FMOD_Studio_System_Create(byref(studio_sys), VERSION))
    check_result(studio_dll.FMOD_Studio_System_Initialize(studio_sys,256,0,0,c_voidp()))
    for bankname in BANK_FILES:
        print("Loading bank: " + bankname)
        bank = c_voidp()
        check_result(studio_dll.FMOD_Studio_System_LoadBankFile(studio_sys, bankname.encode('ascii'), 0, byref(bank)))

def play_sound(soundname):
    event_desc = c_voidp()
    check_result(studio_dll.FMOD_Studio_System_GetEvent(studio_sys, soundname.encode('ascii'), byref(event_desc)))
    event_inst = c_voidp()
    check_result(studio_dll.FMOD_Studio_EventDescription_CreateInstance(event_desc, byref(event_inst)))
    check_result(studio_dll.FMOD_Studio_EventInstance_SetVolume(event_inst, c_float(0.75)))
    check_result(studio_dll.FMOD_Studio_EventInstance_Start(event_inst))
    check_result(studio_dll.FMOD_Studio_EventInstance_Release(event_inst))
    check_result(studio_dll.FMOD_Studio_System_Update(studio_sys))

studio_init()
To test this code, create the following in a python block in Ren'Py or using something such as Python IDE add use the following:

Running FMOD_Studio.py in Python 2.7 IDE:

Code: Select all

play_sound("event:/Weapons/Explosion")
Ren'Py:

Code: Select all

python:
	from sys import path
	import platform
	import os
	for n in range(0,len(path)):
	if path[n].find(<Name of your game>) != -1:
		if platform.system() == "Windows":
			new_dir = path[n] + "\game\python_plugins"
			if new_dir in path:
				break
		elif platform.system() == "Darwin":
			new_dir = path[n] + "/game/python_plugins"
		else:
			raise NameError("Game directory/folder could not be found.")
		path.append(new_dir)
		break

os.chdir(path[-1])

import FMOD_Studio
Windows Problem

This works completely fine on Mac but fails on Windows when ran in Ren'Py in a python block or attempting to import FMOD_Studio.py as a module. I have gotten it to work on Windows before

The following trackback occurs in Ren'Py:
FMOD_Studio.py:
studio_dll = WinDLL("fmodstudioL" + PLATFORM_SUFFIX)
File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/ctypes/__init__.py", line 443, in LoadLibrary
File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/ctypes/__init__.py", line 365, in __init__
WindowsError:[Error 126] The specified module could not be found

This error is not version dependent. I am using 6.99 for my game due to it being better optimized for that version than more recent iterations of Ren'Py. And attempting to run version 6.99 or 7.5 of Ren'Py always results in this error.

I would like to be able to release Python code that anyone in the Ren'Py community can use this in any Ren'Py project for any platform and/or have versions specified for one platform (e.g. Mac, Windows, or Linux).

If you want me to provide a test version of this for you to run for yourself, I will be more than happy to provide that.

User avatar
Andredron
Veteran
Posts: 446
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: Implementing FMOD Studio into Ren'Py for All Platforms Community Project

#2 Post by Andredron » Wed Apr 22, 2020 5:10 pm

You can publish a link to download the finished project
Or a link to the github so that other users, if they want, can join.

I would be very grateful if you had an example of a video from YouTube that this module does. Not necessarily made in renpу, аt least even in unity. As I understand it, it is also used there. Thank you in advance.

info fmod studio:
The list of supported platforms is pretty good: Windows, Linux, Macintosh, GameCube, PS2 and XBox! From programming languages, FMOD supports: C ++, C, Delphi and VB.

Of the shortcomings, I would like to emphasize: procedural programming https://en.wikipedia.org/wiki/Procedural_programming and unsupported Java.
Last edited by Andredron on Wed Apr 22, 2020 5:57 pm, edited 2 times in total.
I know, I'm writing terribly in English.

I'm writing a Renpy textbook (in Russian). https://disk.yandex.ru/i/httNEajU7iFWHA Update 22.06.18

Honest Critique

AJFLink
Newbie
Posts: 16
Joined: Mon Nov 05, 2018 2:04 am
Contact:

Re: Implementing FMOD Studio into Ren'Py for All Platforms Community Project

#3 Post by AJFLink » Wed Apr 22, 2020 5:39 pm

Andredron wrote:
Wed Apr 22, 2020 5:10 pm
Here, many people at the level copied stupidly the code> stupidly inserted the code. It would be much easier for users to download an example project else githab. As I understand it, this module allows you to create a direction for the speakers, type of where the sound comes from, I understand correctly?

You can show the simplest example or video, even let it do what it does on a unity game
Yes, this would allow you to create directions (i.e. creating an auditory illusion of 3D binaural sound, especially for people using headphones or similar devices), assuming that you have the FMOD Studio bank files created for the sounds that you need and have written down each event in each sound bank since reopening bank files individually in FMOD Studio is not a feature for who-knows-why. This supposedly saves memory for audio files (but have yet to confirm that yet through testing).

To be fair, this is the closest thing that we are going to get as python port of FMOD Studio as the developers for FMOD Studio seem to have something against Python as they refuse to actually provide code to do it in python. It works but it makes little to no sense, especially on Windows considering that it works fine on Mac version of Ren'Py but on Windows version of Ren'Py you cannot simply set the path to the files and rune the Python file. I am still trying to figure out why the Windows version is so picky about how it is done. Then, again, I have a basic understanding of how the code from FMOD Studio works in Python.

I will work on making an example Ren'Py project utilizing this with all required files in place. However, a Mac version will be provided before the Windows version due to me having not figured out how to get it working on Ren'Py for Windows (even though it works in Python 2.7.17 IDE). I will make sure to include comments in the script files as well as a ReadMe.txt file to let people know what to look at.

I agree the code is barbarically written and appalling to look at. Hopefully someone who knows how ctypes and FMOD Studio works (i.e. obviously not me) can help clean it up and/or explain what everything does.

AJFLink
Newbie
Posts: 16
Joined: Mon Nov 05, 2018 2:04 am
Contact:

Re: Implementing FMOD Studio into Ren'Py for All Platforms Community Project

#4 Post by AJFLink » Thu May 07, 2020 1:31 pm

Support has been given to Windows operating systems as well as Macs. Linux implementation still needs to be made. This currently only works on Ren'Py 6.99. Testing on newest version of Ren'Py 7.3.5 resulted in an exception being flagged for some reason.
Attachments
FMOD Studio Example.zip
Free to use and/or do whatever you want with it.
(37.26 MiB) Downloaded 14 times

AJFLink
Newbie
Posts: 16
Joined: Mon Nov 05, 2018 2:04 am
Contact:

Re: Implementing FMOD Studio into Ren'Py for All Platforms Community Project

#5 Post by AJFLink » Thu May 14, 2020 5:16 pm

I should also note that the Example version uses 2.00.08 FMOD instead of 1.10.20.

AJFLink
Newbie
Posts: 16
Joined: Mon Nov 05, 2018 2:04 am
Contact:

Re: Implementing FMOD Studio into Ren'Py for All Platforms Community Project

#6 Post by AJFLink » Fri May 15, 2020 1:50 pm

I am getting this following message when attempting to run Ren'Py 7.3.5 on the Mac.

OSError: dlopen(libfmod.dylib, 6): no suitable image found. Did find:
[]libfmod.dylib: code signature in (libfmod.dylib) not valid for use in process using Library Validation: mapped file has cdhash, completely unsigned? Code has to be at least ad-hoc signed.



When running on versions prior to taking into consideration Apple's 64-bit app only policy on MacOS, FMOD Studio works as it should.


Note: I am still using MacOS Mojave instead of Catalina due to Catalina being extremely anti-developer friendly in terms of upgrade transition for everything pre-existing and I have A LOT of apps that I regularly use that are 32-bit.

Post Reply

Who is online

Users browsing this forum: No registered users