Jump to content

Use fmod to play sounds at volume


icuurd12b42
 Share

Recommended Posts

After looking far and wide on the site for playing sounds at volume on all platforms, I figured they is not solution but to take control of the sound system as you would in a game.... Not to rely on the OS GUI to setup the volume as in the app volume in WIN:8, but in the application itself, like most games on Windows.. I decided to use my fmod experience for overcoming the SoundPlay and SoundSetWaveVolume limitation in AutoIT.

As you may have found out loooking in the docs, SoundSetWaveVolume sets the volume of the wav mixer in older window's OS. it means you change the wav out (for every app) to the volume specified. In short, you screwed up the user's settings. So the user will be wondering why his app or game sound is lower than usuall because your app screwed with his setttings... ,confusion.

The best thing to do is to control the volume of each sound your app plays. I have writen a fmod wrapper for gamemaker so I figure I would insight you into using fmod for this type of need.

You need to make a fmod wrapper dll and you need fmod. fmod plays all types of sounds on all windows platforms. And the licensing is interesting if you play your cards right...

Basic dumb ass setup for AutoIT and FMOD:

FMOD:

get FMOD at http://fmod.org/fmod-downloads.html

download the API and install it.

Copy the fmod dll in your autoit project...

AutoIT code

func myplaysound($filename, $vol)
;filename is the full path and name.ext
;vol is a 0 to 1 value
return DllCall(@ScriptDir & "\myFMODwrap.DLL", "int:cdecl", "myplaysound", "str", $filename, "float",$vol)
EndFunc

create a wrapper dll myFMODwrap with code::blocks or ms dev studio with this file: new dll project: myFMODwrap

main.cpp

code.

#include "main.h"
#include <windows.h>
#include <fmod.h>
#include <fmod_errors.h>
#include <string>
#include <stdio.h>
#define MYEXPORT __declspec (dllexport)


int ERRCHECK(FMOD_RESULT result)
{
if (result != FMOD_OK)
{
     MessageBoxA(NULL, FMOD_ErrorString(result), MB_IConerror);
     return 0;

}
return 1;
}
extern "C"
{

// a sample exported function
DLL_EXPORT int myplaysound(LPCSTR filename, float vol)
{

FMOD_SYSTEM  *system;
FMOD_SOUND   *sound1;
FMOD_CHANNEL     *channel = 0;
FMOD_RESULT  result;
unsigned int     version;
int          playing = 0;
//MessageBoxA(NULL,"Debug", "1", MB_IConerror);
//char *filename = strtok((lpszArgument),",\"");
//MessageBoxA(NULL, filename,"myPlaySound", MB_IConerror);
// std::string msg = "Hello";
// char msg2[20];
// float my_float = 10.0f;
// sprintf((char*)msg2, "%f", vol);
// msg = msg + msg2;// + ftoa(my_float);
// MessageBox(NULL,"In the dll", msg2,0);
result = FMOD_System_Create(&system);
//MessageBoxA(NULL,"Play Called", "FMOD_System_Create", MB_IConerror);
if(ERRCHECK(result))
{
     result = FMOD_System_GetVersion(system, &version);
     if(ERRCHECK(result))
     {
         if (version == FMOD_VERSION)
         {
             result = FMOD_System_Init(system, 32, FMOD_INIT_NORMAL, NULL);
             //MessageBoxA(NULL,"gpPlaySound - by Gilles Page", "FMOD_System_Init", MB_IConerror);
             if(ERRCHECK(result))
             {
                 result = FMOD_System_CreateSound(system, filename, FMOD_HARDWARE | FMOD_LOOP_OFF, 0, &sound1);
                 //MessageBoxA(NULL,"gpPlaySound - by Gilles Page", "FMOD_System_CreateSound", MB_IConerror);
                 if(ERRCHECK(result))
                 {
                     result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound1, 0, &channel);
                     //MessageBoxA(NULL,"gpPlaySound - by Gilles Page", "FMOD_System_PlaySound", MB_IConerror);
                     if(ERRCHECK(result))
                     {
                         result = FMOD_Channel_SetVolume(channel,vol);
                         if(ERRCHECK(result))
                         {
/// sprintf((char*)msg2, "%f", vol);
/// msg = msg + msg2;// + ftoa(my_float);
/// MessageBox(NULL,"In the playing", msg2,0);
                             do
                             {
                                 result = FMOD_Channel_IsPlaying(channel, &playing);
                                 FMOD_System_Update(system);

                                 Sleep(10);

                             } while (playing);
                         }
                     }
                     FMOD_Sound_Release(sound1);

                 }
                 result = FMOD_System_Close(system);
             }
         }
     }
     FMOD_System_Release(system);
}
return 1;

}
} //EXTERN "C"
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
     case DLL_PROCESS_ATTACH:
         // attach to process
         // return FALSE to fail DLL load
         break;

     case DLL_PROCESS_DETACH:
         // detach from process
         break;

     case DLL_THREAD_ATTACH:
         // attach to thread
         break;

     case DLL_THREAD_DETACH:
         // detach from thread
         break;
}
return TRUE; // succesful
}

1) create a new dll project with ms dev studio or code::blocks

2) replace the generated main code with the suplied code

3) add fmod 's inc folder to your additional include in ms dev or code;;blocks

4) add fmodexp_vc.lib (ms studio) or fmodex.a (code::blocks) to your additional lib options

5) compile and copy the dll to your au3 directory, and also copy fmodex.dll to the same location

I tried to acess fmod directly from the autoit script but it had trouble with the double ref **system. so a wrapper did the job for me. I have a method to play the sound, though locking the GUI... you can add the fmod update call to your api and call it from your main GUI loop for asynchronus playing... or in a timed function call... FMODUptate()....

Edited by icuurd12b42
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...