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-2005 by Christian Schoenebeck * |
* Copyright (C) 2003-2006 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 * |
285 |
|
|
286 |
pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); |
pCk3gix = waveList->GetSubChunk(CHUNK_ID_3GIX); |
287 |
if (pCk3gix) { |
if (pCk3gix) { |
288 |
SampleGroup = pCk3gix->ReadInt16(); |
uint16_t iSampleGroup = pCk3gix->ReadInt16(); |
289 |
|
pGroup = pFile->GetGroup(iSampleGroup); |
290 |
} else { // '3gix' chunk missing |
} else { // '3gix' chunk missing |
291 |
// use default value(s) |
// by default assigned to that mandatory "Default Group" |
292 |
SampleGroup = 0; |
pGroup = pFile->GetGroup(0); |
293 |
} |
} |
294 |
|
|
295 |
pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL); |
pCkSmpl = waveList->GetSubChunk(CHUNK_ID_SMPL); |
313 |
// use default values |
// use default values |
314 |
Manufacturer = 0; |
Manufacturer = 0; |
315 |
Product = 0; |
Product = 0; |
316 |
SamplePeriod = 1 / SamplesPerSecond; |
SamplePeriod = uint32_t(1000000000.0 / SamplesPerSecond + 0.5); |
317 |
MIDIUnityNote = 64; |
MIDIUnityNote = 64; |
318 |
FineTune = 0; |
FineTune = 0; |
319 |
SMPTEOffset = 0; |
SMPTEOffset = 0; |
399 |
// make sure '3gix' chunk exists |
// make sure '3gix' chunk exists |
400 |
pCk3gix = pWaveList->GetSubChunk(CHUNK_ID_3GIX); |
pCk3gix = pWaveList->GetSubChunk(CHUNK_ID_3GIX); |
401 |
if (!pCk3gix) pCk3gix = pWaveList->AddSubChunk(CHUNK_ID_3GIX, 4); |
if (!pCk3gix) pCk3gix = pWaveList->AddSubChunk(CHUNK_ID_3GIX, 4); |
402 |
|
// determine appropriate sample group index (to be stored in chunk) |
403 |
|
uint16_t iSampleGroup = 0; // 0 refers to default sample group |
404 |
|
File* pFile = static_cast<File*>(pParent); |
405 |
|
if (pFile->pGroups) { |
406 |
|
std::list<Group*>::iterator iter = pFile->pGroups->begin(); |
407 |
|
std::list<Group*>::iterator end = pFile->pGroups->end(); |
408 |
|
for (int i = 0; iter != end; i++, iter++) { |
409 |
|
if (*iter == pGroup) { |
410 |
|
iSampleGroup = i; |
411 |
|
break; // found |
412 |
|
} |
413 |
|
} |
414 |
|
} |
415 |
// update '3gix' chunk |
// update '3gix' chunk |
416 |
pData = (uint8_t*) pCk3gix->LoadChunkData(); |
pData = (uint8_t*) pCk3gix->LoadChunkData(); |
417 |
memcpy(&pData[0], &SampleGroup, 2); |
memcpy(&pData[0], &iSampleGroup, 2); |
418 |
} |
} |
419 |
|
|
420 |
/// Scans compressed samples for mandatory informations (e.g. actual number of total sample points). |
/// Scans compressed samples for mandatory informations (e.g. actual number of total sample points). |
1152 |
} |
} |
1153 |
} |
} |
1154 |
|
|
1155 |
|
/** |
1156 |
|
* Returns pointer to the Group this Sample belongs to. In the .gig |
1157 |
|
* format a sample always belongs to one group. If it wasn't explicitly |
1158 |
|
* assigned to a certain group, it will be automatically assigned to a |
1159 |
|
* default group. |
1160 |
|
* |
1161 |
|
* @returns Sample's Group (never NULL) |
1162 |
|
*/ |
1163 |
|
Group* Sample::GetGroup() const { |
1164 |
|
return pGroup; |
1165 |
|
} |
1166 |
|
|
1167 |
Sample::~Sample() { |
Sample::~Sample() { |
1168 |
Instances--; |
Instances--; |
1169 |
if (!Instances && InternalDecompressionBuffer.Size) { |
if (!Instances && InternalDecompressionBuffer.Size) { |
2695 |
|
|
2696 |
|
|
2697 |
|
|
2698 |
|
// *************** Group *************** |
2699 |
|
// * |
2700 |
|
|
2701 |
|
/** @brief Constructor. |
2702 |
|
* |
2703 |
|
* @param file - pointer to the gig::File object |
2704 |
|
* @param ck3gnm - pointer to 3gnm chunk associated with this group or |
2705 |
|
* NULL if this is a new Group |
2706 |
|
*/ |
2707 |
|
Group::Group(File* file, RIFF::Chunk* ck3gnm) { |
2708 |
|
pFile = file; |
2709 |
|
pNameChunk = ck3gnm; |
2710 |
|
::LoadString(pNameChunk, Name); |
2711 |
|
} |
2712 |
|
|
2713 |
|
Group::~Group() { |
2714 |
|
} |
2715 |
|
|
2716 |
|
/** @brief Update chunks with current group settings. |
2717 |
|
* |
2718 |
|
* Apply current Group field values to the respective. You have to call |
2719 |
|
* File::Save() to make changes persistent. |
2720 |
|
*/ |
2721 |
|
void Group::UpdateChunks() { |
2722 |
|
// make sure <3gri> and <3gnl> list chunks exist |
2723 |
|
RIFF::List* _3gri = pFile->pRIFF->GetSubList(LIST_TYPE_3GRI); |
2724 |
|
if (!_3gri) _3gri = pFile->pRIFF->AddSubList(LIST_TYPE_3GRI); |
2725 |
|
RIFF::List* _3gnl = _3gri->GetSubList(LIST_TYPE_3GNL); |
2726 |
|
if (!_3gnl) _3gnl = pFile->pRIFF->AddSubList(LIST_TYPE_3GNL); |
2727 |
|
// now store the name of this group as <3gnm> chunk as subchunk of the <3gnl> list chunk |
2728 |
|
::SaveString(CHUNK_ID_3GNM, pNameChunk, _3gnl, Name, String("Unnamed Group"), true, 64); |
2729 |
|
} |
2730 |
|
|
2731 |
|
/** |
2732 |
|
* Returns the first Sample of this Group. You have to call this method |
2733 |
|
* once before you use GetNextSample(). |
2734 |
|
* |
2735 |
|
* <b>Notice:</b> this method might block for a long time, in case the |
2736 |
|
* samples of this .gig file were not scanned yet |
2737 |
|
* |
2738 |
|
* @returns pointer address to first Sample or NULL if there is none |
2739 |
|
* applied to this Group |
2740 |
|
* @see GetNextSample() |
2741 |
|
*/ |
2742 |
|
Sample* Group::GetFirstSample() { |
2743 |
|
// FIXME: lazy und unsafe implementation, should be an autonomous iterator |
2744 |
|
for (Sample* pSample = pFile->GetFirstSample(); pSample; pSample = pFile->GetNextSample()) { |
2745 |
|
if (pSample->GetGroup() == this) return pSample; |
2746 |
|
} |
2747 |
|
return NULL; |
2748 |
|
} |
2749 |
|
|
2750 |
|
/** |
2751 |
|
* Returns the next Sample of the Group. You have to call |
2752 |
|
* GetFirstSample() once before you can use this method. By calling this |
2753 |
|
* method multiple times it iterates through the Samples assigned to |
2754 |
|
* this Group. |
2755 |
|
* |
2756 |
|
* @returns pointer address to the next Sample of this Group or NULL if |
2757 |
|
* end reached |
2758 |
|
* @see GetFirstSample() |
2759 |
|
*/ |
2760 |
|
Sample* Group::GetNextSample() { |
2761 |
|
// FIXME: lazy und unsafe implementation, should be an autonomous iterator |
2762 |
|
for (Sample* pSample = pFile->GetNextSample(); pSample; pSample = pFile->GetNextSample()) { |
2763 |
|
if (pSample->GetGroup() == this) return pSample; |
2764 |
|
} |
2765 |
|
return NULL; |
2766 |
|
} |
2767 |
|
|
2768 |
|
/** |
2769 |
|
* Move Sample given by \a pSample from another Group to this Group. |
2770 |
|
*/ |
2771 |
|
void Group::AddSample(Sample* pSample) { |
2772 |
|
pSample->pGroup = this; |
2773 |
|
} |
2774 |
|
|
2775 |
|
/** |
2776 |
|
* Move all members of this group to another group (preferably the 1st |
2777 |
|
* one except this). This method is called explicitly by |
2778 |
|
* File::DeleteGroup() thus when a Group was deleted. This code was |
2779 |
|
* intentionally not placed in the destructor! |
2780 |
|
*/ |
2781 |
|
void Group::MoveAll() { |
2782 |
|
// get "that" other group first |
2783 |
|
Group* pOtherGroup = NULL; |
2784 |
|
for (pOtherGroup = pFile->GetFirstGroup(); pOtherGroup; pOtherGroup = pFile->GetNextGroup()) { |
2785 |
|
if (pOtherGroup != this) break; |
2786 |
|
} |
2787 |
|
if (!pOtherGroup) throw Exception( |
2788 |
|
"Could not move samples to another group, since there is no " |
2789 |
|
"other Group. This is a bug, report it!" |
2790 |
|
); |
2791 |
|
// now move all samples of this group to the other group |
2792 |
|
for (Sample* pSample = GetFirstSample(); pSample; pSample = GetNextSample()) { |
2793 |
|
pOtherGroup->AddSample(pSample); |
2794 |
|
} |
2795 |
|
} |
2796 |
|
|
2797 |
|
|
2798 |
|
|
2799 |
// *************** File *************** |
// *************** File *************** |
2800 |
// * |
// * |
2801 |
|
|
2802 |
File::File() : DLS::File() { |
File::File() : DLS::File() { |
2803 |
|
pGroups = NULL; |
2804 |
pInfo->UseFixedLengthStrings = true; |
pInfo->UseFixedLengthStrings = true; |
2805 |
} |
} |
2806 |
|
|
2807 |
File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { |
File::File(RIFF::File* pRIFF) : DLS::File(pRIFF) { |
2808 |
|
pGroups = NULL; |
2809 |
pInfo->UseFixedLengthStrings = true; |
pInfo->UseFixedLengthStrings = true; |
2810 |
} |
} |
2811 |
|
|
2812 |
|
File::~File() { |
2813 |
|
if (pGroups) { |
2814 |
|
std::list<Group*>::iterator iter = pGroups->begin(); |
2815 |
|
std::list<Group*>::iterator end = pGroups->end(); |
2816 |
|
while (iter != end) { |
2817 |
|
delete *iter; |
2818 |
|
++iter; |
2819 |
|
} |
2820 |
|
delete pGroups; |
2821 |
|
} |
2822 |
|
} |
2823 |
|
|
2824 |
Sample* File::GetFirstSample(progress_t* pProgress) { |
Sample* File::GetFirstSample(progress_t* pProgress) { |
2825 |
if (!pSamples) LoadSamples(pProgress); |
if (!pSamples) LoadSamples(pProgress); |
2826 |
if (!pSamples) return NULL; |
if (!pSamples) return NULL; |
2873 |
} |
} |
2874 |
|
|
2875 |
void File::LoadSamples(progress_t* pProgress) { |
void File::LoadSamples(progress_t* pProgress) { |
2876 |
|
// Groups must be loaded before samples, because samples will try |
2877 |
|
// to resolve the group they belong to |
2878 |
|
LoadGroups(); |
2879 |
|
|
2880 |
if (!pSamples) pSamples = new SampleList; |
if (!pSamples) pSamples = new SampleList; |
2881 |
|
|
2882 |
RIFF::File* file = pRIFF; |
RIFF::File* file = pRIFF; |
3040 |
} |
} |
3041 |
} |
} |
3042 |
|
|
3043 |
|
Group* File::GetFirstGroup() { |
3044 |
|
if (!pGroups) LoadGroups(); |
3045 |
|
// there must always be at least one group |
3046 |
|
GroupsIterator = pGroups->begin(); |
3047 |
|
return *GroupsIterator; |
3048 |
|
} |
3049 |
|
|
3050 |
|
Group* File::GetNextGroup() { |
3051 |
|
if (!pGroups) return NULL; |
3052 |
|
++GroupsIterator; |
3053 |
|
return (GroupsIterator == pGroups->end()) ? NULL : *GroupsIterator; |
3054 |
|
} |
3055 |
|
|
3056 |
|
/** |
3057 |
|
* Returns the group with the given index. |
3058 |
|
* |
3059 |
|
* @param index - number of the sought group (0..n) |
3060 |
|
* @returns sought group or NULL if there's no such group |
3061 |
|
*/ |
3062 |
|
Group* File::GetGroup(uint index) { |
3063 |
|
if (!pGroups) LoadGroups(); |
3064 |
|
GroupsIterator = pGroups->begin(); |
3065 |
|
for (uint i = 0; GroupsIterator != pGroups->end(); i++) { |
3066 |
|
if (i == index) return *GroupsIterator; |
3067 |
|
++GroupsIterator; |
3068 |
|
} |
3069 |
|
return NULL; |
3070 |
|
} |
3071 |
|
|
3072 |
|
Group* File::AddGroup() { |
3073 |
|
if (!pGroups) LoadGroups(); |
3074 |
|
// there must always be at least one group |
3075 |
|
__ensureMandatoryChunksExist(); |
3076 |
|
Group* pGroup = new Group(this, NULL); |
3077 |
|
pGroups->push_back(pGroup); |
3078 |
|
return pGroup; |
3079 |
|
} |
3080 |
|
|
3081 |
|
void File::DeleteGroup(Group* pGroup) { |
3082 |
|
if (!pGroups) LoadGroups(); |
3083 |
|
std::list<Group*>::iterator iter = find(pGroups->begin(), pGroups->end(), pGroup); |
3084 |
|
if (iter == pGroups->end()) throw gig::Exception("Could not delete group, could not find given group"); |
3085 |
|
if (pGroups->size() == 1) throw gig::Exception("Cannot delete group, there must be at least one default group!"); |
3086 |
|
// move all members of this group to another group |
3087 |
|
pGroup->MoveAll(); |
3088 |
|
pGroups->erase(iter); |
3089 |
|
delete pGroup; |
3090 |
|
} |
3091 |
|
|
3092 |
|
void File::LoadGroups() { |
3093 |
|
if (!pGroups) pGroups = new std::list<Group*>; |
3094 |
|
// try to read defined groups from file |
3095 |
|
RIFF::List* lst3gri = pRIFF->GetSubList(LIST_TYPE_3GRI); |
3096 |
|
if (lst3gri) { |
3097 |
|
RIFF::List* lst3gnl = lst3gri->GetSubList(LIST_TYPE_3GNL); |
3098 |
|
if (lst3gnl) { |
3099 |
|
RIFF::Chunk* ck = lst3gnl->GetFirstSubChunk(); |
3100 |
|
while (ck) { |
3101 |
|
if (ck->GetChunkID() == CHUNK_ID_3GNM) { |
3102 |
|
pGroups->push_back(new Group(this, ck)); |
3103 |
|
} |
3104 |
|
ck = lst3gnl->GetNextSubChunk(); |
3105 |
|
} |
3106 |
|
} |
3107 |
|
} |
3108 |
|
// if there were no group(s), create at least the mandatory default group |
3109 |
|
if (!pGroups->size()) { |
3110 |
|
Group* pGroup = new Group(this, NULL); |
3111 |
|
pGroup->Name = "Default Group"; |
3112 |
|
pGroups->push_back(pGroup); |
3113 |
|
} |
3114 |
|
} |
3115 |
|
|
3116 |
|
|
3117 |
|
|
3118 |
// *************** Exception *************** |
// *************** Exception *************** |