Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Free FMOD .NET Plugin - Indie Pro

Discussion in 'General Discussion' started by johnny tangle, Aug 16, 2009.

  1. johnny tangle

    johnny tangle

    Joined:
    Aug 14, 2009
    Posts:
    15
    Hi all,
    SquareTangle has released FMOD for Unity a free .NET Plugin to use FMOD (sound system) with Unity. It works with Indie and Pro, Mac and Windows version 2.5.

    This is a .NET Assembly so will work on Indie as well as Pro versions of Unity.
    FMODUnity works with both the low level Ex and Event systems. (for us anyway :) )
    You can download it from
    http://www.squaretangle.com/FMODUnity.html

    There's a demo unity project, library binaries, as well as source code if anyone is interested in how to make a c# .NET plugin using cross platform libraries.

    It is not heavily tested yet, let us know how it works for you.
    see the demos for basic usage.

    enjoy
    johnny tangle

    edited to avoid confusion regarding the 'plugin' word. This is a .NET assembly.
     
  2. Murcho

    Murcho

    Joined:
    Mar 2, 2009
    Posts:
    309
    I'm curious as to why you would use FMOD when there is already a spacial audio system built into the engine.

    Are there any major advantages using FMOD over the in-built Unity system, and if so, could you give us a couple of examples?
     
  3. johnny tangle

    johnny tangle

    Joined:
    Aug 14, 2009
    Posts:
    15
    one of the main features of fmod is the fmod designer front end which allows sound designers to construct and audition their sound designs without programming. the results can then be passed on to the programmers (or designer/programmer) for easier integration into the engine. can make life easier for both designers and programmers. fmod has interfaces for many platforms (computer, console, handheld) making porting the sound between platforms potentially easier.
    Others on the forum with more experience with fmod than me might be able to add their thoughts.
    see also http://www.fmod.org/index.php/products/designer
     
  4. Nic-Cusworth

    Nic-Cusworth

    Joined:
    Oct 12, 2008
    Posts:
    218
    Interested in this. Just wondering what license is required? Is it the $500 iPhone license? If so I guess it's going to need some pretty good justification over the Unity inbuilt.
     
  5. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    551
    This wouldn't work on the iPhone. Plugins are only available on standalone Mac and PC platforms at the moment.

    You would need to get a FMOD license for distributing your product depending on its size. FMOD license pricing is here for commercial products. For non-commercial products FMOD is free to use.
     
  6. Nic-Cusworth

    Nic-Cusworth

    Joined:
    Oct 12, 2008
    Posts:
    218
    Think I'm getting confused because I was playing 'Mevo' at the weekend which I believe is built in Unity and also had the FMOD logo.

    I know the audio side of the iPhone engine could do with being improved so it's a shame it's not easily compatible.
     
  7. Murcho

    Murcho

    Joined:
    Mar 2, 2009
    Posts:
    309
    Thanks for the info. I was aware of FMOD, and that it is used a fair bit in the games industry. I met some of the guys that work on it a couple of years back at GCAP and the product looked really impressive.

    I guess what it comes down to is the money you need to spend on extra licencing going to reduce costs in other areas (in this case sound design), and seeing as we don't have any in house sound people yet, for us the in-built system will suffice. I will keep an eye on this though, as needs may change in the future.
     
  8. johnny tangle

    johnny tangle

    Joined:
    Aug 14, 2009
    Posts:
    15
    There is some details of fmod for iphone here
    http://www.fmod.org/index.php/fmod/more/28
    Currently $500 per title. I think that is what moondog was referring to.
    I don't have iphone for unity so can't comment on usage.
    The plugin is a .NET assembly. can iphone use .NET plugins?
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,603
    And naturally the primary trouble area, support, as OpenAL is a piece of *****.
    As free it is, as crappy it is. UT likely has invested masses of time and cash in fixing up OpenAL to what they are using, because its definitely not the regular free release, thats for sure (their version worked since 2.0 in 08 on Vista64, while the official release needed till june 2009 to finally stop locking on startup and alike)

    I think if Unity finally replaces OpenAL with FMOD, its a win win for all
     
  10. lankoski

    lankoski

    Joined:
    Apr 20, 2008
    Posts:
    148
    FMOD is good for creating sounds that reacts to game state, for example, car's engine sound reacting players accelerating. Some examples of FMOD use can be found from our Lies and Seductions (game available at www.liesandseductions.com and source code available at http://mlab.taik.fi/~plankosk/blog/?p=308).

    In addition, you can use FMOD to model spatial sounds such as echos based on the space model (we have not tried this, though).
     
  11. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    You're using terminology that's confusing to Unity folks. :) "Plugins" in the Unity world are C/C++ libraries you link to that are only supported in Unity Pro and are only supported on the desktop (Mac/Win). Compare that with .NET assemblies that are supported in both Unity Indie and Unity Pro and on the desktop and the web. .NET assemblies are only partially/somewhat supported on the iPhone (in 1.1 things are improving there, notably if you're using custom Objective-C code).
     
  12. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,603
    The main restriction with .NET assemblies on the iphone is .NET 1.1 and the maximum size of libraries to use.
    You won't find many if any .NET assemblies that only require .NET 1.1, due to the simple fact that generics are a common and powerfull thing but require .NET 2.0 and thus are Unity iPhone incompatible.

    The other thing is that with iPhone Basic, you don't stand a real chance to be able to use them due to the total size of the libraries. You would require iPhone Advanced and be able to reduce the size enough through stripping to use it.
     
  13. johnny tangle

    johnny tangle

    Joined:
    Aug 14, 2009
    Posts:
    15
    thanks tom. my bad.
    I was naming according to the Extensions page on the unify wiki which calls both .NET assemblies and C/C++ dll's Plugins. ie. .NET plugin or Native Plugin.
    Is .NET Plugin less confusing or .NET assembly better? I thought assembly might also be confusing.

    anywho, we went the .NET assemby route specifically so it could be used in indie and pro on mac and win.

    @dreamora. thanks for the info. sounds like it may be possible to get something working on iPhone. with some difficulty. you've piqued my interest, i may have to download the iPhone SDK and have a look.
     
  14. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,157
    Personally, I think .NET assembly is the best term. While these are "dlls" according to their file extensions, they don't have much in common with Win32 DLLs. So I'd call those either Mono assemblies or .NET assemblies (some people using Unity might not know that Mono more or less equals .NET, so I guess Mono assemblies might be easiest to understand for most, even if it may not be perfectly technically accurate). Also, I wouldn't call them "plugin" because usually a plugin is something that plugs into some sort of plugin-framework, which is different from assemblies which basically are just libraries offering functionalities through an API.

    That said: I doubt that FMOD can be fully made to work in a Mono/.NET-only assembly. Correct me if I'm wrong but I'd believe that FMOD requires some native code and the Mono assembly would just be a wrapper for that. In that case, it won't work in Unity iPhone. Also, in that case it wouldn't work in the Web player. But I'll be happy to be corrected if I'm wrong.

    Sunny regards,
    Jashan
     
  15. johnny tangle

    johnny tangle

    Joined:
    Aug 14, 2009
    Posts:
    15
    good point jashan. seems like web player and iphone don't handle plugins or linking to external libraries. maybe soon on the iphone for unity.
    At the moment FMODUnity works well for our productions which are all based on the desktop unity environment which gives us plenty of scope to work with. Hopefully it will provide some others with enhanced audio potential as well. If anyone tries it out we'd really appreciate any feedback.
     
  16. bliprob

    bliprob

    Joined:
    May 13, 2007
    Posts:
    901
    FMOD could be integrated into Unity iPhone using Unity 1.1's native function calls (you'd probably want to make a wrapper layer). I downloaded the FMOD for iPhone SDK and it looks pretty straightforward.
     
  17. magwo

    magwo

    Joined:
    May 20, 2009
    Posts:
    402
    I'm a bit confused.. is this a pure .Net assembly or is it just a .Net wrapper/abstractor around the FMod C++ library?

    How does the .Net assembly interface with the platform's audio system?

    What I really want to know is whether it's usable in web player or not.
     
  18. johnny tangle

    johnny tangle

    Joined:
    Aug 14, 2009
    Posts:
    15
    this assemly still has to use the FMOD libraries so probably won't work with the web player anytime soon.
     
  19. Almar

    Almar

    Joined:
    May 11, 2009
    Posts:
    87
    You can do some pretty fun things with FMod, like getting the spectrum, and trying some beat detection on that. I have been playing a few days with it, also to see if I could get polymorphism to work properly in Unity.

    I will probably release it soon anyway, since I do not have a lot of time to spent on it. just a small hobby project next to all that serious game programming at work ;).

    I would love to see some visualizations created by other people though. Spectrum data is a good source to create nice effects. I hope to be able to make a mesh 'dance' on the music, but I'm not sure I can pull it off time wise.



     
  20. Nic-Cusworth

    Nic-Cusworth

    Joined:
    Oct 12, 2008
    Posts:
    218
    @Almar That looks great. Nice to see that you can get so much data back out of FMod. Love Unity but the audio side is very very weak.

    @bliprod Almost certainly going to buy the next update to the enhancement pack. FMod support would make it even more essential. Think I'd finally make a music game with that combination ;)
     
  21. speigg

    speigg

    Joined:
    Jan 17, 2009
    Posts:
    6
    When I try to build the demo project with Unity Indie, it does not work. I looked inside the application bundle, and the fmod library files are not included. I also tried placing the library files in the Frameworks folder inside the application bundle, but it still does not work. The scene works fine when run from inside Unity, but not when it is built to a standalone application. Does the plugin just not work in a standalone application built with Unity Indie or am I doing something wrong? Has anyone been able to build a working scene that uses this FMod .Net assembly?
     
  22. johnny tangle

    johnny tangle

    Joined:
    Aug 14, 2009
    Posts:
    15
    hi speigg
    the exe or app probably can't find the assets or fmod distributable libraries.
    in the examples the paths to the assets are hard coded in script.
    as a quick test you can try copying the relavent library (fmodex.dll or libfmodex.dylib or the event system equivs)and the Assets folder to the same directory as the standalone executable.
     
  23. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Yes, I've checked it out and you're right - there is a .dylib included in the package as well as the .NET assembly.
     
  24. hogus

    hogus

    Joined:
    Jul 9, 2009
    Posts:
    145
    FMod is a great library. If only Unity could manage some OEM deals with partners like these for audio and networking when using Unity-Pro it would suddenly become a whole lot more attractive to an Indie user like me.
     
  25. polytropoi

    polytropoi

    Joined:
    Aug 16, 2006
    Posts:
    676
    UT announced at the conference that the audio core in Unity 2.6 is now fmod instead of openal, but they haven't yet put to use any of the cool stuff fmod can do (i.e spectrum analysis...), aside from improved audio performance; I'd expect to see some support for that in a pre-3 point release.
     
  26. guitarguy91403

    guitarguy91403

    Joined:
    Oct 22, 2009
    Posts:
    7
    Hi all,

    I've been using the FMOD/Unity plugin and so far so good! Really cool.
    DSP, multi-channel, getSpectrum all work -

    However, I can't get the following to work:

    C++ code, provided in the FMOD examples:
    Code (csharp):
    1. system->createSound(0, FMOD_2D | FMOD_SOFTWARE | FMOD_LOOP_NORMAL | FMOD_OPENUSER, &exinfo, &sound);
    2.  
    The above works fine, when I build run from Mac OS X 10.5.8 XCode.

    However, when I convert the above C++ to C# to be used with Unity, here is the C# code:
    Code (csharp):
    1. system.createSound( 0 , FMOD.MODE._2D | FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL | FMOD.MODE.OPENUSER  , ref exinfo, ref sound);
    2.  
    I get errors:

    Code (csharp):
    1. Argument 1: Cannot convert type `int' to `string'
    2.  
    What is the proper way to create the sound?

    Thank you, maximum respect, GuitarGuy91403
     
  27. theotherstudio13

    theotherstudio13

    Joined:
    Oct 8, 2009
    Posts:
    40
    This is great news for sound designers, I'm eager to contribute my experience with FMOD and sound design on a Unity game project.
     
  28. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    999
    The unity example project event scene doesn't work out of the box here (FMOD_Event_Test):
    The other example *does* work. I didn't move the dll's and they should be at the right folder. Building the event scene does work though:?. So this puzzles me.
    Any clues?


    Thanks for all the work!
     
  29. hajasal_sk

    hajasal_sk

    Joined:
    Jan 5, 2010
    Posts:
    37
    Thanks for this great plugin!

    ...but has anyone been able to get the interactive-music side of FMOD working? We've been trying here without luck.

    Basically everything here runs without error, but no music is heard. (The FEV/FSB work just fine in the FMOD Music Player App).
     
  30. adamtangle

    adamtangle

    Joined:
    Jan 22, 2010
    Posts:
    1
    I had the same problem and also fixed it by giving the Id of the Music Cue as an integer (which can be found in the programmer’s report generated when building the FMOD project) in the argument to prepareCue().

    For reference, here’s how to get the Interactive Music system working (refer to the FMOD_Event_Test script in the FMODUnity demo for usage):

    Initialise it with:
    private FMOD.MusicSystem musicsystem = null;
    private FMOD.MusicPrompt musiccue = null;

    prepare it with:
    result = eventsystem.getMusicSystem(ref musicsystem);
    ERRCHECK(result);

    result = musicsystem.prepareCue(1, ref musiccue); //find the cue number in the programmer’s report generated when building the fmod project
    ERRCHECK(result);

    then play the cue with:
    result = musiccue.begin();
    ERRCHECK(result);

    and stop it with:
    result = musiccue.end();
    ERRCHECK(result);

    Cheers,
    Adam
     
  31. johnny tangle

    johnny tangle

    Joined:
    Aug 14, 2009
    Posts:
    15
    @leepo
    Are you using 2.6.1 on the PC?
    we recently had trouble when we went to update to FMOD2.8 and Unity2.61 on the PC. The projects (FMOD 2.6 and 2.8 work fine in Unity25 but report your error in 2.6. Mac 2.61 version works fine.
    Also you might notice a warning in the FMOD_Ex_Test scene that reports the wrong version of FMOD library.
    I have a suspicion that the fmod running in the 2.6 version is conflicting with the plugin. haven't been able to sort it yet from our end.
    Can't add the fmod_event.dll or change fmodex.dll to another version.
    Will post a solution if one arises
     
  32. johnny tangle

    johnny tangle

    Joined:
    Aug 14, 2009
    Posts:
    15
    Could anyone from UT please offer advice on a workaround for using the fmod event system with our plugin, or a workaround for calling the fmod_event.dll. Currently 2.6 won't allow dropping the .dll into the project folder, ie. it is not picked up and there is no fmod_event.dll in the editor only an older version of fmdoex.dll
    thanks for any help.
     
  33. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    999
    Yep.It's probably a conflict indeed. I did also notice the warning.
     
  34. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Hey Johnny,

    I'll get one of my testers to take a look and work with one of our audio guys to see what we can suggest.

    Thanks,
    Graham
     
  35. johnny tangle

    johnny tangle

    Joined:
    Aug 14, 2009
    Posts:
    15
    Great
    Thanks Graham
     
  36. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,041
    I want to try some real time pitchdetection from mic input, converting the c# fmod example for the fine SquareTangle plugin (thanks guys!).

    [I know Unity 2.6 now uses FMOD as the backend but I gather currently it is only minimal API that is exposesd?)

    Just thought I'd ask before starting if this works or are there any issues I need to be aware of?
    (just so I don't waster a couple of days beating my head against a wall other have already found).


    Thanks in advance!
     
  37. johnny tangle

    johnny tangle

    Joined:
    Aug 14, 2009
    Posts:
    15
    realtime pitch detection from mic input works fine (tested and working on PC unity 2.5). should work the same on mac. Event system on mac works fine.
    If you are on PC 2.6 then there seems to be a compatibility problem with the inbuilt version of FMOD and the event system currently doesn't work. We are hoping to find a solution soon. Should be OK on mac up to 2.6(not fully tested by me) and win up to 2.5

    here is a script that works for me..

    Code (csharp):
    1. /******************************************************************
    2. Example script for using the FMOD Event system.
    3. Initializes the FMOD Event, opens an input device and attempts to detect pitch.
    4. Visit [url]www.squaretangle.com[/url] for updates and more information.
    5. Enjoy.
    6. johnny tangle
    7. ********************************************************************/
    8.  
    9. using UnityEngine;
    10. using System;
    11. using System.Collections;
    12. using System.Text;
    13. using System.Threading;
    14. using System.Runtime.InteropServices;
    15.  
    16. public class PitchDetection : MonoBehaviour {
    17.                 private FMOD.System     system  = null;
    18.         private FMOD.Sound      sound   = null;
    19.         private FMOD.Channel    channel = null;        
    20.         private int             outputfreq = 0;
    21.         private int             bin     = 0;
    22.         private int                         playbackDriverNumber = 0;
    23.         private int                         recordDriverNumber = 0;
    24.    
    25.         private const int   OUTPUTRATE       = 48000;
    26.         private const int   SPECTRUMSIZE     = 8192;
    27.         private const float SPECTRUMRANGE    = ((float)OUTPUTRATE / 2.0f);     /* 0 to nyquist */
    28.         private const float BINSIZE          = (SPECTRUMRANGE / (float)SPECTRUMSIZE);
    29.        
    30.         private string[] note =
    31.         {
    32.             "C 0", "C#0", "D 0", "D#0", "E 0", "F 0", "F#0", "G 0", "G#0", "A 0", "A#0", "B 0",  
    33.             "C 1", "C#1", "D 1", "D#1", "E 1", "F 1", "F#1", "G 1", "G#1", "A 1", "A#1", "B 1",  
    34.             "C 2", "C#2", "D 2", "D#2", "E 2", "F 2", "F#2", "G 2", "G#2", "A 2", "A#2", "B 2",  
    35.             "C 3", "C#3", "D 3", "D#3", "E 3", "F 3", "F#3", "G 3", "G#3", "A 3", "A#3", "B 3",  
    36.             "C 4", "C#4", "D 4", "D#4", "E 4", "F 4", "F#4", "G 4", "G#4", "A 4", "A#4", "B 4",  
    37.             "C 5", "C#5", "D 5", "D#5", "E 5", "F 5", "F#5", "G 5", "G#5", "A 5", "A#5", "B 5",  
    38.             "C 6", "C#6", "D 6", "D#6", "E 6", "F 6", "F#6", "G 6", "G#6", "A 6", "A#6", "B 6",  
    39.             "C 7", "C#7", "D 7", "D#7", "E 7", "F 7", "F#7", "G 7", "G#7", "A 7", "A#7", "B 7",  
    40.             "C 8", "C#8", "D 8", "D#8", "E 8", "F 8", "F#8", "G 8", "G#8", "A 8", "A#8", "B 8",  
    41.             "C 9", "C#9", "D 9", "D#9", "E 9", "F 9", "F#9", "G 9", "G#9", "A 9", "A#9", "B 9"
    42.         };
    43.        
    44.         private float[] notefreq =
    45.         {
    46.             16.35f,   17.32f,   18.35f,   19.45f,    20.60f,    21.83f,    23.12f,    24.50f,    25.96f,    27.50f,    29.14f,    30.87f,
    47.             32.70f,   34.65f,   36.71f,   38.89f,    41.20f,    43.65f,    46.25f,    49.00f,    51.91f,    55.00f,    58.27f,    61.74f,
    48.             65.41f,   69.30f,   73.42f,   77.78f,    82.41f,    87.31f,    92.50f,    98.00f,   103.83f,   110.00f,   116.54f,   123.47f,
    49.             130.81f,  138.59f,  146.83f,  155.56f,   164.81f,   174.61f,   185.00f,   196.00f,   207.65f,   220.00f,   233.08f,   246.94f,
    50.             261.63f,  277.18f,  293.66f,  311.13f,   329.63f,   349.23f,   369.99f,   392.00f,   415.30f,   440.00f,   466.16f,   493.88f,
    51.             523.25f,  554.37f,  587.33f,  622.25f,   659.26f,   698.46f,   739.99f,   783.99f,   830.61f,   880.00f,   932.33f,   987.77f,
    52.             1046.50f, 1108.73f, 1174.66f, 1244.51f,  1318.51f,  1396.91f,  1479.98f,  1567.98f,  1661.22f,  1760.00f,  1864.66f,  1975.53f,
    53.             2093.00f, 2217.46f, 2349.32f, 2489.02f,  2637.02f,  2793.83f,  2959.96f,  3135.96f,  3322.44f,  3520.00f,  3729.31f,  3951.07f,
    54.             4186.01f, 4434.92f, 4698.64f, 4978.03f,  5274.04f,  5587.65f,  5919.91f,  6271.92f,  6644.87f,  7040.00f,  7458.62f,  7902.13f,
    55.             8372.01f, 8869.84f, 9397.27f, 9956.06f, 10548.08f, 11175.30f, 11839.82f, 12543.85f, 13289.75f, 14080.00f, 14917.24f, 15804.26f
    56.         };
    57.  
    58.     // Use this for initialization
    59.     void Start () {
    60.                         int             numdrivers = 0;
    61.             uint            version = 0;
    62.             FMOD.RESULT     result;
    63.             StringBuilder   drivername = new StringBuilder(256);
    64.             FMOD.GUID       guid = new FMOD.GUID();
    65.  
    66.            
    67.             //Create an FMOD System object            
    68.             result = FMOD.Factory.System_Create(ref system);
    69.             ERRCHECK(result);
    70.             //Check FMOD Version
    71.             result = system.getVersion(ref version);
    72.             ERRCHECK(result);
    73.             if (version < FMOD.VERSION.number)
    74.             {
    75.                 Debug.Log("Error!  You are using an old version of FMOD " + version.ToString("X") + ".  This program requires " + FMOD.VERSION.number.ToString("X") + ".");
    76.             }
    77.             /*
    78.                 Get Playback drivers
    79.             */
    80.             result = system.getNumDrivers(ref numdrivers);
    81.             ERRCHECK(result);
    82.  
    83.             for (int count = 0; count < numdrivers; count++)
    84.             {
    85.                 result = system.getDriverInfo(count, drivername, drivername.Capacity, ref guid);
    86.                 ERRCHECK(result);
    87.  
    88.                 Debug.Log("Playback Driver: " + drivername.ToString());
    89.             }
    90.  
    91.             /*
    92.                 Get Record drivers
    93.             */
    94.             result = system.getRecordNumDrivers(ref numdrivers);
    95.             ERRCHECK(result);
    96.  
    97.             for (int count = 0; count < numdrivers; count++)
    98.             {
    99.                 result = system.getRecordDriverInfo(count, drivername, drivername.Capacity, ref guid);
    100.                 ERRCHECK(result);
    101.  
    102.                 Debug.Log("Record Driver: " + drivername.ToString());
    103.             }  
    104.            
    105.             result = system.setOutput(FMOD.OUTPUTTYPE.DSOUND);
    106.             //result = system.setOutput(FMOD.OUTPUTTYPE.WINMM);
    107.             //result = system.setOutput(FMOD.OUTPUTTYPE.ASIO);
    108.             ERRCHECK(result);
    109.            
    110.             result = system.setDriver(playbackDriverNumber);
    111.             ERRCHECK(result);
    112.            
    113.             FMOD.CREATESOUNDEXINFO exinfo = new FMOD.CREATESOUNDEXINFO();
    114.             //FMOD.RESULT result;
    115.             FMOD.DSP_RESAMPLER  resampler = FMOD.DSP_RESAMPLER.MAX;
    116.  
    117.             int temp = 0;
    118.             FMOD.SOUND_FORMAT format = FMOD.SOUND_FORMAT.NONE;
    119.             IntPtr tempptr = new IntPtr();
    120.  
    121.             result = system.setSoftwareFormat(OUTPUTRATE, FMOD.SOUND_FORMAT.PCM16, 1, 0, 0);
    122.             ERRCHECK(result);
    123.  
    124.             result = system.init(32, FMOD.INITFLAG.NORMAL, (IntPtr)null);
    125.             ERRCHECK(result);
    126.  
    127.             result = system.getSoftwareFormat(ref outputfreq, ref format, ref temp, ref temp, ref resampler, ref temp);
    128.             ERRCHECK(result);
    129.  
    130.             /*
    131.                 Create a sound to record to.
    132.             */
    133.             exinfo.cbsize           = Marshal.SizeOf(exinfo);
    134.             exinfo.numchannels      = 1;
    135.             exinfo.format           = FMOD.SOUND_FORMAT.PCM16;
    136.             exinfo.defaultfrequency = OUTPUTRATE;
    137.             exinfo.length           = (uint)(exinfo.defaultfrequency * 2 * exinfo.numchannels * 5);
    138.    
    139.             result = system.createSound((string)null, (FMOD.MODE._2D | FMOD.MODE.SOFTWARE | FMOD.MODE.LOOP_NORMAL | FMOD.MODE.OPENUSER), ref exinfo, ref sound);
    140.             ERRCHECK(result);
    141.  
    142.  
    143.  
    144.             /*
    145.                 Start recording
    146.             */
    147.             result = system.recordStart(recordDriverNumber, sound, true);
    148.             ERRCHECK(result);
    149.    
    150.             Thread.Sleep(200);      /* Give it some time to record something */
    151.    
    152.             result = system.playSound(FMOD.CHANNELINDEX.REUSE, sound, false, ref channel);
    153.             ERRCHECK(result);
    154.  
    155.             /* Dont hear what is being recorded otherwise it will feedback.  Spectrum analysis is done before volume scaling in the DSP chain */
    156.             result = channel.setVolume(0);
    157.             ERRCHECK(result);
    158.    
    159.     }
    160.    
    161.     // Update is called once per frame
    162.     void Update () {
    163.         if (channel != null)
    164.             {
    165.                 FMOD.RESULT result;
    166.                 float[] spectrum = new float[SPECTRUMSIZE];
    167.                 float   dominanthz = 0;
    168.                 float   max;
    169.                 int     dominantnote = 0;
    170.                 int     lastnote = 0;
    171.                 int     count = 0;
    172.  
    173.                 result = channel.getSpectrum(spectrum, SPECTRUMSIZE, 0, FMOD.DSP_FFT_WINDOW.TRIANGLE);
    174.                 ERRCHECK(result);
    175.  
    176.                 max = 0;
    177.  
    178.                 for (count = 0; count < SPECTRUMSIZE; count++)
    179.                 {
    180.                     if (spectrum[count] > 0.01f  spectrum[count] > max)
    181.                     {
    182.                         max = spectrum[count];
    183.                         bin = count;
    184.                     }
    185.                 }        
    186.  
    187.                 dominanthz  = (float)bin * BINSIZE;       /* dominant frequency min */
    188.  
    189.                 dominantnote = 0;
    190.                 for (count = 0; count < 120; count++)
    191.                 {
    192.                     if (dominanthz >= notefreq[count]  dominanthz < notefreq[count + 1])
    193.                     {
    194.                         // which is it closer to.  This note or the next note
    195.                         if (Math.Abs(dominanthz - notefreq[count]) < Math.Abs(dominanthz - notefreq[count+1]))
    196.                         {
    197.                             dominantnote = count;
    198.                         }
    199.                         else
    200.                         {
    201.                             dominantnote = count + 1;
    202.                         }
    203.                         break;
    204.                     }
    205.                 }
    206.                 //lastnote = dominantnote;
    207.                 if(dominantnote != lastnote){
    208.                
    209. //send pitch to Debug console window                
    210. Debug.Log("Detected rate : " + dominanthz + " -> " + (((float)bin + 0.99f) * BINSIZE) + " Detected musical note : " + note[dominantnote] + " (" + notefreq[dominantnote]+")");
    211.                 }
    212.                 lastnote = dominantnote;
    213.             }
    214.  
    215.             if (system != null)
    216.             {
    217.                 system.update();
    218.             }
    219.     }
    220.    
    221.     void OnDisable() {
    222.         //cleanup routine here
    223.                
    224.                 //Shut down
    225.                 FMOD.RESULT     result;
    226.                
    227.                 if (sound != null)
    228.                 {
    229.                     result = sound.release();
    230.                     ERRCHECK(result);
    231.                 }
    232.                
    233.                 if (system != null)
    234.                 {
    235.                     result = system.close();
    236.                     ERRCHECK(result);
    237.                     result = system.release();
    238.                     ERRCHECK(result);
    239.                     Debug.Log("FMOD release! " + result );
    240.                 }
    241.     }
    242.     //FMOD error checking codes
    243.     void ERRCHECK(FMOD.RESULT result)
    244.         {
    245.             if (result != FMOD.RESULT.OK)
    246.             {
    247.                 Debug.Log("FMOD error! " + result + " - " + FMOD.Error.String(result));
    248.                
    249.             }
    250.         }
    251. }
     
  38. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,041
    Thanks Johnny!

    I have both pc/mac but have only tested event under 2.6 under mac so far and it worked ok.
     
  39. johnny tangle

    johnny tangle

    Joined:
    Aug 14, 2009
    Posts:
    15
    great
    may work on PC 26 as well, haven't tested yet
    pitch detection uses fmod ex.
     
  40. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,041
    Pitchdetection as above works fine on 2.6 on mac, just had to comment out
    Code (csharp):
    1.             result = system.setOutput(FMOD.OUTPUTTYPE.DSOUND);
    2.             //result = system.setOutput(FMOD.OUTPUTTYPE.WINMM);
    3.             //result = system.setOutput(FMOD.OUTPUTTYPE.ASIO);
    4.             ERRCHECK(result);
    so it used default outputs on the mac.

    I'll run some more extensive tests later but the detection seemed slightly off. A semitone to a tone out?
     
  41. pegorari

    pegorari

    Joined:
    Nov 19, 2009
    Posts:
    46
    Hello everybody,

    I´m trying to get the spectrum value of sound to make an spectrum wave preview bitmap (like Unity does), but no success. The function getWaveData() is ok, but I can´t get any return from getSpectrum() function.

    Any Idea?

    this is the code i´m trying (a modified version of PlayEx.cs)



    Code (csharp):
    1. /******************************************************************
    2. Example script for using the FMOD EX low level sound system.
    3. Initializes the FMOD sound system and loads and plays some sounds.
    4. Visit [url]www.squaretangle.com[/url] for updates and more information.
    5. Enjoy.
    6. johnny tangle
    7. ********************************************************************/
    8.  
    9. using UnityEngine;
    10. using System;
    11. using System.Collections;
    12.  
    13. public class teste_spectrum : MonoBehaviour
    14. {
    15.     private FMOD.System system = null;
    16.     private FMOD.Sound sound1 = null, sound2 = null, sound3 = null;
    17.     private FMOD.Channel channel = null;
    18.     //private uint            ms      = 0;
    19.     //private uint            lenms   = 0;
    20.     //private bool            playing = false;
    21.     //private bool            paused  = false;
    22.     //private int             channelsplaying = 0;
    23.     //private bool          soundPlayed = false;
    24.  
    25.     // Use this for initialization
    26.     float[] spectrum = new float[512];
    27.     float[] wavedata = new float[512];
    28.     bool is_playing = false;
    29.  
    30.     void Start()
    31.     {
    32.         uint version = 0;
    33.         FMOD.RESULT result;
    34.  
    35.  
    36.         //Create an FMOD System object            
    37.         result = FMOD.Factory.System_Create(ref system);
    38.         ERRCHECK(result);
    39.         //Check FMOD Version
    40.         result = system.getVersion(ref version);
    41.         ERRCHECK(result);
    42.         if (version < FMOD.VERSION.number)
    43.         {
    44.             Debug.Log("Error!  You are using an old version of FMOD " + version.ToString("X") + ".  This program requires " + FMOD.VERSION.number.ToString("X") + ".");
    45.         }
    46.         //Initialize the FMOD system object
    47.         result = system.init(32, FMOD.INITFLAG.NORMAL, (IntPtr)null);
    48.         ERRCHECK(result);
    49.         if (result == FMOD.RESULT.OK)
    50.         {
    51.             Debug.Log("FMOD init! " + result);
    52.         }
    53.  
    54.         //Create some sound references to play
    55.         result = system.createSound("Assets/FMOD/jaguar.wav", FMOD.MODE.SOFTWARE, ref sound1);
    56.         ERRCHECK(result);
    57.  
    58.         system.setOutput(FMOD.OUTPUTTYPE.NOSOUND_NRT);
    59.  
    60.     }
    61.  
    62.     // Update is called once per frame
    63.     void Update()
    64.     {
    65.        
    66.         FMOD.RESULT result;
    67.         //Use some keyboard input to play the sound references             
    68.         if (Input.GetKeyDown("1"))
    69.         {
    70.             result = system.playSound(FMOD.CHANNELINDEX.FREE, sound1, false, ref channel);
    71.             ERRCHECK(result);
    72.         }
    73.  
    74.  
    75.         if (channel != null){
    76.            
    77.             system.update();
    78.            
    79.             wavedata = new float[256];
    80.             result = system.getWaveData(wavedata, 256, 1);
    81.             ERRCHECK(result);
    82.             Debug.Log("wavedata: " + wavedata[0]);
    83.  
    84.             spectrum = new float[512];
    85.             result = channel.getSpectrum(spectrum, 512, 0, FMOD.DSP_FFT_WINDOW.TRIANGLE);
    86.             ERRCHECK(result);
    87.             Debug.Log("spectrum: " + spectrum[100]);
    88.            
    89.         }
    90.     }
    91.  
    92.     void OnDisable()
    93.     {
    94.         //cleanup routine here
    95.  
    96.         //Shut down
    97.         FMOD.RESULT result;
    98.  
    99.         if (sound1 != null)
    100.         {
    101.             result = sound1.release();
    102.             ERRCHECK(result);
    103.         }
    104.         if (system != null)
    105.         {
    106.             result = system.close();
    107.             ERRCHECK(result);
    108.             result = system.release();
    109.             ERRCHECK(result);
    110.             Debug.Log("FMOD release! " + result);
    111.         }
    112.     }
    113.     //FMOD error checking codes
    114.     void ERRCHECK(FMOD.RESULT result)
    115.     {
    116.         if (result != FMOD.RESULT.OK)
    117.         {
    118.             Debug.Log("FMOD error! " + result + " - " + FMOD.Error.String(result));
    119.  
    120.         }
    121.     }
    122. }
    123.  
     
  42. Mark-Ripley

    Mark-Ripley

    Joined:
    Aug 22, 2007
    Posts:
    118
    Is it possible to use this to generate spectrums based on music that's being played outside of the Unity app? I'm writing a music visualiser, and at the moment it's based upon an iTunes visualiser plugin. It'd be nice to be able to make it work from any music that's being played regardless of which music player app they're using.

    Thanks :)
     
  43. johnny tangle

    johnny tangle

    Joined:
    Aug 14, 2009
    Posts:
    15
    @pegorari
    not sure what you need but does getWaveData return what you want?
    here is a rough example, can use wavedata to animate an object.

    Code (csharp):
    1. /******************************************************************
    2. Spectrum.cs
    3. Example script for using the FMOD EX low level sound system.
    4. Initializes the FMOD sound system and loads and plays some sounds with keyboard then gets wave data of played sound.
    5. Visit [url]www.squaretangle.com[/url] for updates and more information.
    6. Enjoy.
    7. johnny tangle
    8. ********************************************************************/
    9.  
    10. using UnityEngine;
    11. using System;
    12. using System.Collections;
    13.  
    14. public class Spectrum : MonoBehaviour {
    15.    
    16.                 private const int SPECTRUMSIZE = 512;
    17.         private const int WAVEDATASIZE = 256;
    18.  
    19.         private FMOD.System     system  = null;
    20.         private FMOD.Sound      sound   = null;
    21.         private FMOD.Channel    channel = null;
    22.         private bool    isplaying = false;
    23.         private bool        ispaused = false;
    24.         public string   soundFile = "Assets/FMOD/jaguar.wav";
    25.         //public AudioClip soundFile;
    26.  
    27.         private float[] spectrum = new float[SPECTRUMSIZE];
    28.         private float[] wavedata = new float[WAVEDATASIZE];
    29.        
    30.         private int numchannels                     = 0;
    31.         private int dummy                           = 0;
    32.         private int count                           = 0;
    33.         private int count2                          = 0;
    34.                 public  float y                             =0;
    35.                 private float y1                                =0;
    36.                 private Vector3 spectPosVect;
    37.        
    38.         // Use this for initialization
    39.     void Start () {
    40.                         uint            version = 0;
    41.             FMOD.RESULT     result;
    42.            
    43.             /*
    44.                 Create a System object and initialize.
    45.             */
    46.             result = FMOD.Factory.System_Create(ref system);
    47.             ERRCHECK(result);
    48.  
    49.             result = system.getVersion(ref version);
    50.             ERRCHECK(result);
    51.             if (version < FMOD.VERSION.number)
    52.             {
    53.                 Debug.Log("Error!  You are using an old version of FMOD " + version.ToString("X") + ".  This program requires " + FMOD.VERSION.number.ToString("X") + ".");
    54.             }
    55.  
    56.             result = system.init(32, FMOD.INITFLAG.NORMAL, (IntPtr)null);
    57.             ERRCHECK(result);
    58.         //}
    59.        
    60.                 result = system.createStream(soundFile, FMOD.MODE.SOFTWARE | FMOD.MODE._2D, ref sound);
    61.             ERRCHECK(result);
    62.            
    63.            
    64.           }
    65.            
    66.             // Update is called once per frame
    67.     void Update () {
    68.         FMOD.RESULT     result;
    69.         FMOD.SOUND_FORMAT dummyformat     = FMOD.SOUND_FORMAT.NONE;
    70.     FMOD.DSP_RESAMPLER dummyresampler = FMOD.DSP_RESAMPLER.LINEAR;
    71.     system.getSoftwareFormat(ref dummy, ref dummyformat, ref numchannels, ref dummy ,ref dummyresampler, ref dummy);
    72.  
    73.         //Use some keyboard input to play the sound references             
    74.         if(Input.GetKeyDown("1"))
    75.         {
    76.             if (channel != null)
    77.             {
    78.                 channel.isPlaying(ref isplaying);
    79.             }
    80.  
    81.             if (sound != null  !isplaying)
    82.             {
    83.                 result = system.playSound(FMOD.CHANNELINDEX.FREE, sound, false, ref channel);
    84.                 ERRCHECK(result);
    85.  
    86.  
    87.             }
    88.             else
    89.             {
    90.                 if (channel != null)
    91.                 {
    92.                     channel.stop();
    93.                     channel = null;
    94.                 }
    95.  
    96.  
    97.             }
    98.         }
    99.         else if(Input.GetKeyDown("2"))
    100.         {
    101.             if (channel != null)
    102.             {
    103.  
    104.  
    105.                 channel.getPaused(ref ispaused);
    106.                 channel.setPaused(!ispaused);
    107.  
    108.             }
    109.         }
    110.         //else if(Input.GetKeyDown("3"))
    111.         //{
    112.             //result = system.playSound(FMOD.CHANNELINDEX.FREE, sound3, false, ref channel);
    113.             //ERRCHECK(result);
    114.         //}
    115.         /*
    116.                     DRAW WAVEDATA
    117.             */
    118.             for (count = 0; count < numchannels; count++)
    119.             {
    120.                 system.getWaveData(wavedata, WAVEDATASIZE, count);
    121.  
    122.                 for (count2 = 0; count2 < WAVEDATASIZE-1; count2++)
    123.                 {
    124.  
    125.                     y = (wavedata[count2] + 1) / 2.0f * 100;
    126.  
    127.                     spectPosVect.y = y;
    128.                     //Debug.Log("Spectrum Vector X: " + spectPosVect.x);
    129.                     GameObject.Find("SpectrumSphere").transform.position = spectPosVect;
    130.                            
    131.                     if(y != y1){
    132.                     Debug.Log("Wave Data: " + y);
    133.                     y1 = y;
    134.                   }
    135.                 }
    136.             }
    137.  
    138.     }
    139.        
    140.      
    141.     void OnDisable() {
    142.         //cleanup routine here
    143.                
    144.                 //Shut down
    145.                 FMOD.RESULT     result;
    146.                
    147.                 if (sound != null)
    148.                 {
    149.                     result = sound.release();
    150.                     ERRCHECK(result);
    151.                 }
    152.                
    153.                 if (system != null)
    154.                 {
    155.                     result = system.close();
    156.                     ERRCHECK(result);
    157.                     result = system.release();
    158.                     ERRCHECK(result);
    159.                     Debug.Log("FMOD release! " + result );
    160.                 }
    161.     }
    162.     //FMOD error checking codes
    163.     void ERRCHECK(FMOD.RESULT result)
    164.         {
    165.             if (result != FMOD.RESULT.OK)
    166.             {
    167.                 Debug.Log("FMOD error! " + result + " - " + FMOD.Error.String(result));
    168.                
    169.             }
    170.         }
    171. }
     
  44. pegorari

    pegorari

    Joined:
    Nov 19, 2009
    Posts:
    46
    Hello Johnny!

    I´m trying to make a spectrum image from an audio like this one:



    According with some FMOD forum posts, getSpectrum() it´s the best way to make a real and complete spectrum of a sound.

    So I tried to use getSpectrum function, but there is no response in Unity, (i´m using Unity 2.6.1 free and 0.4.25.1 Unity FMOD parser), just like the sound was paused...

    Anyway, I think the getWaveData() function can make something similar and solve my problem...

    Thanks for the help (and sorry for the bad english :D)!
     
  45. klabauterklaus

    klabauterklaus

    Joined:
    Feb 9, 2010
    Posts:
    9
    Any news regarding Unity 2.6.1 on PC compatibility problems? I would be very interested in using FMOD in our project.. Or would it be possible to make a Windows build using the Mac Unity version (with FMOD working of course)?
     
  46. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    999
    A pretty easy workaround is mentioned by JC here: http://answers.unity3d.com/questions/2786/dll-loading-in-editor-on-windows/3920#3920

    You basically rename fmodex.dll to fmode2.dll and rename it as well in both dll's (fmodex.dll, fmodevent.dll). Finally rename fmodex to fmode2 in the unity fmod plugin.

    In total I had to rename fmodev to fmode2 only three times I believe (Just once per dll/plugin).
    I used the free version of Neo hex editor for windows.
     
  47. klabauterklaus

    klabauterklaus

    Joined:
    Feb 9, 2010
    Posts:
    9
    Ah great, gonna try that, thx!
     
  48. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    999
    The plugin lacks getDSPClock, is that correct?


    And does anyone know how to rewrite this to C#?
    Code (csharp):
    1. // simple 64-bit int handling types
    2. typedef unsigned __int64 Uint64;
    3.  
    4. class Uint64P
    5. {
    6. public:
    7.     Uint64P(Uint64 val = 0)
    8.         : mHi(unsigned int(val >> 32)), mLo(unsigned int(val  0xFFFFFFFF))
    9.     { }
    10.  
    11.     Uint64 value() const
    12.     {
    13.         return (Uint64(mHi) << 32) + mLo;
    14.     }
    15.  
    16.     void operator+=(const Uint64P &rhs)
    17.     {
    18.         FMOD_64BIT_ADD(mHi, mLo, rhs.mHi, rhs.mLo);
    19.     }
    20.  
    21.     unsigned int mHi;
    22.     unsigned int mLo;
    23. };
    24.  
    It's from the sample-accurate sequence example
     
  49. joehamilton

    joehamilton

    Joined:
    Apr 24, 2010
    Posts:
    10
    I maybe a little out of my depth here but could I use this to get a mic-in to unity?
    And if so. Any tips on where to start?

    btw. I am hoping to use the mic amplitude to control objects.

    cheers
     
  50. havchr

    havchr

    Joined:
    Jun 18, 2009
    Posts:
    58
    I'm trying to get the pitch detection to work, but so far I've had no luck.
    Tried it on 2.6.1f3 on mac and 2.6.1 on the pc.

    On both it has failed on createSound with INVALID_PARAM ..
    Is there some weird conflict thing, or is the exinfo supposed to be different? I'm not sure what's going on. Anyone got it working?
     
unityunity