Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Runtime.InteropServices;
- using AOT;
- using FMOD;
- using FMOD.Studio;
- using FMODUnity;
- using UnityEngine;
- public static class AudioClipToFmod
- {
- private static EVENT_CALLBACK dialogueCallback;
- public static EventInstance Play(AudioClip audioclip)
- {
- float[] audioclip_data = new float[audioclip.samples * audioclip.channels];
- audioclip.GetData(audioclip_data, 0);
- SoundRequirements sound_requirements = new SoundRequirements(
- audioclip.name,
- audioclip.samples,
- audioclip.channels,
- audioclip.frequency,
- audioclip_data);
- var audioClipInstance = RuntimeManager.CreateInstance("event:/App/WalkieTalkie");
- GCHandle stringHandle = GCHandle.Alloc(sound_requirements);
- audioClipInstance.setUserData(GCHandle.ToIntPtr(stringHandle));
- dialogueCallback ??= PlayFileCallBackUsingAudioFile;
- audioClipInstance.setCallback(dialogueCallback);
- audioClipInstance.start();
- audioClipInstance.release();
- return audioClipInstance;
- }
- [MonoPInvokeCallback(typeof(EVENT_CALLBACK))]
- private static RESULT PlayFileCallBackUsingAudioFile(EVENT_CALLBACK_TYPE type, IntPtr instPrt, IntPtr paramsPrt)
- {
- EventInstance inst = new EventInstance(instPrt);
- if (!inst.isValid())
- {
- return RESULT.ERR_EVENT_NOTFOUND;
- }
- inst.getUserData(out IntPtr clipDataPtr);
- GCHandle clipHandle = GCHandle.FromIntPtr(clipDataPtr);
- if (clipHandle.Target is SoundRequirements clip)
- {
- switch (type)
- {
- case EVENT_CALLBACK_TYPE.CREATE_PROGRAMMER_SOUND:
- {
- var param = (PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(paramsPrt, typeof(PROGRAMMER_SOUND_PROPERTIES));
- ERRCHECK(RuntimeManager.CoreSystem.getMasterChannelGroup(out ChannelGroup masterGroup), "Failed to get masterGroup from core system");
- uint lenBytes = (uint)(clip.samples * clip.channels * sizeof(float));
- CREATESOUNDEXINFO soundInfo = new CREATESOUNDEXINFO
- {
- cbsize = Marshal.SizeOf(typeof(CREATESOUNDEXINFO)),
- length = lenBytes,
- format = SOUND_FORMAT.PCMFLOAT,
- defaultfrequency = clip.defaultFrequency,
- numchannels = clip.channels
- };
- RESULT result = ERRCHECK(RuntimeManager.CoreSystem.createSound(clip.name, MODE.OPENUSER, ref soundInfo, out Sound sound), "Failed to create sound");
- if (result != RESULT.OK)
- return result;
- result = ERRCHECK(sound.@lock(0, lenBytes, out IntPtr ptr1, out IntPtr ptr2, out uint len1, out uint len2), "Failed to lock sound");
- if (result != RESULT.OK)
- return result;
- Marshal.Copy(clip.sampleData, 0, ptr1, (int)(len1 / sizeof(float)));
- if (len2 > 0)
- Marshal.Copy(clip.sampleData, (int)(len1 / sizeof(float)), ptr2, (int)(len2 / sizeof(float)));
- result = ERRCHECK(sound.unlock(ptr1, ptr2, len1, len2), "Failed to unlock sound");
- if (result != RESULT.OK)
- return result;
- ERRCHECK(sound.setMode(MODE.DEFAULT), "Failed to set the sound mode");
- param.sound = sound.handle;
- param.subsoundIndex = -1;
- Marshal.StructureToPtr(param, paramsPrt, false);
- }
- break;
- case EVENT_CALLBACK_TYPE.DESTROY_PROGRAMMER_SOUND:
- {
- var param = (PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(paramsPrt, typeof(PROGRAMMER_SOUND_PROPERTIES));
- var sound = new Sound(param.sound);
- RESULT result = ERRCHECK(sound.release(), "Failed to release sound");
- if (result != RESULT.OK)
- return result;
- }
- break;
- case EVENT_CALLBACK_TYPE.DESTROYED:
- clipHandle.Free();
- break;
- }
- }
- return RESULT.OK;
- }
- private static RESULT ERRCHECK(RESULT result, string failMsg)
- {
- #if UNITY_EDITOR
- if (result != RESULT.OK)
- {
- UnityEngine.Debug.Log(failMsg + " with result: " + result);
- return result;
- }
- #endif
- return result;
- }
- }
- internal class SoundRequirements
- {
- public readonly string name;
- public readonly int samples;
- public readonly int channels;
- public readonly int defaultFrequency;
- public readonly float[] sampleData;
- public SoundRequirements(string name, int samples, int channel, int defaultFrequency, float[] sampleData)
- {
- this.name = name;
- this.samples = samples;
- channels = channel;
- this.defaultFrequency = defaultFrequency;
- this.sampleData = sampleData;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement