| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- #include "config.h"
- #include <stdlib.h>
- #include <string.h>
- #include "alMain.h"
- #include "alMidi.h"
- #include "alError.h"
- #include "alThunk.h"
- #include "midi/base.h"
- extern inline struct ALsfpreset *LookupPreset(ALCdevice *device, ALuint id);
- extern inline struct ALsfpreset *RemovePreset(ALCdevice *device, ALuint id);
- static void ALsfpreset_Construct(ALsfpreset *self);
- void ALsfpreset_Destruct(ALsfpreset *self);
- AL_API void AL_APIENTRY alGenPresetsSOFT(ALsizei n, ALuint *ids)
- {
- ALCcontext *context;
- ALsizei cur = 0;
- context = GetContextRef();
- if(!context) return;
- if(!(n >= 0))
- SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
- for(cur = 0;cur < n;cur++)
- {
- ALsfpreset *preset = NewPreset(context);
- if(!preset)
- {
- alDeletePresetsSOFT(cur, ids);
- break;
- }
- ids[cur] = preset->id;
- }
- done:
- ALCcontext_DecRef(context);
- }
- AL_API ALvoid AL_APIENTRY alDeletePresetsSOFT(ALsizei n, const ALuint *ids)
- {
- ALCdevice *device;
- ALCcontext *context;
- ALsfpreset *preset;
- ALsizei i;
- context = GetContextRef();
- if(!context) return;
- if(!(n >= 0))
- SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
- device = context->Device;
- for(i = 0;i < n;i++)
- {
- /* Check for valid ID */
- if((preset=LookupPreset(device, ids[i])) == NULL)
- SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
- if(preset->ref != 0)
- SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
- }
- for(i = 0;i < n;i++)
- {
- if((preset=LookupPreset(device, ids[i])) == NULL)
- continue;
- DeletePreset(preset, device);
- }
- done:
- ALCcontext_DecRef(context);
- }
- AL_API ALboolean AL_APIENTRY alIsPresetSOFT(ALuint id)
- {
- ALCcontext *context;
- ALboolean ret;
- context = GetContextRef();
- if(!context) return AL_FALSE;
- ret = LookupPreset(context->Device, id) ? AL_TRUE : AL_FALSE;
- ALCcontext_DecRef(context);
- return ret;
- }
- AL_API void AL_APIENTRY alPresetiSOFT(ALuint id, ALenum param, ALint value)
- {
- ALCdevice *device;
- ALCcontext *context;
- ALsfpreset *preset;
- context = GetContextRef();
- if(!context) return;
- device = context->Device;
- if((preset=LookupPreset(device, id)) == NULL)
- SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
- if(preset->ref != 0)
- SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
- switch(param)
- {
- case AL_MIDI_PRESET_SOFT:
- if(!(value >= 0 && value <= 127))
- SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
- preset->Preset = value;
- break;
- case AL_MIDI_BANK_SOFT:
- if(!(value >= 0 && value <= 128))
- SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
- preset->Bank = value;
- break;
- default:
- SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
- }
- done:
- ALCcontext_DecRef(context);
- }
- AL_API void AL_APIENTRY alPresetivSOFT(ALuint id, ALenum param, const ALint *values)
- {
- ALCdevice *device;
- ALCcontext *context;
- ALsfpreset *preset;
- switch(param)
- {
- case AL_MIDI_PRESET_SOFT:
- case AL_MIDI_BANK_SOFT:
- alPresetiSOFT(id, param, values[0]);
- return;
- }
- context = GetContextRef();
- if(!context) return;
- device = context->Device;
- if((preset=LookupPreset(device, id)) == NULL)
- SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
- if(preset->ref != 0)
- SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
- switch(param)
- {
- default:
- SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
- }
- done:
- ALCcontext_DecRef(context);
- }
- AL_API void AL_APIENTRY alGetPresetivSOFT(ALuint id, ALenum param, ALint *values)
- {
- ALCdevice *device;
- ALCcontext *context;
- ALsfpreset *preset;
- ALsizei i;
- context = GetContextRef();
- if(!context) return;
- device = context->Device;
- if((preset=LookupPreset(device, id)) == NULL)
- SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
- switch(param)
- {
- case AL_MIDI_PRESET_SOFT:
- values[0] = preset->Preset;
- break;
- case AL_MIDI_BANK_SOFT:
- values[0] = preset->Bank;
- break;
- case AL_FONTSOUNDS_SIZE_SOFT:
- values[0] = preset->NumSounds;
- break;
- case AL_FONTSOUNDS_SOFT:
- for(i = 0;i < preset->NumSounds;i++)
- values[i] = preset->Sounds[i]->id;
- break;
- default:
- SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
- }
- done:
- ALCcontext_DecRef(context);
- }
- AL_API void AL_APIENTRY alPresetFontsoundsSOFT(ALuint id, ALsizei count, const ALuint *fsids)
- {
- ALCdevice *device;
- ALCcontext *context;
- ALsfpreset *preset;
- ALfontsound **sounds;
- ALsizei i;
- context = GetContextRef();
- if(!context) return;
- device = context->Device;
- if(!(preset=LookupPreset(device, id)))
- SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
- if(count < 0)
- SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
- if(preset->ref != 0)
- SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
- if(count == 0)
- sounds = NULL;
- else
- {
- sounds = calloc(count, sizeof(sounds[0]));
- if(!sounds)
- SET_ERROR_AND_GOTO(context, AL_OUT_OF_MEMORY, done);
- for(i = 0;i < count;i++)
- {
- if(!(sounds[i]=LookupFontsound(device, fsids[i])))
- {
- free(sounds);
- SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
- }
- }
- }
- for(i = 0;i < count;i++)
- IncrementRef(&sounds[i]->ref);
- sounds = ExchangePtr((XchgPtr*)&preset->Sounds, sounds);
- count = ExchangeInt(&preset->NumSounds, count);
- for(i = 0;i < count;i++)
- DecrementRef(&sounds[i]->ref);
- free(sounds);
- done:
- ALCcontext_DecRef(context);
- }
- ALsfpreset *NewPreset(ALCcontext *context)
- {
- ALCdevice *device = context->Device;
- ALsfpreset *preset;
- ALenum err;
- preset = calloc(1, sizeof(*preset));
- if(!preset)
- SET_ERROR_AND_RETURN_VALUE(context, AL_OUT_OF_MEMORY, NULL);
- ALsfpreset_Construct(preset);
- err = NewThunkEntry(&preset->id);
- if(err == AL_NO_ERROR)
- err = InsertUIntMapEntry(&device->PresetMap, preset->id, preset);
- if(err != AL_NO_ERROR)
- {
- ALsfpreset_Destruct(preset);
- memset(preset, 0, sizeof(*preset));
- free(preset);
- SET_ERROR_AND_RETURN_VALUE(context, err, NULL);
- }
- return preset;
- }
- void DeletePreset(ALsfpreset *preset, ALCdevice *device)
- {
- RemovePreset(device, preset->id);
- ALsfpreset_Destruct(preset);
- memset(preset, 0, sizeof(*preset));
- free(preset);
- }
- static void ALsfpreset_Construct(ALsfpreset *self)
- {
- self->ref = 0;
- self->Preset = 0;
- self->Bank = 0;
- self->Sounds = NULL;
- self->NumSounds = 0;
- self->id = 0;
- }
- void ALsfpreset_Destruct(ALsfpreset *self)
- {
- ALsizei i;
- FreeThunkEntry(self->id);
- self->id = 0;
- for(i = 0;i < self->NumSounds;i++)
- DecrementRef(&self->Sounds[i]->ref);
- free(self->Sounds);
- self->Sounds = NULL;
- self->NumSounds = 0;
- }
- /* ReleaseALPresets
- *
- * Called to destroy any presets that still exist on the device
- */
- void ReleaseALPresets(ALCdevice *device)
- {
- ALsizei i;
- for(i = 0;i < device->PresetMap.size;i++)
- {
- ALsfpreset *temp = device->PresetMap.array[i].value;
- device->PresetMap.array[i].value = NULL;
- ALsfpreset_Destruct(temp);
- memset(temp, 0, sizeof(*temp));
- free(temp);
- }
- }
|