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 * |
514 |
AverageBytesPerSecond = pCkFormat->ReadUint32(); |
AverageBytesPerSecond = pCkFormat->ReadUint32(); |
515 |
BlockAlign = pCkFormat->ReadUint16(); |
BlockAlign = pCkFormat->ReadUint16(); |
516 |
// PCM format specific |
// PCM format specific |
517 |
if (FormatTag == WAVE_FORMAT_PCM) { |
if (FormatTag == DLS_WAVE_FORMAT_PCM) { |
518 |
BitDepth = pCkFormat->ReadUint16(); |
BitDepth = pCkFormat->ReadUint16(); |
519 |
FrameSize = (BitDepth / 8) * Channels; |
FrameSize = (BitDepth / 8) * Channels; |
520 |
} else { // unsupported sample data format |
} else { // unsupported sample data format |
522 |
FrameSize = 0; |
FrameSize = 0; |
523 |
} |
} |
524 |
} else { // 'fmt' chunk missing |
} else { // 'fmt' chunk missing |
525 |
FormatTag = WAVE_FORMAT_PCM; |
FormatTag = DLS_WAVE_FORMAT_PCM; |
526 |
BitDepth = 16; |
BitDepth = 16; |
527 |
Channels = 1; |
Channels = 1; |
528 |
SamplesPerSecond = 44100; |
SamplesPerSecond = 44100; |
530 |
FrameSize = (BitDepth / 8) * Channels; |
FrameSize = (BitDepth / 8) * Channels; |
531 |
BlockAlign = FrameSize; |
BlockAlign = FrameSize; |
532 |
} |
} |
533 |
SamplesTotal = (pCkData) ? (FormatTag == WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize |
SamplesTotal = (pCkData) ? (FormatTag == DLS_WAVE_FORMAT_PCM) ? pCkData->GetSize() / FrameSize |
534 |
: 0 |
: 0 |
535 |
: 0; |
: 0; |
536 |
} |
} |
537 |
|
|
591 |
* the RIFF chunk which encapsulates the sample's wave data. The |
* the RIFF chunk which encapsulates the sample's wave data. The |
592 |
* returned value is dependant to the current FrameSize value. |
* returned value is dependant to the current FrameSize value. |
593 |
* |
* |
594 |
* @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 |
595 |
* @see FrameSize, FormatTag |
* @see FrameSize, FormatTag |
596 |
*/ |
*/ |
597 |
unsigned long Sample::GetSize() { |
unsigned long Sample::GetSize() { |
598 |
if (FormatTag != WAVE_FORMAT_PCM) return 0; |
if (FormatTag != DLS_WAVE_FORMAT_PCM) return 0; |
599 |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
return (pCkData) ? pCkData->GetSize() / FrameSize : 0; |
600 |
} |
} |
601 |
|
|
617 |
* calling File::Save() as this might exceed the current sample's |
* calling File::Save() as this might exceed the current sample's |
618 |
* boundary! |
* boundary! |
619 |
* |
* |
620 |
* Also note: only WAVE_FORMAT_PCM is currently supported, that is |
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
621 |
* FormatTag must be WAVE_FORMAT_PCM. Trying to resize samples with |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to resize samples with |
622 |
* other formats will fail! |
* other formats will fail! |
623 |
* |
* |
624 |
* @param iNewSize - new sample wave data size in sample points (must be |
* @param iNewSize - new sample wave data size in sample points (must be |
625 |
* greater than zero) |
* greater than zero) |
626 |
* @throws Excecption if FormatTag != WAVE_FORMAT_PCM |
* @throws Excecption if FormatTag != DLS_WAVE_FORMAT_PCM |
627 |
* @throws Exception if \a iNewSize is less than 1 |
* @throws Exception if \a iNewSize is less than 1 |
628 |
* @see File::Save(), FrameSize, FormatTag |
* @see File::Save(), FrameSize, FormatTag |
629 |
*/ |
*/ |
630 |
void Sample::Resize(int iNewSize) { |
void Sample::Resize(int iNewSize) { |
631 |
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"); |
632 |
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"); |
633 |
const int iSizeInBytes = iNewSize * FrameSize; |
const int iSizeInBytes = iNewSize * FrameSize; |
634 |
pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); |
pCkData = pWaveList->GetSubChunk(CHUNK_ID_DATA); |
641 |
* 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 |
642 |
* the sample into RAM, thus for disk streaming. |
* the sample into RAM, thus for disk streaming. |
643 |
* |
* |
644 |
* Also note: only WAVE_FORMAT_PCM is currently supported, that is |
* Also note: only DLS_WAVE_FORMAT_PCM is currently supported, that is |
645 |
* FormatTag must be WAVE_FORMAT_PCM. Trying to reposition the sample |
* FormatTag must be DLS_WAVE_FORMAT_PCM. Trying to reposition the sample |
646 |
* with other formats will fail! |
* with other formats will fail! |
647 |
* |
* |
648 |
* @param SampleCount number of sample points |
* @param SampleCount number of sample points |
649 |
* @param Whence to which relation \a SampleCount refers to |
* @param Whence to which relation \a SampleCount refers to |
650 |
* @returns new position within the sample, 0 if |
* @returns new position within the sample, 0 if |
651 |
* FormatTag != WAVE_FORMAT_PCM |
* FormatTag != DLS_WAVE_FORMAT_PCM |
652 |
* @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 |
653 |
* @see FrameSize, FormatTag |
* @see FrameSize, FormatTag |
654 |
*/ |
*/ |
655 |
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
unsigned long Sample::SetPos(unsigned long SampleCount, RIFF::stream_whence_t Whence) { |
656 |
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 |
657 |
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"); |
658 |
unsigned long orderedBytes = SampleCount * FrameSize; |
unsigned long orderedBytes = SampleCount * FrameSize; |
659 |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
unsigned long result = pCkData->SetPos(orderedBytes, Whence); |
671 |
* @param SampleCount number of sample points to read |
* @param SampleCount number of sample points to read |
672 |
*/ |
*/ |
673 |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
unsigned long Sample::Read(void* pBuffer, unsigned long SampleCount) { |
674 |
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 |
675 |
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? |
676 |
} |
} |
677 |
|
|
691 |
* @see LoadSampleData() |
* @see LoadSampleData() |
692 |
*/ |
*/ |
693 |
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
unsigned long Sample::Write(void* pBuffer, unsigned long SampleCount) { |
694 |
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 |
695 |
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"); |
696 |
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? |
697 |
} |
} |
700 |
* Apply sample and its settings to the respective RIFF chunks. You have |
* Apply sample and its settings to the respective RIFF chunks. You have |
701 |
* to call File::Save() to make changes persistent. |
* to call File::Save() to make changes persistent. |
702 |
* |
* |
703 |
* @throws Exception if FormatTag != WAVE_FORMAT_PCM or no sample data |
* @throws Exception if FormatTag != DLS_WAVE_FORMAT_PCM or no sample data |
704 |
* was provided yet |
* was provided yet |
705 |
*/ |
*/ |
706 |
void Sample::UpdateChunks() { |
void Sample::UpdateChunks() { |
707 |
if (FormatTag != WAVE_FORMAT_PCM) |
if (FormatTag != DLS_WAVE_FORMAT_PCM) |
708 |
throw Exception("Could not save sample, only PCM format is supported"); |
throw Exception("Could not save sample, only PCM format is supported"); |
709 |
// 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 |
710 |
if (!pCkData) |
if (!pCkData) |