/* Copyright (c) 2021 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "audiosynth/MusicInstrumentConfiguration.h" #include "audiosynth/MusicInstrument_drumlike.h" #include "audiosynth/MusicInstrument_pianolike.h" #include #define stemobject_implementation MusicInstrumentConfiguration stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); MusicInstrumentConfiguration * MusicInstrumentConfiguration_create(void) { stemobject_create_implementation(init) } bool MusicInstrumentConfiguration_init(MusicInstrumentConfiguration * self) { call_super(init, self); return true; } void MusicInstrumentConfiguration_dispose(MusicInstrumentConfiguration * self) { switch (self->instrumentType) { case INSTRUMENT_TYPE_PIANOLIKE: if (self->parameters.pianolike.configuration != NULL) { GeneralSynthConfiguration_dispose(self->parameters.pianolike.configuration); } break; case INSTRUMENT_TYPE_DRUMLIKE: for (unsigned int entryIndex = 0; entryIndex < self->parameters.drumlike.entryCount; entryIndex++) { if (self->parameters.drumlike.entries[entryIndex].configuration != NULL) { GeneralSynthConfiguration_dispose(self->parameters.drumlike.entries[entryIndex].configuration); } } free(self->parameters.drumlike.entries); break; } call_super_virtual(dispose, self); } MusicInstrumentConfiguration_instrumentType MusicInstrumentConfiguration_getMusicInstrumentType(MusicInstrument * instrument) { if (StemObject_isExactClass(instrument, &MusicInstrument_pianolike_class)) { return INSTRUMENT_TYPE_PIANOLIKE; } if (StemObject_isExactClass(instrument, &MusicInstrument_drumlike_class)) { return INSTRUMENT_TYPE_DRUMLIKE; } #ifdef DEBUG fprintf(stderr, "Warning: Unknown instrument class %p passed to MusicInstrumentConfiguration_getMusicInstrumentType\n", instrument->vtable); #endif return INSTRUMENT_TYPE_PIANOLIKE; } MusicInstrument * MusicInstrumentConfiguration_createMusicInstrument(MusicInstrumentConfiguration * self) { switch (self->instrumentType) { case INSTRUMENT_TYPE_PIANOLIKE: { MusicInstrument_pianolike * instrument = MusicInstrument_pianolike_create(); MusicInstrument_pianolike_setBaseSamplerParameters(instrument, GeneralSynthConfiguration_createAudioSamplerParameters(self->parameters.pianolike.configuration)); return (MusicInstrument *) instrument; } case INSTRUMENT_TYPE_DRUMLIKE: { MusicInstrument_drumlike * instrument = MusicInstrument_drumlike_create(); for (unsigned int entryIndex = 0; entryIndex < self->parameters.drumlike.entryCount; entryIndex++) { MusicInstrument_drumlike_setEntry(instrument, self->parameters.drumlike.entries[entryIndex].note, GeneralSynthConfiguration_createAudioSamplerParameters(self->parameters.drumlike.entries[entryIndex].configuration)); } return (MusicInstrument *) instrument; } } return NULL; } MusicInstrumentConfiguration * MusicInstrumentConfiguration_createFromMusicInstrument(MusicInstrument * instrument) { MusicInstrumentConfiguration * self = MusicInstrumentConfiguration_create(); self->instrumentType = MusicInstrumentConfiguration_getMusicInstrumentType(instrument); switch (self->instrumentType) { case INSTRUMENT_TYPE_PIANOLIKE: { MusicInstrument_pianolike * instrumentTyped = (MusicInstrument_pianolike *) instrument; self->parameters.pianolike.configuration = GeneralSynthConfiguration_createFromAudioSamplerParameters(instrumentTyped->baseSamplerParameters); break; } case INSTRUMENT_TYPE_DRUMLIKE: { MusicInstrument_drumlike * instrumentTyped = (MusicInstrument_drumlike *) instrument; self->parameters.drumlike.entryCount = instrumentTyped->entryCount; self->parameters.drumlike.entries = malloc(self->parameters.drumlike.entryCount * sizeof(*self->parameters.drumlike.entries)); for (unsigned int entryIndex = 0; entryIndex < self->parameters.drumlike.entryCount; entryIndex++) { self->parameters.drumlike.entries[entryIndex].note = instrumentTyped->entries[entryIndex].note; self->parameters.drumlike.entries[entryIndex].configuration = GeneralSynthConfiguration_createFromAudioSamplerParameters(instrumentTyped->entries[entryIndex].audioSamplerParameters); } break; } } return self; } MusicInstrumentConfiguration * MusicInstrumentConfiguration_deserialize(compat_type(DeserializationContext *) deserializationContext) { stemobject_deserialize_implementation(initFromSerializedData) } MusicInstrumentConfiguration * MusicInstrumentConfiguration_deserializeMinimal(compat_type(DeserializationContext *) deserializationContext, uint16_t formatVersion) { stemobject_deserialize_implementation(initFromSerializedDataMinimal, formatVersion) } #define INSTRUMENT_TYPE_ENUM_VALUES { \ {"pianolike", INSTRUMENT_TYPE_PIANOLIKE}, \ {"drumlike", INSTRUMENT_TYPE_DRUMLIKE} \ } static void loadSerializedDataMinimal(MusicInstrumentConfiguration * self, DeserializationContext * context, uint16_t formatVersion) { const uint16_t SYNTH_CONFIGURATION_FORMAT_VERSIONS[2] = {0, 1}; Serialization_enumKeyValue instrumentTypeValues[] = INSTRUMENT_TYPE_ENUM_VALUES; self->instrumentType = call_virtual(readEnumeration, context, "type", sizeof(instrumentTypeValues) / sizeof(instrumentTypeValues[0]), instrumentTypeValues); switch (self->instrumentType) { case INSTRUMENT_TYPE_PIANOLIKE: self->parameters.pianolike.configuration = GeneralSynthConfiguration_deserializeMinimal(context, SYNTH_CONFIGURATION_FORMAT_VERSIONS[formatVersion]); break; case INSTRUMENT_TYPE_DRUMLIKE: self->parameters.drumlike.entryCount = call_virtual(beginArray, context, "entries"); self->parameters.drumlike.entries = calloc(self->parameters.drumlike.entryCount, sizeof(*self->parameters.drumlike.entries)); for (unsigned int entryIndex = 0; entryIndex < self->parameters.drumlike.entryCount; entryIndex++) { call_virtual(beginStructure, context, NULL); self->parameters.drumlike.entries[entryIndex].note = call_virtual(readInt16, context, "note"); self->parameters.drumlike.entries[entryIndex].configuration = GeneralSynthConfiguration_deserializeMinimal(context, SYNTH_CONFIGURATION_FORMAT_VERSIONS[formatVersion]); call_virtual(endStructure, context); } call_virtual(endArray, context); break; } } bool MusicInstrumentConfiguration_initFromSerializedData(MusicInstrumentConfiguration * self, compat_type(DeserializationContext *) deserializationContext) { DeserializationContext * context = deserializationContext; call_virtual(beginStructure, context, MusicInstrumentConfiguration_formatType); const char * formatType = call_virtual(readString, context, "format_type"); if (formatType == NULL || strcmp(formatType, MusicInstrumentConfiguration_formatType)) { return false; } uint16_t formatVersion = call_virtual(readUInt16, context, "format_version"); if (formatVersion > MusicInstrumentConfiguration_formatVersion) { return false; } call_super(init, self); loadSerializedDataMinimal(self, context, formatVersion); call_virtual(endStructure, context); if (context->status != SERIALIZATION_ERROR_OK) { call_virtual(dispose, self); return false; } return true; } bool MusicInstrumentConfiguration_initFromSerializedDataMinimal(MusicInstrumentConfiguration * self, compat_type(DeserializationContext *) deserializationContext, uint16_t formatVersion) { DeserializationContext * context = deserializationContext; call_super(init, self); loadSerializedDataMinimal(self, context, formatVersion); if (context->status != SERIALIZATION_ERROR_OK) { call_virtual(dispose, self); return false; } return true; } void MusicInstrumentConfiguration_serialize(MusicInstrumentConfiguration * self, compat_type(SerializationContext *) serializationContext) { SerializationContext * context = serializationContext; call_virtual(beginStructure, context, MusicInstrumentConfiguration_formatType); call_virtual(writeString, context, "format_type", MusicInstrumentConfiguration_formatType); call_virtual(writeUInt16, context, "format_version", MusicInstrumentConfiguration_formatVersion); MusicInstrumentConfiguration_serializeMinimal(self, context); call_virtual(endStructure, context); } void MusicInstrumentConfiguration_serializeMinimal(MusicInstrumentConfiguration * self, compat_type(SerializationContext *) serializationContext) { SerializationContext * context = serializationContext; Serialization_enumKeyValue instrumentTypeValues[] = INSTRUMENT_TYPE_ENUM_VALUES; call_virtual(writeEnumeration, context, "type", self->instrumentType, sizeof(instrumentTypeValues) / sizeof(instrumentTypeValues[0]), instrumentTypeValues); switch (self->instrumentType) { case INSTRUMENT_TYPE_PIANOLIKE: GeneralSynthConfiguration_serializeMinimal(self->parameters.pianolike.configuration, context); break; case INSTRUMENT_TYPE_DRUMLIKE: call_virtual(beginArray, context, "entries"); for (unsigned int entryIndex = 0; entryIndex < self->parameters.drumlike.entryCount; entryIndex++) { call_virtual(beginStructure, context, NULL); call_virtual(writeInt16, context, "note", self->parameters.drumlike.entries[entryIndex].note); GeneralSynthConfiguration_serializeMinimal(self->parameters.drumlike.entries[entryIndex].configuration, context); call_virtual(endStructure, context); } call_virtual(endArray, context); break; } }