1 //-----------------------------------------------------------------------------
2 // Copyright (c) 2015-2018 Marcelo Fernandez
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to
6 // deal in the Software without restriction, including without limitation the
7 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 // sell copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 // IN THE SOFTWARE.
21 //-----------------------------------------------------------------------------
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "oamlCommon.h"
28 #include "oamlUnityPlugin.h"
29 
30 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wpedantic"
32 #include "AudioPluginInterface.h"
33 #pragma GCC diagnostic pop
34 
35 oamlApi oaml;
36 
oamlInit(const char * defsFilename)37 int oamlInit(const char *defsFilename) {
38 	return oaml.Init(defsFilename);
39 }
40 
oamlReadDefsFile(const char * defsFilename)41 int oamlReadDefsFile(const char *defsFilename) {
42 	return oaml.ReadDefsFile(defsFilename);
43 }
44 
oamlInitString(const char * defs)45 int oamlInitString(const char *defs) {
46 	return oaml.InitString(defs);
47 }
48 
oamlSetAudioFormat(int freq,int channels,int bytesPerSample)49 void oamlSetAudioFormat(int freq, int channels, int bytesPerSample) {
50 	oaml.SetAudioFormat(freq, channels, bytesPerSample);
51 }
52 
oamlPlayTrack(const char * name)53 int oamlPlayTrack(const char *name) {
54 	return oaml.PlayTrack(name);
55 }
56 
oamlPlayTrackWithStringRandom(const char * str)57 int oamlPlayTrackWithStringRandom(const char *str) {
58 	return oaml.PlayTrackWithStringRandom(str);
59 }
60 
oamlIsTrackPlaying(const char * name)61 bool oamlIsTrackPlaying(const char *name) {
62 	return oaml.IsTrackPlaying(name);
63 }
64 
oamlIsPlaying()65 bool oamlIsPlaying() {
66 	return oaml.IsPlaying();
67 }
68 
oamlStopPlaying()69 void oamlStopPlaying() {
70 	oaml.StopPlaying();
71 }
72 
oamlPause()73 void oamlPause() {
74 	oaml.Pause();
75 }
76 
oamlResume()77 void oamlResume() {
78 	oaml.Resume();
79 }
80 
oamlPauseToggle()81 void oamlPauseToggle() {
82 	oaml.PauseToggle();
83 }
84 
oamlIsPaused()85 bool oamlIsPaused() {
86 	return oaml.IsPaused();
87 }
88 
oamlMixToBuffer(void * buffer,int size)89 void oamlMixToBuffer(void *buffer, int size) {
90 	oaml.MixToBuffer(buffer, size);
91 }
92 
oamlSetCondition(int id,int value)93 void oamlSetCondition(int id, int value) {
94 	oaml.SetCondition(id, value);
95 }
96 
oamlSetVolume(float vol)97 void oamlSetVolume(float vol) {
98 	oaml.SetVolume(vol);
99 }
100 
oamlGetVolume()101 float oamlGetVolume() {
102 	return oaml.GetVolume();
103 }
104 
oamlAddTension(int value)105 void oamlAddTension(int value) {
106 	oaml.AddTension(value);
107 }
108 
oamlSetMainLoopCondition(int value)109 void oamlSetMainLoopCondition(int value) {
110 	oaml.SetMainLoopCondition(value);
111 }
112 
oamlUpdate()113 void oamlUpdate() {
114 	oaml.Update();
115 }
116 
oamlSetFileCallbacks(oamlFileCallbacks * cbs)117 void oamlSetFileCallbacks(oamlFileCallbacks *cbs) {
118 	oaml.SetFileCallbacks(cbs);
119 }
120 
oamlEnableDynamicCompressor(bool enable,double threshold,double ratio)121 void oamlEnableDynamicCompressor(bool enable, double threshold, double ratio) {
122 	oaml.EnableDynamicCompressor(enable, threshold, ratio);
123 }
124 
oamlGetTracksInfo()125 oamlTracksInfo* oamlGetTracksInfo() {
126 	return oaml.GetTracksInfo();
127 }
128 
oamlGetDefsFile()129 const char* oamlGetDefsFile() {
130 	return oaml.GetDefsFile();
131 }
132 
oamlGetPlayingInfo()133 const char* oamlGetPlayingInfo() {
134 	return oaml.GetPlayingInfo();
135 }
136 
oamlShutdown()137 void oamlShutdown() {
138 	oaml.Shutdown();
139 }
140 
CreateCallback(UnityAudioEffectState *)141 UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK CreateCallback(UnityAudioEffectState*) {
142 	return UNITY_AUDIODSP_OK;
143 }
144 
ReleaseCallback(UnityAudioEffectState *)145 UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK ReleaseCallback(UnityAudioEffectState*) {
146 	return UNITY_AUDIODSP_OK;
147 }
148 
ProcessCallback(UnityAudioEffectState * state,float * inbuffer,float * outbuffer,unsigned int length,int inchannels,int outchannels)149 UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK ProcessCallback(UnityAudioEffectState* state, float* inbuffer, float* outbuffer, unsigned int length, int inchannels, int outchannels) {
150 	if (inchannels != outchannels)
151 		return UNITY_AUDIODSP_ERR_UNSUPPORTED;
152 
153 	memcpy(outbuffer, inbuffer, sizeof(float) * length * inchannels);
154 
155 	oaml.SetAudioFormat(state->samplerate, inchannels, 4, true);
156 	oaml.MixToBuffer(outbuffer, length * inchannels);
157 	oaml.Update();
158 
159 	return UNITY_AUDIODSP_OK;
160 }
161 
SetFloatParameterCallback(UnityAudioEffectState *,int,float)162 UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK SetFloatParameterCallback(UnityAudioEffectState*, int, float) {
163 	return UNITY_AUDIODSP_OK;
164 }
165 
GetFloatParameterCallback(UnityAudioEffectState *,int,float *,char *)166 UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK GetFloatParameterCallback(UnityAudioEffectState*, int, float*, char *) {
167 	return UNITY_AUDIODSP_OK;
168 }
169 
GetFloatBufferCallback(UnityAudioEffectState *,const char *,float *,int)170 int UNITY_AUDIODSP_CALLBACK GetFloatBufferCallback(UnityAudioEffectState*, const char*, float*, int) {
171 	return UNITY_AUDIODSP_OK;
172 }
173 
UnityGetAudioEffectDefinitions(UnityAudioEffectDefinition *** definitionptr)174 extern "C" UNITY_AUDIODSP_EXPORT_API int UnityGetAudioEffectDefinitions (UnityAudioEffectDefinition*** definitionptr) {
175 	static UnityAudioEffectDefinition definition;
176 	static UnityAudioEffectDefinition* definitionp[1];
177 
178 	memset(&definition, 0, sizeof(definition));
179 	strcpy(definition.name, "AudioPluginOAML");
180 	definition.structsize = sizeof(UnityAudioEffectDefinition);
181 	definition.paramstructsize = sizeof(UnityAudioParameterDefinition);
182 	definition.apiversion = UNITY_AUDIO_PLUGIN_API_VERSION;
183 	definition.pluginversion = 0x010000;
184 	definition.create = CreateCallback;
185 	definition.release = ReleaseCallback;
186 	definition.process = ProcessCallback;
187 	definition.setfloatparameter = SetFloatParameterCallback;
188 	definition.getfloatparameter = GetFloatParameterCallback;
189 	definition.getfloatbuffer = GetFloatBufferCallback;
190 
191 	definitionp[0] = &definition;
192 	*definitionptr = definitionp;
193 	return 1;
194 }
195