--- libgig/trunk/src/gig.cpp 2003/12/25 01:09:08 21 +++ libgig/trunk/src/gig.cpp 2004/09/05 00:46:28 231 @@ -2,8 +2,8 @@ * * * libgig - C++ cross-platform Gigasampler format file loader library * * * - * Copyright (C) 2003 by Christian Schoenebeck * - * * + * Copyright (C) 2003, 2004 by Christian Schoenebeck * + * * * * * This library is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -73,9 +73,7 @@ } FrameOffset = 0; // just for streaming compressed samples - LoopStart /= FrameSize; // convert to sample points - LoopEnd /= FrameSize; // convert to sample points - LoopSize = LoopEnd - LoopStart; + LoopSize = LoopEnd - LoopStart; } /// Scans compressed samples for mandatory informations (e.g. actual number of total sample points). @@ -315,6 +313,181 @@ } /** + * Reads \a SampleCount number of sample points from the position stored + * in \a pPlaybackState into the buffer pointed by \a pBuffer and moves + * the position within the sample respectively, this method honors the + * looping informations of the sample (if any). The sample wave stream + * will be decompressed on the fly if using a compressed sample. Use this + * method if you don't want to load the sample into RAM, thus for disk + * streaming. All this methods needs to know to proceed with streaming + * for the next time you call this method is stored in \a pPlaybackState. + * You have to allocate and initialize the playback_state_t structure by + * yourself before you use it to stream a sample: + * + * + * gig::playback_state_t playbackstate;
+ * playbackstate.position = 0;
+ * playbackstate.reverse = false;
+ * playbackstate.loop_cycles_left = pSample->LoopPlayCount;
+ *
+ * + * You don't have to take care of things like if there is actually a loop + * defined or if the current read position is located within a loop area. + * The method already handles such cases by itself. + * + * @param pBuffer destination buffer + * @param SampleCount number of sample points to read + * @param pPlaybackState will be used to store and reload the playback + * state for the next ReadAndLoop() call + * @returns number of successfully read sample points + */ + unsigned long Sample::ReadAndLoop(void* pBuffer, unsigned long SampleCount, playback_state_t* pPlaybackState) { + unsigned long samplestoread = SampleCount, totalreadsamples = 0, readsamples, samplestoloopend; + uint8_t* pDst = (uint8_t*) pBuffer; + + SetPos(pPlaybackState->position); // recover position from the last time + + if (this->Loops && GetPos() <= this->LoopEnd) { // honor looping if there are loop points defined + + switch (this->LoopType) { + + case loop_type_bidirectional: { //TODO: not tested yet! + do { + // if not endless loop check if max. number of loop cycles have been passed + if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; + + if (!pPlaybackState->reverse) { // forward playback + do { + samplestoloopend = this->LoopEnd - GetPos(); + readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); + samplestoread -= readsamples; + totalreadsamples += readsamples; + if (readsamples == samplestoloopend) { + pPlaybackState->reverse = true; + break; + } + } while (samplestoread && readsamples); + } + else { // backward playback + + // as we can only read forward from disk, we have to + // determine the end position within the loop first, + // read forward from that 'end' and finally after + // reading, swap all sample frames so it reflects + // backward playback + + unsigned long swapareastart = totalreadsamples; + unsigned long loopoffset = GetPos() - this->LoopStart; + unsigned long samplestoreadinloop = Min(samplestoread, loopoffset); + unsigned long reverseplaybackend = GetPos() - samplestoreadinloop; + + SetPos(reverseplaybackend); + + // read samples for backward playback + do { + readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoreadinloop); + samplestoreadinloop -= readsamples; + samplestoread -= readsamples; + totalreadsamples += readsamples; + } while (samplestoreadinloop && readsamples); + + SetPos(reverseplaybackend); // pretend we really read backwards + + if (reverseplaybackend == this->LoopStart) { + pPlaybackState->loop_cycles_left--; + pPlaybackState->reverse = false; + } + + // reverse the sample frames for backward playback + SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); + } + } while (samplestoread && readsamples); + break; + } + + case loop_type_backward: { // TODO: not tested yet! + // forward playback (not entered the loop yet) + if (!pPlaybackState->reverse) do { + samplestoloopend = this->LoopEnd - GetPos(); + readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); + samplestoread -= readsamples; + totalreadsamples += readsamples; + if (readsamples == samplestoloopend) { + pPlaybackState->reverse = true; + break; + } + } while (samplestoread && readsamples); + + if (!samplestoread) break; + + // as we can only read forward from disk, we have to + // determine the end position within the loop first, + // read forward from that 'end' and finally after + // reading, swap all sample frames so it reflects + // backward playback + + unsigned long swapareastart = totalreadsamples; + unsigned long loopoffset = GetPos() - this->LoopStart; + unsigned long samplestoreadinloop = (this->LoopPlayCount) ? Min(samplestoread, pPlaybackState->loop_cycles_left * LoopSize - loopoffset) + : samplestoread; + unsigned long reverseplaybackend = this->LoopStart + Abs((loopoffset - samplestoreadinloop) % this->LoopSize); + + SetPos(reverseplaybackend); + + // read samples for backward playback + do { + // if not endless loop check if max. number of loop cycles have been passed + if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; + samplestoloopend = this->LoopEnd - GetPos(); + readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoreadinloop, samplestoloopend)); + samplestoreadinloop -= readsamples; + samplestoread -= readsamples; + totalreadsamples += readsamples; + if (readsamples == samplestoloopend) { + pPlaybackState->loop_cycles_left--; + SetPos(this->LoopStart); + } + } while (samplestoreadinloop && readsamples); + + SetPos(reverseplaybackend); // pretend we really read backwards + + // reverse the sample frames for backward playback + SwapMemoryArea(&pDst[swapareastart * this->FrameSize], (totalreadsamples - swapareastart) * this->FrameSize, this->FrameSize); + break; + } + + default: case loop_type_normal: { + do { + // if not endless loop check if max. number of loop cycles have been passed + if (this->LoopPlayCount && !pPlaybackState->loop_cycles_left) break; + samplestoloopend = this->LoopEnd - GetPos(); + readsamples = Read(&pDst[totalreadsamples * this->FrameSize], Min(samplestoread, samplestoloopend)); + samplestoread -= readsamples; + totalreadsamples += readsamples; + if (readsamples == samplestoloopend) { + pPlaybackState->loop_cycles_left--; + SetPos(this->LoopStart); + } + } while (samplestoread && readsamples); + break; + } + } + } + + // read on without looping + if (samplestoread) do { + readsamples = Read(&pDst[totalreadsamples * this->FrameSize], samplestoread); + samplestoread -= readsamples; + totalreadsamples += readsamples; + } while (readsamples && samplestoread); + + // store current position + pPlaybackState->position = GetPos(); + + return totalreadsamples; + } + + /** * Reads \a SampleCount number of sample points from the current * position into the buffer pointed by \a pBuffer and increments the * position within the sample. The sample wave stream will be @@ -523,13 +696,13 @@ _3ewa->ReadInt16(); // unknown EG1Sustain = _3ewa->ReadUint16(); EG1Release = (double) GIG_EXP_DECODE(_3ewa->ReadInt32()); - EG1Controller = static_cast(_3ewa->ReadUint8()); + EG1Controller = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); uint8_t eg1ctrloptions = _3ewa->ReadUint8(); EG1ControllerInvert = eg1ctrloptions & 0x01; EG1ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg1ctrloptions); EG1ControllerDecayInfluence = GIG_EG_CTR_DECAY_INFLUENCE_EXTRACT(eg1ctrloptions); EG1ControllerReleaseInfluence = GIG_EG_CTR_RELEASE_INFLUENCE_EXTRACT(eg1ctrloptions); - EG2Controller = static_cast(_3ewa->ReadUint8()); + EG2Controller = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); uint8_t eg2ctrloptions = _3ewa->ReadUint8(); EG2ControllerInvert = eg2ctrloptions & 0x01; EG2ControllerAttackInfluence = GIG_EG_CTR_ATTACK_INFLUENCE_EXTRACT(eg2ctrloptions); @@ -591,7 +764,7 @@ ReleaseVelocityResponseDepth = 0; } VelocityResponseCurveScaling = _3ewa->ReadUint8(); - AttenuationControlTreshold = _3ewa->ReadInt8(); + AttenuationControllerThreshold = _3ewa->ReadInt8(); _3ewa->ReadInt32(); // unknown SampleStartOffset = (uint16_t) _3ewa->ReadInt16(); _3ewa->ReadInt16(); // unknown @@ -607,12 +780,12 @@ uint8_t lfo3ctrl = _3ewa->ReadUint8(); LFO3Controller = static_cast(lfo3ctrl & 0x07); // lower 3 bits LFO3Sync = lfo3ctrl & 0x20; // bit 5 - InvertAttenuationControl = lfo3ctrl & 0x80; // bit 7 + InvertAttenuationController = lfo3ctrl & 0x80; // bit 7 if (VCFType == vcf_type_lowpass) { if (lfo3ctrl & 0x40) // bit 6 VCFType = vcf_type_lowpassturbo; } - AttenuationControl = static_cast(_3ewa->ReadUint8()); + AttenuationController = DecodeLeverageController(static_cast<_lev_ctrl_t>(_3ewa->ReadUint8())); uint8_t lfo2ctrl = _3ewa->ReadUint8(); LFO2Controller = static_cast(lfo2ctrl & 0x07); // lower 3 bits LFO2FlipPhase = lfo2ctrl & 0x80; // bit 7 @@ -668,25 +841,25 @@ case curve_type_nonlinear: for (int velocity = 0; velocity < 128; velocity++) { pVelocityAttenuationTable[velocity] = - GIG_VELOCITY_TRANSFORM_NONLINEAR((double)(velocity+1),(double)(VelocityResponseDepth+1),(double)VelocityResponseCurveScaling); - if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; - else if (pVelocityAttenuationTable[velocity] < 0.0) pVelocityAttenuationTable[velocity] = 0.0; + GIG_VELOCITY_TRANSFORM_NONLINEAR(((double)velocity),((double)VelocityResponseDepth),((double)VelocityResponseCurveScaling)); + if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; + else if (pVelocityAttenuationTable[velocity] < 1e-15) pVelocityAttenuationTable[velocity] = 0.0; } break; case curve_type_linear: for (int velocity = 0; velocity < 128; velocity++) { pVelocityAttenuationTable[velocity] = - GIG_VELOCITY_TRANSFORM_LINEAR((double)velocity,(double)(VelocityResponseDepth+1),(double)VelocityResponseCurveScaling); - if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; - else if (pVelocityAttenuationTable[velocity] < 0.0) pVelocityAttenuationTable[velocity] = 0.0; + GIG_VELOCITY_TRANSFORM_LINEAR(((double)velocity),((double)VelocityResponseDepth),((double)VelocityResponseCurveScaling)); + if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; + else if (pVelocityAttenuationTable[velocity] < 1e-15) pVelocityAttenuationTable[velocity] = 0.0; } break; case curve_type_special: for (int velocity = 0; velocity < 128; velocity++) { pVelocityAttenuationTable[velocity] = - GIG_VELOCITY_TRANSFORM_SPECIAL((double)(velocity+1),(double)(VelocityResponseDepth+1),(double)VelocityResponseCurveScaling); - if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; - else if (pVelocityAttenuationTable[velocity] < 0.0) pVelocityAttenuationTable[velocity] = 0.0; + GIG_VELOCITY_TRANSFORM_SPECIAL(((double)velocity),((double)VelocityResponseDepth),((double)VelocityResponseCurveScaling)); + if (pVelocityAttenuationTable[velocity] > 1.0) pVelocityAttenuationTable[velocity] = 1.0; + else if (pVelocityAttenuationTable[velocity] < 1e-15) pVelocityAttenuationTable[velocity] = 0.0; } break; case curve_type_unknown: @@ -697,6 +870,124 @@ } } + leverage_ctrl_t DimensionRegion::DecodeLeverageController(_lev_ctrl_t EncodedController) { + leverage_ctrl_t decodedcontroller; + switch (EncodedController) { + // special controller + case _lev_ctrl_none: + decodedcontroller.type = leverage_ctrl_t::type_none; + decodedcontroller.controller_number = 0; + break; + case _lev_ctrl_velocity: + decodedcontroller.type = leverage_ctrl_t::type_velocity; + decodedcontroller.controller_number = 0; + break; + case _lev_ctrl_channelaftertouch: + decodedcontroller.type = leverage_ctrl_t::type_channelaftertouch; + decodedcontroller.controller_number = 0; + break; + + // ordinary MIDI control change controller + case _lev_ctrl_modwheel: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 1; + break; + case _lev_ctrl_breath: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 2; + break; + case _lev_ctrl_foot: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 4; + break; + case _lev_ctrl_effect1: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 12; + break; + case _lev_ctrl_effect2: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 13; + break; + case _lev_ctrl_genpurpose1: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 16; + break; + case _lev_ctrl_genpurpose2: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 17; + break; + case _lev_ctrl_genpurpose3: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 18; + break; + case _lev_ctrl_genpurpose4: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 19; + break; + case _lev_ctrl_portamentotime: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 5; + break; + case _lev_ctrl_sustainpedal: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 64; + break; + case _lev_ctrl_portamento: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 65; + break; + case _lev_ctrl_sostenutopedal: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 66; + break; + case _lev_ctrl_softpedal: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 67; + break; + case _lev_ctrl_genpurpose5: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 80; + break; + case _lev_ctrl_genpurpose6: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 81; + break; + case _lev_ctrl_genpurpose7: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 82; + break; + case _lev_ctrl_genpurpose8: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 83; + break; + case _lev_ctrl_effect1depth: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 91; + break; + case _lev_ctrl_effect2depth: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 92; + break; + case _lev_ctrl_effect3depth: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 93; + break; + case _lev_ctrl_effect4depth: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 94; + break; + case _lev_ctrl_effect5depth: + decodedcontroller.type = leverage_ctrl_t::type_controlchange; + decodedcontroller.controller_number = 95; + break; + + // unknown controller type + default: + throw gig::Exception("Unknown leverage controller type."); + } + return decodedcontroller; + } + DimensionRegion::~DimensionRegion() { Instances--; if (!Instances) { @@ -720,8 +1011,8 @@ * triggered to get the volume with which the sample should be played * back. * - * @param MIDI velocity value of the triggered key (between 0 and 127) - * @returns amplitude factor (between 0.0 and 1.0) + * @param MIDIKeyVelocity MIDI velocity value of the triggered key (between 0 and 127) + * @returns amplitude factor (between 0.0 and 1.0) */ double DimensionRegion::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) { return pVelocityAttenuationTable[MIDIKeyVelocity];