// ======================================================================================================================================== #region Start / Stop void StartFMODSound(int sampleRate) { this.StopFMODSound(); FMOD.CREATESOUNDEXINFO exinfo = new FMOD.CREATESOUNDEXINFO(); // exinfo.cbsize = sizeof(FMOD.CREATESOUNDEXINFO); exinfo.numchannels = this.unityOAFRChannels; /* Number of channels in the sound. */ exinfo.defaultfrequency = sampleRate; /* Default playback rate of sound. */ exinfo.decodebuffersize = (uint)this.unityOAFRDataLength; /* Chunk size of stream update in samples. This will be the amount of data passed to the user callback. */ exinfo.length = (uint)(exinfo.defaultfrequency * exinfo.numchannels * this.elementSize); /* Length of PCM data in bytes of whole song (for Sound::getLength) */ exinfo.format = FMOD.SOUND_FORMAT.PCM16; /* Data format of sound. */ exinfo.pcmreadcallback = this.pcmreadcallback; /* User callback for reading. */ exinfo.pcmsetposcallback = this.pcmsetposcallback; /* User callback for seeking. */ result = system.createSound("" , FMOD.MODE.OPENUSER | FMOD.MODE.CREATESTREAM | FMOD.MODE.LOOP_NORMAL , ref exinfo , out sound); AudioStreamSupport.ERRCHECK(result, this.logLevel, this.gameObjectName, null, "system.createSound"); AudioStreamSupport.LOG(LogLevel.DEBUG, this.logLevel, this.gameObjectName, null, "About to play..."); result = system.playSound(sound, null, false, out channel); AudioStreamSupport.ERRCHECK(result, this.logLevel, this.gameObjectName, null, "system.playSound"); }
// ======================================================================================================================================== #region user support public void SetOutput(int _outputDriverID) { AudioStreamSupport.LOG(LogLevel.INFO, this.logLevel, this.gameObjectName, null, "Setting output to driver {0} ", _outputDriverID); result = system.setDriver(_outputDriverID); AudioStreamSupport.ERRCHECK(result, this.logLevel, this.gameObjectName, null, "system.setDriver"); this.outputDriverID = _outputDriverID; }
// ======================================================================================================================================== #region Unity lifecycle void Start() { this.gameObjectName = this.gameObject.name; result = FMOD.Factory.System_Create(out system); AudioStreamSupport.ERRCHECK(result, this.logLevel, this.gameObjectName, null, "FMOD.Factory.System_Create"); result = system.getVersion(out version); AudioStreamSupport.ERRCHECK(result, this.logLevel, this.gameObjectName, null, "system.getVersion"); if (version < FMOD.VERSION.number) { var msg = string.Format("FMOD lib version {0} doesn't match header version {1}", version, FMOD.VERSION.number); throw new System.Exception(msg); } int rate; FMOD.SPEAKERMODE sm; int sc; result = system.getSoftwareFormat(out rate, out sm, out sc); AudioStreamSupport.ERRCHECK(result, this.logLevel, this.gameObjectName, null, "system.getSoftwareFormat"); AudioStreamSupport.LOG(LogLevel.INFO, this.logLevel, this.gameObjectName, null, "FMOD samplerate: {0}, speaker mode: {1}, num. of raw speakers {2}", rate, sm, sc); // TODO: evaluate maxchannels result = system.init(32, FMOD.INITFLAGS.NORMAL, extradriverdata); AudioStreamSupport.ERRCHECK(result, this.logLevel, this.gameObjectName, null, "system.init"); this.SetOutput(this.outputDriverID); /* tags ERR_FILE_COULDNOTSEEK: * http://stackoverflow.com/questions/7154223/streaming-mp3-from-internet-with-fmod * http://www.fmod.org/docs/content/generated/FMOD_System_SetFileSystem.html */ // result = system.setFileSystem(null, null, null, null, null, null, -1); // ERRCHECK(result, "system.setFileSystem"); // Explicitly create the delegate object and assign it to a member so it doesn't get freed // by the garbage collected while it's being used this.pcmreadcallback = new FMOD.SOUND_PCMREADCALLBACK(PCMReadCallback); this.pcmsetposcallback = new FMOD.SOUND_PCMSETPOSCALLBACK(PCMSetPosCallback); this.elementSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(System.Int16)); // decodebuffersize samples worth of bytes will be called in read callback // createSound calls back, too this.pcmReadCallbackBuffer = new List <List <byte> >(); this.pcmReadCallbackBuffer.Add(new List <byte>()); this.pcmReadCallbackBuffer.Add(new List <byte>()); }
// ======================================================================================================================================== #region Support protected void ERRCHECK(FMOD.RESULT result, string customMessage, bool throwOnError = true) { this.lastError = result; AudioStreamSupport.ERRCHECK(result, this.logLevel, this.gameObjectName, this.OnError, customMessage, throwOnError); }