public static FMOD.RESULT createSound(this FMOD.System system, IntPtr data, FMOD.MODE mode, ref FMOD.CREATESOUNDEXINFO exinfo, ref FMOD.Sound sound) { FMOD.RESULT result = FMOD.RESULT.OK; IntPtr soundraw = new IntPtr(); FMOD.Sound soundnew = null; try { result = FMOD_System_CreateSound(system.getRaw(), data, mode, ref exinfo, ref soundraw); } catch { result = FMOD.RESULT.ERR_INVALID_PARAM; } if (result != FMOD.RESULT.OK) { return(result); } if (sound == null) { soundnew = new FMOD.Sound(); soundnew.setRaw(soundraw); sound = soundnew; } else { sound.setRaw(soundraw); } return(result); }
public static bool LoadSound(string key, string soundFile, bool looping = false, bool oneAtATime = false) { FMOD.MODE mode = FMOD.MODE._2D | FMOD.MODE._3D | FMOD.MODE._3D_WORLDRELATIVE | FMOD.MODE.CREATESAMPLE; if (looping) { mode |= FMOD.MODE.LOOP_NORMAL; } if (oneAtATime) { mode |= FMOD.MODE.UNIQUE; } FMOD.RESULT r1 = RuntimeManager.CoreSystem.createSound(soundFile, mode, out FMOD.Sound sound); if (r1 == FMOD.RESULT.OK) { sounds.Add(key, sound); return(true); } else { Debug.LogError($"AutoUtil: Failed to create sound. (key={key}, error={r1})"); return(false); } }
public static void LoadSong(int id, bool loop) { PlayingSongID = id; if (CurrentSong != null) { CurrentSong.release(); } FMOD.MODE mode = (loop) ? FMOD.MODE.LOOP_NORMAL : FMOD.MODE.DEFAULT; system.createStream(PlayList[id], mode, out CurrentSong); system.playSound(CurrentSong, ChannelGroup, false, out Channel); }
public FMOD.Sound LoadSound(string path, FMOD.MODE mode = FMOD.MODE._2D) { FMOD.Sound sound; FMOD.RESULT res = soundSystem.createSound(path, mode, out sound); if (res == FMOD.RESULT.OK) { return(sound); } else { throw new Exceptions.LoadFailException(FMOD.Error.String(res) + " File name: " + path); } }
public void LoadAnySound(SoundResource soundResource, string fullPath) { //FMOD.MODE flags = FMOD.MODE.HARDWARE; FMOD.MODE flags = FMOD.MODE.SOFTWARE | FMOD.MODE._3D_LINEARROLLOFF; if (soundResource.Is3DSound) { flags |= FMOD.MODE._3D | FMOD.MODE._3D_IGNOREGEOMETRY; } else { flags |= FMOD.MODE._2D; } FMOD.Sound sound = new FMOD.Sound(); soundResource.SoundGlue = new FMODSound(sound); if (soundResource.IsStream) { flags = FMOD.MODE.SOFTWARE; flags |= FMOD.MODE.CREATESTREAM | FMOD.MODE.NONBLOCKING; FMOD.CREATESOUNDEXINFO createSoundExInfo = new FMOD.CREATESOUNDEXINFO(); createSoundExInfo.cbsize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(FMOD.CREATESOUNDEXINFO)); createSoundExInfo.nonblockcallback = NonBlockCallback; ERRCHECK(FMODSystem.createSound(fullPath, flags, ref createSoundExInfo, ref sound)); int index = sound.getRaw().ToInt32(); try { loadingSoundResources[index] = soundResource; } catch (IndexOutOfRangeException) { System.Diagnostics.Debugger.Break(); // please report the value of the index-variable defined above } } else { ERRCHECK(FMODSystem.createSound(fullPath, flags, ref sound)); InitiateLoadedSound(soundResource); } }
static FMOD.RESULT DialogueEventCallback(FMOD.Studio.EVENT_CALLBACK_TYPE type, IntPtr instancePtr, IntPtr parameterPtr) { FMOD.Studio.EventInstance instance = new FMOD.Studio.EventInstance(instancePtr); // Retrieve the user data IntPtr stringPtr; instance.getUserData(out stringPtr); // Get the string object GCHandle stringHandle = GCHandle.FromIntPtr(stringPtr); String key = stringHandle.Target as String; switch (type) { case FMOD.Studio.EVENT_CALLBACK_TYPE.CREATE_PROGRAMMER_SOUND: { FMOD.MODE soundMode = FMOD.MODE.DEFAULT | FMOD.MODE.CREATESTREAM; var parameter = (FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)); if (key.Contains(".")) { FMOD.Sound dialogueSound; var soundResult = FMODUnity.RuntimeManager.CoreSystem.createSound(Application.streamingAssetsPath + "/" + key, soundMode, out dialogueSound); if (soundResult == FMOD.RESULT.OK) { parameter.sound = dialogueSound.handle; parameter.subsoundIndex = -1; Marshal.StructureToPtr(parameter, parameterPtr, false); } } else { FMOD.Studio.SOUND_INFO dialogueSoundInfo; var keyResult = FMODUnity.RuntimeManager.StudioSystem.getSoundInfo(key, out dialogueSoundInfo); if (keyResult != FMOD.RESULT.OK) { break; } FMOD.Sound dialogueSound; var soundResult = FMODUnity.RuntimeManager.CoreSystem.createSound(dialogueSoundInfo.name_or_data, soundMode | dialogueSoundInfo.mode, ref dialogueSoundInfo.exinfo, out dialogueSound); if (soundResult == FMOD.RESULT.OK) { parameter.sound = dialogueSound.handle; parameter.subsoundIndex = dialogueSoundInfo.subsoundindex; Marshal.StructureToPtr(parameter, parameterPtr, false); } } } break; case FMOD.Studio.EVENT_CALLBACK_TYPE.DESTROY_PROGRAMMER_SOUND: { var parameter = (FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)); var sound = new FMOD.Sound(); sound.handle = parameter.sound; sound.release(); } break; case FMOD.Studio.EVENT_CALLBACK_TYPE.DESTROYED: // Now the event has been destroyed, unpin the string // memory so it can be garbage collected stringHandle.Free(); break; } return(FMOD.RESULT.OK); }
// HACER: CREAR Y REPRODUCIR EVENTOS 3D O SIN POSICIONAMIENTO (CREATE3D(), PLAY3D()) public void Create(string path, FMOD.MODE mode, out FMOD.Sound sound) { SoundSystem.instance.ErrorCheck(_soundSystem.createSound(path, mode | FMOD.MODE._3D, out sound)); }
private void FMODloopButton_CheckedChanged(object sender, EventArgs e) { FMOD.RESULT result; if (FMODloopButton.Checked) { loopMode = FMOD.MODE.LOOP_NORMAL; } else { loopMode = FMOD.MODE.LOOP_OFF; } if (sound != null) { result = sound.setMode(loopMode); if (ERRCHECK(result)) { return; } } if (channel != null) { bool playing = false; result = channel.isPlaying(out playing); if ((result != FMOD.RESULT.OK) && (result != FMOD.RESULT.ERR_INVALID_HANDLE)) { if (ERRCHECK(result)) { return; } } bool paused = false; result = channel.getPaused(out paused); if ((result != FMOD.RESULT.OK) && (result != FMOD.RESULT.ERR_INVALID_HANDLE)) { if (ERRCHECK(result)) { return; } } if (playing || paused) { result = channel.setMode(loopMode); if (ERRCHECK(result)) { return; } } /*else { /esult = system.playSound(FMOD.CHANNELINDEX.FREE, sound, false, ref channel); ERRCHECK(result); result = channel.setMode(loopMode); ERRCHECK(result); }*/ } }
static FMOD.RESULT DialogueEventCallback(FMOD.Studio.EVENT_CALLBACK_TYPE type, FMOD.Studio.EventInstance instance, IntPtr parameterPtr) { IntPtr stringPtr; instance.getUserData(out stringPtr); GCHandle stringHandle = GCHandle.FromIntPtr(stringPtr); String key = stringHandle.Target as String; switch (type) { case FMOD.Studio.EVENT_CALLBACK_TYPE.CREATE_PROGRAMMER_SOUND: { FMOD.MODE soundMode = FMOD.MODE.LOOP_NORMAL | FMOD.MODE.CREATECOMPRESSEDSAMPLE | FMOD.MODE.NONBLOCKING; var parameter = (FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)); if (key.Contains(".")) { FMOD.Sound dialogueSound; var soundResult = FMODUnity.RuntimeManager.CoreSystem.createSound(Application.streamingAssetsPath + "/" + key, soundMode, out dialogueSound); if (soundResult == FMOD.RESULT.OK) { parameter.sound = dialogueSound.handle; parameter.subsoundIndex = -1; Marshal.StructureToPtr(parameter, parameterPtr, false); } } else { FMOD.Studio.SOUND_INFO dialogueSoundInfo; var keyResult = FMODUnity.RuntimeManager.StudioSystem.getSoundInfo(key, out dialogueSoundInfo); if (keyResult != FMOD.RESULT.OK) { break; } FMOD.Sound dialogueSound; var soundResult = FMODUnity.RuntimeManager.CoreSystem.createSound(dialogueSoundInfo.name_or_data, soundMode | dialogueSoundInfo.mode, ref dialogueSoundInfo.exinfo, out dialogueSound); if (soundResult == FMOD.RESULT.OK) { parameter.sound = dialogueSound.handle; parameter.subsoundIndex = dialogueSoundInfo.subsoundindex; Marshal.StructureToPtr(parameter, parameterPtr, false); } } } break; case FMOD.Studio.EVENT_CALLBACK_TYPE.DESTROY_PROGRAMMER_SOUND: { var parameter = (FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)); var sound = new FMOD.Sound(); sound.handle = parameter.sound; sound.release(); } break; case FMOD.Studio.EVENT_CALLBACK_TYPE.DESTROYED: stringHandle.Free(); break; } return(FMOD.RESULT.OK); }
public static void Update() { if (system == null) { return; } // Update sound if (channel != null) { FMOD.MODE mode = FMOD.MODE.DEFAULT; channel.getMode(ref mode); if ((mode & FMOD.MODE._3D) == FMOD.MODE._3D) { FMOD.VECTOR pos; pos.x = 0.0f; pos.y = 0.0f; pos.z = 0.0f; FMOD.VECTOR vel; vel.x = 0.0f; vel.y = 0.0f; vel.z = 0.0f; channel.get3DAttributes(ref pos, ref vel); float distance = (float)Math.Sqrt((double)(pos.x * pos.x + pos.y * pos.y)); float minDist = 0.0f, maxDist = 1000.0f; channel.get3DMinMaxDistance(ref minDist, ref maxDist); float distRatio = distance / maxDist; if (ignorefod == false) { // Set direct and reverb occlusion float direct = 1.0f - GetDistanceRatioValue(CONTROLTYPE.DIRECT, distRatio); float reverb = 1.0f - GetDistanceRatioValue(CONTROLTYPE.REVERB, distRatio); channel.set3DOcclusion(direct, reverb); // Set lowpass FMOD.DSP head = null; channel.getDSPHead(ref head); if (head != null) { int inputs = 0; head.getNumInputs(ref inputs); for (int i = 0; i < inputs; i++) { FMOD.DSP dsp = null; FMOD.DSPConnection dspcon = null; head.getInput(i, ref dsp, ref dspcon); if (dsp == null) { continue; } FMOD.DSP_TYPE type = FMOD.DSP_TYPE.UNKNOWN; dsp.getType(ref type); if (type == FMOD.DSP_TYPE.LOWPASS_SIMPLE) { float hrtf = 1.0f; FMOD.VECTOR dir; dir.x = pos.x; dir.y = pos.y; dir.z = 0.0f; float length = (float)Math.Sqrt((double)(dir.x * dir.x + dir.y * dir.y)); if (length > 0.0001f) { length = 1.0f / length; dir.x *= length; dir.y *= length; float _angle = (float)Math.Acos((double)dir.y); hrtf = 1.0f - HRTFDATA.Scale * Math.Min(1.0f, Math.Max(0.0f, (_angle - HRTFDATA.MinAngle) / (HRTFDATA.MaxAngle - HRTFDATA.MinAngle))); } float lowpass = 21990.0f * GetDistanceRatioValue(CONTROLTYPE.LOWPASS, distRatio) * hrtf + 10.0f; dsp.setParameter((int)FMOD.DSP_LOWPASS_SIMPLE.CUTOFF, lowpass); break; } } } // Set pan level float panlevel = GetDistanceRatioValue(CONTROLTYPE.PANLEVEL, distRatio); channel.set3DPanLevel(panlevel); } else { channel.set3DPanLevel(0.0f); } } } system.update(); }
public static bool Play(string strRootPath, SoundItemProperty prop) { FMOD.RESULT result; if (channel != null) { channel.stop(); } string name = strRootPath + prop.FileName; FMOD.MODE mode = FMOD.MODE.DEFAULT; if (prop.Type == SoundItemProperty.typeSound[0]) { mode |= FMOD.MODE._2D | FMOD.MODE.CREATESAMPLE | FMOD.MODE.SOFTWARE; } else if (prop.Type == SoundItemProperty.typeSound[1]) { mode |= FMOD.MODE._3D | FMOD.MODE.CREATESAMPLE | FMOD.MODE.SOFTWARE | FMOD.MODE._3D_LINEARROLLOFF | FMOD.MODE._3D_WORLDRELATIVE | FMOD.MODE._3D_IGNOREGEOMETRY; } else if (prop.Type == SoundItemProperty.typeSound[2]) { mode |= FMOD.MODE._2D | FMOD.MODE.CREATESTREAM | FMOD.MODE.SOFTWARE; } if (prop.Loop == true) { mode |= FMOD.MODE.LOOP_NORMAL; } result = system.createSound(name, mode, ref sound); ERRCHECK(result); result = system.playSound(FMOD.CHANNELINDEX.FREE, sound, true, ref channel); ERRCHECK(result); if (prop.Type == SoundItemProperty.typeSound[1]) { channel.set3DMinMaxDistance(100.0f, prop.MaxDistance); FMOD.VECTOR pos = new FMOD.VECTOR(); pos.x = 0.0f; pos.y = 0.0f; pos.z = 0.0f; FMOD.VECTOR vel = new FMOD.VECTOR(); vel.x = 0.0f; vel.y = 0.0f; vel.z = 0.0f; channel.set3DAttributes(ref pos, ref vel); FMOD.DSP dsp = null; result = system.createDSPByType(FMOD.DSP_TYPE.LOWPASS_SIMPLE, ref dsp); ERRCHECK(result); dsp.setParameter((int)FMOD.DSP_LOWPASS_SIMPLE.CUTOFF, 22000.0f); FMOD.DSPConnection dspcon = null; channel.addDSP(dsp, ref dspcon); } channel.setVolume((float)prop.Volume * 0.01f); channel.setCallback(CHANNELCALLBACK); channel.setPaused(false); return(true); }
public static void Play(string key, int speaker, float volume = 1f, bool loop = false) { #if NO_AUDIO return; #endif // play audio FMOD.RESULT result = system.playSound(FMOD.CHANNELINDEX.FREE, sounds[key], true, ref channel); ERRCHECK(result); result = channel.setVolume(volume); ERRCHECK(result); int index = 0; channel.getIndex(ref index); channelIDs.Add(index); FMOD.MODE loopAudio = (loop) ? FMOD.MODE.LOOP_NORMAL : FMOD.MODE.LOOP_OFF; result = channel.setMode(loopAudio); //SPEAKER 1 (Satelite Audio, Side Specific) if (speaker == 0) { result = channel.setSpeakerMix(0, 0, 0, 0, 0, 0, 1.0f, 0); ERRCHECK(result); } //SPEAKER 2 (Satelite Audio, Side Specific) if (speaker == 1) { result = channel.setSpeakerMix(0, 0, 0, 0, 0, 0, 0, 1.0f); ERRCHECK(result); } //SPEAKER 3 (Satelite Audio, Side Specific) if (speaker == 2) { result = channel.setSpeakerMix(0, 0, 0, 0, 1.0f, 0, 0, 0); ERRCHECK(result); } //SPEAKER (Satelite Audio, Side Specific) if (speaker == 3) { result = channel.setSpeakerMix(0, 0, 0, 0, 0, 1.0f, 0, 0); ERRCHECK(result); } //SPEAKER 5 (Subwoofer) if (speaker == 4) { result = channel.setSpeakerMix(0, 0, 1.0f, 0, 0, 0, 0, 0); ERRCHECK(result); } if (isMuted) { channel.setMute(isMuted); } result = channel.setPaused(false); ERRCHECK(result); }
private static extern FMOD.RESULT FMOD_System_CreateStream(IntPtr systemraw, short[] data, FMOD.MODE mode, ref FMOD.CREATESOUNDEXINFO exinfo, ref IntPtr stream);
private static extern FMOD.RESULT FMOD_System_CreateSound(IntPtr systemraw, IntPtr data, FMOD.MODE mode, ref FMOD.CREATESOUNDEXINFO exinfo, ref IntPtr sound);
public void Play() { #if !NO_FMOD if (Playing && mChannel != null) { bool paused = false; mChannel.getPaused(ref paused); if (paused) { mChannel.setPaused(false); return; } else { throw new InvalidOperationException(); } } else { if (data._streamIndex >= 0) { if (mStreamBytes != null) { mAbortStream = false; return; } // if this isn't true, we may have 2 async requests to play, which would be super bad GSGE.Debug.assert(mStreamBytes == null); GSGE.Debug.assert(mAbortStream == false); } if (mSound != null) { Dispose(); } FMOD.RESULT result; FMOD.CREATESOUNDEXINFO exinfo = SoundData.exinfo; exinfo.cbsize = Marshal.SizeOf(exinfo); // allocate streaming? if (data._size > 0) { mStreamRequested = true; mStreamBytes = new byte[data._size]; string file = "Content/Audio/"; if (data._persist) { file += "Deferred/"; } else { file += "Streaming/"; } file += data._streamIndex; file += ".mp3"; //GSGE.Debug.logMessage("streaming audio " + file); NaCl.AsyncFS.FileStream fs = new NaCl.AsyncFS.FileStream(file, System.IO.FileMode.Open); fs.BeginRead(mStreamBytes, 0, mStreamBytes.Length, delegate(IAsyncResult aresult) { int bytesRead; try { bytesRead = fs.EndRead(aresult); } catch (System.IO.FileNotFoundException) { bytesRead = -1; } fs.Close(); if (bytesRead != data._size) { GSGE.Debug.logMessage("Error streaming in sound " + data._streamIndex); mStreamStarted = true; Dispose(); return; } if (data._persist) { byte[] audiodata = mStreamBytes; mStreamBytes = null; mStreamRequested = false; mStreamStarted = false; // reconfigure parent Sound to be persistent now. data.setPersistent(audiodata); if (mAbortStream) { Dispose(); } else { Play(); } } else { mStreamStarted = true; if (mAbortStream) { Dispose(); } else { exinfo.length = (uint)mStreamBytes.Length; FMOD.MODE mode = FMOD.MODE.OPENMEMORY_POINT | FMOD.MODE.CREATECOMPRESSEDSAMPLE | FMOD.MODE.SOFTWARE; if (data._looping) { mode |= FMOD.MODE.LOOP_NORMAL; } result = FMOD.Framework.system.createSound(mStreamBytes, mode, ref exinfo, ref mSound); mOwnSound = true; ERRCHECK(result); AudioEngine audioengine = GSGE.AudioManager.GetAudioManager().GetAudioEngine(); audioengine.FmodSounds.Add(this); result = FMOD.Framework.system.playSound(FMOD.CHANNELINDEX.FREE, mSound, false, ref mChannel); if (mSound == null) { Dispose(); } else { GSGE.Debug.assert(mSound != null); ERRCHECK(result); mChannel.getFrequency(ref mBaseFrequency); } } } // all streaming sounds need to assign these asap Volume = _volume; Pitch = _pitch; LowPassCutoff = _lowPassCutoff; Reverb = _reverb; mAbortStream = false; }, null); } else { if (data.mSound == null) { data.createSound(); } //exinfo.length = (uint)data.data.Length; //FMOD.MODE mode = FMOD.MODE.OPENMEMORY_POINT | FMOD.MODE.CREATECOMPRESSEDSAMPLE | FMOD.MODE.SOFTWARE; //if (data._looping) // mode |= FMOD.MODE.LOOP_NORMAL; //result = FMOD.Framework.system.createSound(data.data, mode, ref exinfo, ref mSound); //ERRCHECK(result); mSound = data.mSound; if (mSound == null) { Dispose(); return; } GSGE.Debug.assert(mSound != null); AudioEngine audioengine = GSGE.AudioManager.GetAudioManager().GetAudioEngine(); audioengine.FmodSounds.Add(this); result = FMOD.Framework.system.playSound(FMOD.CHANNELINDEX.FREE, mSound, false, ref mChannel); GSGE.Debug.assert(mSound != null); ERRCHECK(result); mChannel.getFrequency(ref mBaseFrequency); } } #endif }