1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader 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 |
|
// auto correct size field |
498 |
|
pNewLoops[SampleLoops].Size = sizeof(DLS::sample_loop_t); |
499 |
|
// free the old array and update the member variables |
500 |
|
if (SampleLoops) delete[] pSampleLoops; |
501 |
|
pSampleLoops = pNewLoops; |
502 |
|
SampleLoops++; |
503 |
|
} |
504 |
|
|
505 |
|
/** |
506 |
|
* Deletes an existing sample loop. |
507 |
|
* |
508 |
|
* @param pLoopDef - pointer to existing loop definition |
509 |
|
* @throws Exception - if given loop definition does not exist |
510 |
|
*/ |
511 |
|
void Sampler::DeleteSampleLoop(sample_loop_t* pLoopDef) { |
512 |
|
sample_loop_t* pNewLoops = new sample_loop_t[SampleLoops - 1]; |
513 |
|
// copy old loops array (skipping given loop) |
514 |
|
for (int i = 0, o = 0; i < SampleLoops; i++) { |
515 |
|
if (&pSampleLoops[i] == pLoopDef) continue; |
516 |
|
if (o == SampleLoops - 1) |
517 |
|
throw Exception("Could not delete Sample Loop, because it does not exist"); |
518 |
|
pNewLoops[o] = pSampleLoops[i]; |
519 |
|
o++; |
520 |
|
} |
521 |
|
// free the old array and update the member variables |
522 |
|
if (SampleLoops) delete[] pSampleLoops; |
523 |
|
pSampleLoops = pNewLoops; |
524 |
|
SampleLoops--; |
525 |
|
} |
526 |
|
|
527 |
|
|
528 |
|
|
529 |
// *************** Sample *************** |
// *************** Sample *************** |
557 |
AverageBytesPerSecond = pCkFormat->ReadUint32(); |
AverageBytesPerSecond = pCkFormat->ReadUint32(); |
558 |
BlockAlign = pCkFormat->ReadUint16(); |
BlockAlign = pCkFormat->ReadUint16(); |
559 |
// PCM format specific |
// PCM format specific |
560 |
if (FormatTag == WAVE_FORMAT_PCM) { |
if (FormatTag == DLS_WAVE_FORMAT_PCM) { |
561 |
BitDepth = pCkFormat->ReadUint16(); |
BitDepth = pCkFormat->ReadUint16(); |
562 |
FrameSize = (BitDepth / 8) * Channels; |
FrameSize = (BitDepth / 8) * Channels; |
563 |
} else { // unsupported sample data format |
} else { // unsupported sample data format |
565 |
FrameSize = 0; |
FrameSize = 0; |
566 |
} |
} |
567 |
} else { // 'fmt' chunk missing |
} else { // 'fmt' chunk missing |
568 |
FormatTag = WAVE_FORMAT_PCM; |
FormatTag = DLS_WAVE_FORMAT_PCM; |
569 |
BitDepth = 16; |
BitDepth = 16; |
570 |
Channels = 1; |
Channels = 1; |
571 |
SamplesPerSecond = 44100; |
SamplesPerSecond = 44100; |
573 |
FrameSize = (BitDepth / 8) * Channels; |
FrameSize = (BitDepth / 8) * Channels; |
574 |
BlockAlign = FrameSize; |
BlockAlign = FrameSize; |
575 |
} |
} |
576 |
SamplesTotal = (pCkData) ? (FormatTag == WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize |
SamplesTotal = (pCkData) ? (FormatTag == DLS_WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize |
577 |
: 0 |
: 0 |
578 |
: 0; |
: 0; |
579 |
} |
} |
580 |
|
|
634 |
* the RIFF chunk which encapsulates the sample's wave data. The |
* the RIFF chunk which encapsulates the sample's wave data. The |
635 |
* returned value is dependant to the current FrameSize value. |
* returned value is dependant to the current FrameSize value. |
636 |
* |
* |
637 |
* @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 |
638 |
* @see FrameSize, FormatTag |
* @see FrameSize, FormatTag |
639 |
*/ |
*/ |
640 |
unsigned long Sample::GetSize() { |
unsigned long Sample::GetSize() { |
641 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; |
642 |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
643 |
} |
} |
644 |
|
|
660 |
* calling File::Save() as this might exceed the current sample's |
* calling File::Save() as this might exceed the current sample's |
661 |
* boundary! |
* boundary! |
662 |
* |
* |
663 |
* Also note: only WAVE_FORMAT_PCM is currently supported, that is |
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
664 |
* FormatTag must be WAVE_FORMAT_PCM. Trying to resize samples with |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
665 |
* other formats will fail! |
* other formats will fail! |
666 |
* |
* |
667 |
* @param iNewSize - new sample wave data size in sample points (must be |
* @param iNewSize - new sample wave data size in sample points (must be |
668 |
* greater than zero) |
* greater than zero) |
669 |
* @throws Excecption if FormatTag != WAVE_FORMAT_PCM |
* @throws Excecption if FormatTag != DLS_WAVE_FORMAT_PCM |
670 |
* @throws Exception if \a iNewSize is less than 1 |
* @throws Exception if \a iNewSize is less than 1 |
671 |
* @see File::Save(), FrameSize, FormatTag |
* @see File::Save(), FrameSize, FormatTag |
672 |
*/ |
*/ |
673 |
void Sample::Resize(int iNewSize) { |
void Sample::Resize(int iNewSize) { |
674 |
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"); |
675 |
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"); |
676 |
const int iSizeInBytes = iNewSize * FrameSize; |
const int iSizeInBytes = iNewSize * FrameSize; |
677 |
pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); |
pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); |
684 |
* 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 |
685 |
* the sample into RAM, thus for disk streaming. |
* the sample into RAM, thus for disk streaming. |
686 |
* |
* |
687 |
* Also note: only WAVE_FORMAT_PCM is currently supported, that is |
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
688 |
* FormatTag must be WAVE_FORMAT_PCM. Trying to reposition the sample |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to reposition the sample |
689 |
* with other formats will fail! |
* with other formats will fail! |
690 |
* |
* |
691 |
* @param SampleCount number of sample points |
* @param SampleCount number of sample points |
692 |
* @param Whence to which relation \a SampleCount refers to |
* @param Whence to which relation \a SampleCount refers to |
693 |
* @returns new position within the sample, 0 if |
* @returns new position within the sample, 0 if |
694 |
* FormatTag != WAVE_FORMAT_PCM |
* FormatTag != DLS_WAVE_FORMAT_PCM |
695 |
* @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 |
696 |
* @see FrameSize, FormatTag |
* @see FrameSize, FormatTag |
697 |
*/ |
*/ |
698 |
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
699 |
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 |
700 |
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"); |
701 |
unsigned long orderedBytes = SampleCount * FrameSize; |
unsigned long orderedBytes = SampleCount * FrameSize; |
702 |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
714 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
715 |
*/ |
*/ |
716 |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
717 |
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 |
718 |
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? |
719 |
} |
} |
720 |
|
|
734 |
* @see LoadSampleData() |
* @see LoadSampleData() |
735 |
*/ |
*/ |
736 |
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
737 |
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 |
738 |
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"); |
739 |
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? |
740 |
} |
} |
743 |
* Apply sample and its settings to the respective RIFF chunks. You have |
* Apply sample and its settings to the respective RIFF chunks. You have |
744 |
* to call File::Save() to make changes persistent. |
* to call File::Save() to make changes persistent. |
745 |
* |
* |
746 |
* @throws Exception if FormatTag != WAVE_FORMAT_PCM or no sample data |
* @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data |
747 |
* was provided yet |
* was provided yet |
748 |
*/ |
*/ |
749 |
void Sample::UpdateChunks() { |
void Sample::UpdateChunks() { |
750 |
if (FormatTag != WAVE_FORMAT_PCM) |
if (FormatTag != DLS_WAVE_FORMAT_PCM) |
751 |
throw Exception("Could not save sample, only PCM format is supported"); |
throw Exception("Could not save sample, only PCM format is supported"); |
752 |
// 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 |
753 |
if (!pCkData) |
if (!pCkData) |
985 |
return pNewRegion; |
return pNewRegion; |
986 |
} |
} |
987 |
|
|
988 |
|
void Instrument::MoveRegion(Region* pSrc, Region* pDst) { |
989 |
|
RIFF::List* lrgn = pCkInstrument->GetSubList(LIST_TYPE_LRGN); |
990 |
|
lrgn->MoveSubChunk(pSrc->pCkRegion, pDst ? pDst->pCkRegion : 0); |
991 |
|
|
992 |
|
pRegions->remove(pSrc); |
993 |
|
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pDst); |
994 |
|
pRegions->insert(iter, pSrc); |
995 |
|
} |
996 |
|
|
997 |
void Instrument::DeleteRegion(Region* pRegion) { |
void Instrument::DeleteRegion(Region* pRegion) { |
998 |
if (!pRegions) return; |
if (!pRegions) return; |
999 |
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion); |
RegionList::iterator iter = find(pRegions->begin(), pRegions->end(), pRegion); |