2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file access library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2006 by Christian Schoenebeck * |
* Copyright (C) 2003-2007 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
* <cuse@users.sourceforge.net> * |
7 |
* * |
* * |
8 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
352 |
SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""), UseFixedLengthStrings, 128); |
SaveString(CHUNK_ID_ITCH, lstINFO, Technician, String(""), UseFixedLengthStrings, 128); |
353 |
SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware, UseFixedLengthStrings, |
SaveString(CHUNK_ID_ISFT, lstINFO, Software, defaultSoftware, UseFixedLengthStrings, |
354 |
resourceType == LIST_TYPE_INS ? |
resourceType == LIST_TYPE_INS ? |
355 |
(Software == "" ? defaultSoftware.length() : Software.length()) : 128); |
(Software == "" ? defaultSoftware.length()+1 : Software.length()+1) : 128); |
356 |
SaveString(CHUNK_ID_IMED, lstINFO, Medium, String(""), UseFixedLengthStrings, 128); |
SaveString(CHUNK_ID_IMED, lstINFO, Medium, String(""), UseFixedLengthStrings, 128); |
357 |
SaveString(CHUNK_ID_ISRC, lstINFO, Source, String(""), UseFixedLengthStrings, 128); |
SaveString(CHUNK_ID_ISRC, lstINFO, Source, String(""), UseFixedLengthStrings, 128); |
358 |
SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""), UseFixedLengthStrings, 128); |
SaveString(CHUNK_ID_ISRF, lstINFO, SourceForm, String(""), UseFixedLengthStrings, 128); |
481 |
} |
} |
482 |
} |
} |
483 |
|
|
484 |
|
/** |
485 |
|
* Adds a new sample loop with the provided loop definition. |
486 |
|
* |
487 |
|
* @param - points to a loop definition that is to be copied |
488 |
|
*/ |
489 |
|
void Sampler::AddSampleLoop(sample_loop_t* pLoopDef) { |
490 |
|
sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops + 1]; |
491 |
|
// copy old loops array |
492 |
|
for (int i = 0; i < SampleLoops; i++) { |
493 |
|
pNewLoops[i] = pSampleLoops[i]; |
494 |
|
} |
495 |
|
// add the new loop |
496 |
|
pNewLoops[SampleLoops] = *pLoopDef; |
497 |
|
// free the old array and update the member variables |
498 |
|
if (SampleLoops) delete[] pSampleLoops; |
499 |
|
pSampleLoops = pNewLoops; |
500 |
|
SampleLoops++; |
501 |
|
} |
502 |
|
|
503 |
|
/** |
504 |
|
* Deletes an existing sample loop. |
505 |
|
* |
506 |
|
* @param pLoopDef - pointer to existing loop definition |
507 |
|
* @throws Exception - if given loop definition does not exist |
508 |
|
*/ |
509 |
|
void Sampler::DeleteSampleLoop(sample_loop_t* pLoopDef) { |
510 |
|
sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops - 1]; |
511 |
|
// copy old loops array (skipping given loop) |
512 |
|
for (int i = 0, o = 0; i < SampleLoops; i++) { |
513 |
|
if (&pSampleLoops[i] == pLoopDef) continue; |
514 |
|
if (o == SampleLoops - 1) |
515 |
|
throw Exception("Could not delete Sample Loop, because it does not exist"); |
516 |
|
pNewLoops[o] = pSampleLoops[i]; |
517 |
|
o++; |
518 |
|
} |
519 |
|
// free the old array and update the member variables |
520 |
|
if (SampleLoops) delete[] pSampleLoops; |
521 |
|
pSampleLoops = pNewLoops; |
522 |
|
SampleLoops--; |
523 |
|
} |
524 |
|
|
525 |
|
|
526 |
|
|
527 |
// *************** Sample *************** |
// *************** Sample *************** |
555 |
AverageBytesPerSecond = pCkFormat->ReadUint32(); |
AverageBytesPerSecond = pCkFormat->ReadUint32(); |
556 |
BlockAlign = pCkFormat->ReadUint16(); |
BlockAlign = pCkFormat->ReadUint16(); |
557 |
// PCM format specific |
// PCM format specific |
558 |
if (FormatTag == WAVE_FORMAT_PCM) { |
if (FormatTag == DLS_WAVE_FORMAT_PCM) { |
559 |
BitDepth = pCkFormat->ReadUint16(); |
BitDepth = pCkFormat->ReadUint16(); |
560 |
FrameSize = (BitDepth / 8) * Channels; |
FrameSize = (BitDepth / 8) * Channels; |
561 |
} else { // unsupported sample data format |
} else { // unsupported sample data format |
563 |
FrameSize = 0; |
FrameSize = 0; |
564 |
} |
} |
565 |
} else { // 'fmt' chunk missing |
} else { // 'fmt' chunk missing |
566 |
FormatTag = WAVE_FORMAT_PCM; |
FormatTag = DLS_WAVE_FORMAT_PCM; |
567 |
BitDepth = 16; |
BitDepth = 16; |
568 |
Channels = 1; |
Channels = 1; |
569 |
SamplesPerSecond = 44100; |
SamplesPerSecond = 44100; |
571 |
FrameSize = (BitDepth / 8) * Channels; |
FrameSize = (BitDepth / 8) * Channels; |
572 |
BlockAlign = FrameSize; |
BlockAlign = FrameSize; |
573 |
} |
} |
574 |
SamplesTotal = (pCkData) ? (FormatTag == WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize |
SamplesTotal = (pCkData) ? (FormatTag == DLS_WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize |
575 |
: 0 |
: 0 |
576 |
: 0; |
: 0; |
577 |
} |
} |
578 |
|
|
632 |
* the RIFF chunk which encapsulates the sample's wave data. The |
* the RIFF chunk which encapsulates the sample's wave data. The |
633 |
* returned value is dependant to the current FrameSize value. |
* returned value is dependant to the current FrameSize value. |
634 |
* |
* |
635 |
* @returns number of sample points or 0 if FormatTag != WAVE_FORMAT_PCM |
* @returns number of sample points or 0 if FormatTag != DLS_WAVE_FORMAT_PCM |
636 |
* @see FrameSize, FormatTag |
* @see FrameSize, FormatTag |
637 |
*/ |
*/ |
638 |
unsigned long Sample::GetSize() { |
unsigned long Sample::GetSize() { |
639 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; |
640 |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
641 |
} |
} |
642 |
|
|
658 |
* calling File::Save() as this might exceed the current sample's |
* calling File::Save() as this might exceed the current sample's |
659 |
* boundary! |
* boundary! |
660 |
* |
* |
661 |
* Also note: only WAVE_FORMAT_PCM is currently supported, that is |
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
662 |
* FormatTag must be WAVE_FORMAT_PCM. Trying to resize samples with |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
663 |
* other formats will fail! |
* other formats will fail! |
664 |
* |
* |
665 |
* @param iNewSize - new sample wave data size in sample points (must be |
* @param iNewSize - new sample wave data size in sample points (must be |
666 |
* greater than zero) |
* greater than zero) |
667 |
* @throws Excecption if FormatTag != WAVE_FORMAT_PCM |
* @throws Excecption if FormatTag != DLS_WAVE_FORMAT_PCM |
668 |
* @throws Exception if \a iNewSize is less than 1 |
* @throws Exception if \a iNewSize is less than 1 |
669 |
* @see File::Save(), FrameSize, FormatTag |
* @see File::Save(), FrameSize, FormatTag |
670 |
*/ |
*/ |
671 |
void Sample::Resize(int iNewSize) { |
void Sample::Resize(int iNewSize) { |
672 |
if (FormatTag != WAVE_FORMAT_PCM) throw Exception("Sample's format is not WAVE_FORMAT_PCM"); |
if (FormatTag != DLS_WAVE_FORMAT_PCM) throw Exception("Sample's format is not DLS_WAVE_FORMAT_PCM"); |
673 |
if (iNewSize < 1) throw Exception("Sample size must be at least one sample point"); |
if (iNewSize < 1) throw Exception("Sample size must be at least one sample point"); |
674 |
const int iSizeInBytes = iNewSize * FrameSize; |
const int iSizeInBytes = iNewSize * FrameSize; |
675 |
pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); |
pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); |
682 |
* bytes). Use this method and <i>Read()</i> if you don't want to load |
* bytes). Use this method and <i>Read()</i> if you don't want to load |
683 |
* the sample into RAM, thus for disk streaming. |
* the sample into RAM, thus for disk streaming. |
684 |
* |
* |
685 |
* Also note: only WAVE_FORMAT_PCM is currently supported, that is |
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
686 |
* FormatTag must be WAVE_FORMAT_PCM. Trying to reposition the sample |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to reposition the sample |
687 |
* with other formats will fail! |
* with other formats will fail! |
688 |
* |
* |
689 |
* @param SampleCount number of sample points |
* @param SampleCount number of sample points |
690 |
* @param Whence to which relation \a SampleCount refers to |
* @param Whence to which relation \a SampleCount refers to |
691 |
* @returns new position within the sample, 0 if |
* @returns new position within the sample, 0 if |
692 |
* FormatTag != WAVE_FORMAT_PCM |
* FormatTag != DLS_WAVE_FORMAT_PCM |
693 |
* @throws Exception if no data RIFF chunk was created for the sample yet |
* @throws Exception if no data RIFF chunk was created for the sample yet |
694 |
* @see FrameSize, FormatTag |
* @see FrameSize, FormatTag |
695 |
*/ |
*/ |
696 |
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
697 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
698 |
if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one"); |
if (!pCkData) throw Exception("No data chunk created for sample yet, call Sample::Resize() to create one"); |
699 |
unsigned long orderedBytes = SampleCount * FrameSize; |
unsigned long orderedBytes = SampleCount * FrameSize; |
700 |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
712 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
713 |
*/ |
*/ |
714 |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
715 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
716 |
return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
return pCkData->Read(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
717 |
} |
} |
718 |
|
|
732 |
* @see LoadSampleData() |
* @see LoadSampleData() |
733 |
*/ |
*/ |
734 |
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
735 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; // failed: wave data not PCM format |
736 |
if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small"); |
if (GetSize() < SampleCount) throw Exception("Could not write sample data, current sample size to small"); |
737 |
return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
return pCkData->Write(pBuffer, SampleCount, FrameSize); // FIXME: channel inversion due to endian correction? |
738 |
} |
} |
741 |
* Apply sample and its settings to the respective RIFF chunks. You have |
* Apply sample and its settings to the respective RIFF chunks. You have |
742 |
* to call File::Save() to make changes persistent. |
* to call File::Save() to make changes persistent. |
743 |
* |
* |
744 |
* @throws Exception if FormatTag != WAVE_FORMAT_PCM or no sample data |
* @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data |
745 |
* was provided yet |
* was provided yet |
746 |
*/ |
*/ |
747 |
void Sample::UpdateChunks() { |
void Sample::UpdateChunks() { |
748 |
if (FormatTag != WAVE_FORMAT_PCM) |
if (FormatTag != DLS_WAVE_FORMAT_PCM) |
749 |
throw Exception("Could not save sample, only PCM format is supported"); |
throw Exception("Could not save sample, only PCM format is supported"); |
750 |
// we refuse to do anything if not sample wave form was provided yet |
// we refuse to do anything if not sample wave form was provided yet |
751 |
if (!pCkData) |
if (!pCkData) |
983 |
return pNewRegion; |
return pNewRegion; |
984 |
} |
} |
985 |
|
|
986 |
|
void Instrument::MoveRegion(Region* pSrc, Region* pDst) { |
987 |
|
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
988 |
|
lrgn->MoveSubChunk(pSrc->pCkRegion, pDst ? pDst->pCkRegion : 0); |
989 |
|
|
990 |
|
pRegions->remove(pSrc); |
991 |
|
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst); |
992 |
|
pRegions->insert(iter, pSrc); |
993 |
|
} |
994 |
|
|
995 |
void Instrument::DeleteRegion(Region* pRegion) { |
void Instrument::DeleteRegion(Region* pRegion) { |
996 |
if (!pRegions) return; |
if (!pRegions) return; |
997 |
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion); |
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion); |