/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* MIDITempo implementation */

#include "sles_allinclusive.h"

static SLresult IMIDITempo_SetTicksPerQuarterNote(SLMIDITempoItf self, SLuint32 tpqn)
{
    if (!(1 <= tpqn && tpqn <= 32767))
        return SL_RESULT_PARAMETER_INVALID;
    IMIDITempo *this = (IMIDITempo *) self;
    interface_lock_poke(this);
    this->mTicksPerQuarterNote = tpqn;
    interface_unlock_poke(this);
    return SL_RESULT_SUCCESS;
}

static SLresult IMIDITempo_GetTicksPerQuarterNote(SLMIDITempoItf self, SLuint32 *pTpqn)
{
    if (NULL == pTpqn)
        return SL_RESULT_PARAMETER_INVALID;
    IMIDITempo *this = (IMIDITempo *) self;
    interface_lock_peek(this);
    SLuint32 ticksPerQuarterNote = this->mTicksPerQuarterNote;
    interface_unlock_peek(this);
    *pTpqn = ticksPerQuarterNote;
    return SL_RESULT_SUCCESS;
}

static SLresult IMIDITempo_SetMicrosecondsPerQuarterNote(SLMIDITempoItf self, SLmicrosecond uspqn)
{
    // FIXME spec says zero, is that correct?
    if (!(1 <= uspqn && uspqn <= 16777215))
        return SL_RESULT_PARAMETER_INVALID;
    IMIDITempo *this = (IMIDITempo *) self;
    interface_lock_poke(this);
    this->mMicrosecondsPerQuarterNote = uspqn;
    interface_unlock_poke(this);
    return SL_RESULT_SUCCESS;
}

static SLresult IMIDITempo_GetMicrosecondsPerQuarterNote(SLMIDITempoItf self, SLmicrosecond *uspqn)
{
    if (NULL == uspqn)
        return SL_RESULT_PARAMETER_INVALID;
    IMIDITempo *this = (IMIDITempo *) self;
    interface_lock_peek(this);
    SLuint32 microsecondsPerQuarterNote = this->mMicrosecondsPerQuarterNote;
    interface_unlock_peek(this);
    *uspqn = microsecondsPerQuarterNote;
    return SL_RESULT_SUCCESS;
}

static const struct SLMIDITempoItf_ IMIDITempo_Itf = {
    IMIDITempo_SetTicksPerQuarterNote,
    IMIDITempo_GetTicksPerQuarterNote,
    IMIDITempo_SetMicrosecondsPerQuarterNote,
    IMIDITempo_GetMicrosecondsPerQuarterNote
};

void IMIDITempo_init(void *self)
{
    IMIDITempo *this = (IMIDITempo *) self;
    this->mItf = &IMIDITempo_Itf;
    this->mTicksPerQuarterNote = 32; // FIXME
    this->mMicrosecondsPerQuarterNote = 100; // FIXME
}
