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-2014 by Christian Schoenebeck * |
* Copyright (C) 2003-2015 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 * |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
// *************** progress_t *************** |
58 |
|
// * |
59 |
|
|
60 |
|
progress_t::progress_t() { |
61 |
|
callback = NULL; |
62 |
|
custom = NULL; |
63 |
|
__range_min = 0.0f; |
64 |
|
__range_max = 1.0f; |
65 |
|
} |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
// *************** Chunk ************** |
// *************** Chunk ************** |
70 |
// * |
// * |
71 |
|
|
302 |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
303 |
#if POSIX |
#if POSIX |
304 |
if (lseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET) < 0) return 0; |
if (lseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET) < 0) return 0; |
305 |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
ssize_t readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
306 |
if (readWords < 1) return 0; |
if (readWords < 1) { |
307 |
|
#if DEBUG |
308 |
|
std::cerr << "POSIX read() failed: " << strerror(errno) << std::endl << std::flush; |
309 |
|
#endif // DEBUG |
310 |
|
return 0; |
311 |
|
} |
312 |
readWords /= WordSize; |
readWords /= WordSize; |
313 |
#elif defined(WIN32) |
#elif defined(WIN32) |
314 |
if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0; |
if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0; |
318 |
readWords /= WordSize; |
readWords /= WordSize; |
319 |
#else // standard C functions |
#else // standard C functions |
320 |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
321 |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
size_t readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
322 |
#endif // POSIX |
#endif // POSIX |
323 |
if (!pFile->bEndianNative && WordSize != 1) { |
if (!pFile->bEndianNative && WordSize != 1) { |
324 |
switch (WordSize) { |
switch (WordSize) { |
856 |
* chunk should be written to |
* chunk should be written to |
857 |
* @param ulCurrentDataOffset - offset of current (old) data within |
* @param ulCurrentDataOffset - offset of current (old) data within |
858 |
* the file |
* the file |
859 |
|
* @param pProgress - optional: callback function for progress notification |
860 |
* @returns new write position in the "physical" file, that is |
* @returns new write position in the "physical" file, that is |
861 |
* \a ulWritePos incremented by this chunk's new size |
* \a ulWritePos incremented by this chunk's new size |
862 |
* (including its header size of course) |
* (including its header size of course) |
863 |
*/ |
*/ |
864 |
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset, progress_t* pProgress) { |
865 |
const unsigned long ulOriginalPos = ulWritePos; |
const unsigned long ulOriginalPos = ulWritePos; |
866 |
ulWritePos += CHUNK_HEADER_SIZE; |
ulWritePos += CHUNK_HEADER_SIZE; |
867 |
|
|
927 |
CurrentChunkSize = NewChunkSize; |
CurrentChunkSize = NewChunkSize; |
928 |
WriteHeader(ulOriginalPos); |
WriteHeader(ulOriginalPos); |
929 |
|
|
930 |
|
__notify_progress(pProgress, 1.0); // notify done |
931 |
|
|
932 |
// update chunk's position pointers |
// update chunk's position pointers |
933 |
ulStartPos = ulOriginalPos + CHUNK_HEADER_SIZE; |
ulStartPos = ulOriginalPos + CHUNK_HEADER_SIZE; |
934 |
ulPos = 0; |
ulPos = 0; |
1356 |
#endif // POSIX |
#endif // POSIX |
1357 |
} |
} |
1358 |
|
|
1359 |
void List::LoadSubChunks() { |
void List::LoadSubChunks(progress_t* pProgress) { |
1360 |
#if DEBUG |
#if DEBUG |
1361 |
std::cout << "List::LoadSubChunks()"; |
std::cout << "List::LoadSubChunks()"; |
1362 |
#endif // DEBUG |
#endif // DEBUG |
1391 |
} |
} |
1392 |
SetPos(uiOriginalPos); // restore position before this call |
SetPos(uiOriginalPos); // restore position before this call |
1393 |
} |
} |
1394 |
|
__notify_progress(pProgress, 1.0); // notify done |
1395 |
} |
} |
1396 |
|
|
1397 |
void List::LoadSubChunksRecursively() { |
void List::LoadSubChunksRecursively(progress_t* pProgress) { |
1398 |
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList()) |
const int n = CountSubLists(); |
1399 |
pList->LoadSubChunksRecursively(); |
int i = 0; |
1400 |
|
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) { |
1401 |
|
// divide local progress into subprogress |
1402 |
|
progress_t subprogress; |
1403 |
|
__divide_progress(pProgress, &subprogress, n, i); |
1404 |
|
// do the actual work |
1405 |
|
pList->LoadSubChunksRecursively(&subprogress); |
1406 |
|
} |
1407 |
|
__notify_progress(pProgress, 1.0); // notify done |
1408 |
} |
} |
1409 |
|
|
1410 |
/** @brief Write list chunk persistently e.g. to disk. |
/** @brief Write list chunk persistently e.g. to disk. |
1417 |
* list chunk should be written to |
* list chunk should be written to |
1418 |
* @param ulCurrentDataOffset - offset of current (old) data within |
* @param ulCurrentDataOffset - offset of current (old) data within |
1419 |
* the file |
* the file |
1420 |
|
* @param pProgress - optional: callback function for progress notification |
1421 |
* @returns new write position in the "physical" file, that is |
* @returns new write position in the "physical" file, that is |
1422 |
* \a ulWritePos incremented by this list chunk's new size |
* \a ulWritePos incremented by this list chunk's new size |
1423 |
* (including its header size of course) |
* (including its header size of course) |
1424 |
*/ |
*/ |
1425 |
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset, progress_t* pProgress) { |
1426 |
const unsigned long ulOriginalPos = ulWritePos; |
const unsigned long ulOriginalPos = ulWritePos; |
1427 |
ulWritePos += LIST_HEADER_SIZE; |
ulWritePos += LIST_HEADER_SIZE; |
1428 |
|
|
1431 |
|
|
1432 |
// write all subchunks (including sub list chunks) recursively |
// write all subchunks (including sub list chunks) recursively |
1433 |
if (pSubChunks) { |
if (pSubChunks) { |
1434 |
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
int i = 0; |
1435 |
ulWritePos = (*iter)->WriteChunk(ulWritePos, ulCurrentDataOffset); |
const int n = pSubChunks->size(); |
1436 |
|
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) { |
1437 |
|
// divide local progress into subprogress for loading current Instrument |
1438 |
|
progress_t subprogress; |
1439 |
|
__divide_progress(pProgress, &subprogress, n, i); |
1440 |
|
// do the actual work |
1441 |
|
ulWritePos = (*iter)->WriteChunk(ulWritePos, ulCurrentDataOffset, &subprogress); |
1442 |
} |
} |
1443 |
} |
} |
1444 |
|
|
1449 |
// offset of this list chunk in new written file may have changed |
// offset of this list chunk in new written file may have changed |
1450 |
ulStartPos = ulOriginalPos + LIST_HEADER_SIZE; |
ulStartPos = ulOriginalPos + LIST_HEADER_SIZE; |
1451 |
|
|
1452 |
|
__notify_progress(pProgress, 1.0); // notify done |
1453 |
|
|
1454 |
return ulWritePos; |
return ulWritePos; |
1455 |
} |
} |
1456 |
|
|
1475 |
// *************** File *************** |
// *************** File *************** |
1476 |
// * |
// * |
1477 |
|
|
|
//HACK: to avoid breaking DLL compatibility to older versions of libgig we roll the new std::set<Chunk*> into the old std::list<Chunk*> container, should be replaced on member variable level soon though |
|
|
#define _GET_RESIZED_CHUNKS() \ |
|
|
(reinterpret_cast<std::set<Chunk*>*>(ResizedChunks.front())) |
|
|
|
|
1478 |
/** @brief Create new RIFF file. |
/** @brief Create new RIFF file. |
1479 |
* |
* |
1480 |
* Use this constructor if you want to create a new RIFF file completely |
* Use this constructor if you want to create a new RIFF file completely |
1492 |
File::File(uint32_t FileType) |
File::File(uint32_t FileType) |
1493 |
: List(this), bIsNewFile(true), Layout(layout_standard) |
: List(this), bIsNewFile(true), Layout(layout_standard) |
1494 |
{ |
{ |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
|
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
|
1495 |
#if defined(WIN32) |
#if defined(WIN32) |
1496 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1497 |
#else |
#else |
1579 |
* given RIFF file or RIFF-alike file |
* given RIFF file or RIFF-alike file |
1580 |
*/ |
*/ |
1581 |
void File::__openExistingFile(const String& path, uint32_t* FileType) { |
void File::__openExistingFile(const String& path, uint32_t* FileType) { |
1582 |
//HACK: see _GET_RESIZED_CHUNKS() comment |
ResizedChunks.clear(); |
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
|
1583 |
#if POSIX |
#if POSIX |
1584 |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1585 |
if (hFileRead == -1) { |
if (hFileRead == -1) { |
1775 |
* than it will have at the end of the saving process, in case chunks |
* than it will have at the end of the saving process, in case chunks |
1776 |
* were grown. |
* were grown. |
1777 |
* |
* |
1778 |
|
* @param pProgress - optional: callback function for progress notification |
1779 |
* @throws RIFF::Exception if there is an empty chunk or empty list |
* @throws RIFF::Exception if there is an empty chunk or empty list |
1780 |
* chunk or any kind of IO error occured |
* chunk or any kind of IO error occured |
1781 |
*/ |
*/ |
1782 |
void File::Save() { |
void File::Save(progress_t* pProgress) { |
1783 |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1784 |
if (Layout == layout_flat) |
if (Layout == layout_flat) |
1785 |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1786 |
|
|
1787 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1788 |
LoadSubChunksRecursively(); |
{ |
1789 |
|
// divide progress into subprogress |
1790 |
|
progress_t subprogress; |
1791 |
|
__divide_progress(pProgress, &subprogress, 3.f, 0.f); // arbitrarily subdivided into 1/3 of total progress |
1792 |
|
// do the actual work |
1793 |
|
LoadSubChunksRecursively(&subprogress); |
1794 |
|
// notify subprogress done |
1795 |
|
__notify_progress(&subprogress, 1.f); |
1796 |
|
} |
1797 |
|
|
1798 |
// reopen file in write mode |
// reopen file in write mode |
1799 |
SetMode(stream_mode_read_write); |
SetMode(stream_mode_read_write); |
1807 |
|
|
1808 |
// first we sum up all positive chunk size changes (and skip all negative ones) |
// first we sum up all positive chunk size changes (and skip all negative ones) |
1809 |
unsigned long ulPositiveSizeDiff = 0; |
unsigned long ulPositiveSizeDiff = 0; |
1810 |
std::set<Chunk*>* resizedChunks = _GET_RESIZED_CHUNKS(); |
for (std::set<Chunk*>::const_iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
|
for (std::set<Chunk*>::const_iterator iter = resizedChunks->begin(), end = resizedChunks->end(); iter != end; ++iter) { |
|
1811 |
if ((*iter)->GetNewSize() == 0) { |
if ((*iter)->GetNewSize() == 0) { |
1812 |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
1813 |
} |
} |
1820 |
|
|
1821 |
// if there are positive size changes... |
// if there are positive size changes... |
1822 |
if (ulPositiveSizeDiff > 0) { |
if (ulPositiveSizeDiff > 0) { |
1823 |
|
// divide progress into subprogress |
1824 |
|
progress_t subprogress; |
1825 |
|
__divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress |
1826 |
|
|
1827 |
// ... we enlarge this file first ... |
// ... we enlarge this file first ... |
1828 |
ulWorkingFileSize += ulPositiveSizeDiff; |
ulWorkingFileSize += ulPositiveSizeDiff; |
1829 |
ResizeFile(ulWorkingFileSize); |
ResizeFile(ulWorkingFileSize); |
1835 |
#else |
#else |
1836 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
1837 |
#endif |
#endif |
1838 |
for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { |
for (unsigned long ulPos = ulFileSize, iNotif = 0; iBytesMoved > 0; ++iNotif) { |
1839 |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
1840 |
ulPos -= iBytesMoved; |
ulPos -= iBytesMoved; |
1841 |
#if POSIX |
#if POSIX |
1854 |
fseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
fseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1855 |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite); |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite); |
1856 |
#endif |
#endif |
1857 |
|
if (!(iNotif % 8) && iBytesMoved > 0) |
1858 |
|
__notify_progress(&subprogress, float(ulFileSize - ulPos) / float(ulFileSize)); |
1859 |
} |
} |
1860 |
delete[] pCopyBuffer; |
delete[] pCopyBuffer; |
1861 |
if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it"); |
if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it"); |
1862 |
|
|
1863 |
|
__notify_progress(&subprogress, 1.f); // notify subprogress done |
1864 |
} |
} |
1865 |
|
|
1866 |
// rebuild / rewrite complete RIFF tree |
// rebuild / rewrite complete RIFF tree ... |
1867 |
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff); |
|
1868 |
|
// divide progress into subprogress |
1869 |
|
progress_t subprogress; |
1870 |
|
__divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress |
1871 |
|
// do the actual work |
1872 |
|
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff, &subprogress); |
1873 |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1874 |
|
// notify subprogress done |
1875 |
|
__notify_progress(&subprogress, 1.f); |
1876 |
|
|
1877 |
// resize file to the final size |
// resize file to the final size |
1878 |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1879 |
|
|
1880 |
// forget all resized chunks |
// forget all resized chunks |
1881 |
resizedChunks->clear(); |
ResizedChunks.clear(); |
1882 |
|
|
1883 |
|
__notify_progress(pProgress, 1.0); // notify done |
1884 |
} |
} |
1885 |
|
|
1886 |
/** @brief Save changes to another file. |
/** @brief Save changes to another file. |
1895 |
* the new file (given by \a path) afterwards. |
* the new file (given by \a path) afterwards. |
1896 |
* |
* |
1897 |
* @param path - path and file name where everything should be written to |
* @param path - path and file name where everything should be written to |
1898 |
|
* @param pProgress - optional: callback function for progress notification |
1899 |
*/ |
*/ |
1900 |
void File::Save(const String& path) { |
void File::Save(const String& path, progress_t* pProgress) { |
1901 |
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case |
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case |
1902 |
|
|
1903 |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1905 |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1906 |
|
|
1907 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1908 |
LoadSubChunksRecursively(); |
{ |
1909 |
|
// divide progress into subprogress |
1910 |
|
progress_t subprogress; |
1911 |
|
__divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 1/2 of total progress |
1912 |
|
// do the actual work |
1913 |
|
LoadSubChunksRecursively(&subprogress); |
1914 |
|
// notify subprogress done |
1915 |
|
__notify_progress(&subprogress, 1.f); |
1916 |
|
} |
1917 |
|
|
1918 |
if (!bIsNewFile) SetMode(stream_mode_read); |
if (!bIsNewFile) SetMode(stream_mode_read); |
1919 |
// open the other (new) file for writing and truncate it to zero size |
// open the other (new) file for writing and truncate it to zero size |
1944 |
Mode = stream_mode_read_write; |
Mode = stream_mode_read_write; |
1945 |
|
|
1946 |
// write complete RIFF tree to the other (new) file |
// write complete RIFF tree to the other (new) file |
1947 |
unsigned long ulTotalSize = WriteChunk(0, 0); |
unsigned long ulTotalSize; |
1948 |
|
{ |
1949 |
|
// divide progress into subprogress |
1950 |
|
progress_t subprogress; |
1951 |
|
__divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 1/2 of total progress |
1952 |
|
// do the actual work |
1953 |
|
ulTotalSize = WriteChunk(0, 0, &subprogress); |
1954 |
|
// notify subprogress done |
1955 |
|
__notify_progress(&subprogress, 1.f); |
1956 |
|
} |
1957 |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1958 |
|
|
1959 |
// resize file to the final size (if the file was originally larger) |
// resize file to the final size (if the file was originally larger) |
1960 |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1961 |
|
|
1962 |
// forget all resized chunks |
// forget all resized chunks |
1963 |
_GET_RESIZED_CHUNKS()->clear(); |
ResizedChunks.clear(); |
1964 |
|
|
1965 |
#if POSIX |
#if POSIX |
1966 |
if (hFileWrite) close(hFileWrite); |
if (hFileWrite) close(hFileWrite); |
1976 |
bIsNewFile = false; |
bIsNewFile = false; |
1977 |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
1978 |
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
1979 |
|
|
1980 |
|
__notify_progress(pProgress, 1.0); // notify done |
1981 |
} |
} |
1982 |
|
|
1983 |
void File::ResizeFile(unsigned long ulNewSize) { |
void File::ResizeFile(unsigned long ulNewSize) { |
2020 |
#endif // POSIX |
#endif // POSIX |
2021 |
DeleteChunkList(); |
DeleteChunkList(); |
2022 |
pFile = NULL; |
pFile = NULL; |
2023 |
//HACK: see _GET_RESIZED_CHUNKS() comment |
ResizedChunks.clear(); |
|
delete _GET_RESIZED_CHUNKS(); |
|
2024 |
} |
} |
2025 |
|
|
2026 |
void File::LogAsResized(Chunk* pResizedChunk) { |
void File::LogAsResized(Chunk* pResizedChunk) { |
2027 |
_GET_RESIZED_CHUNKS()->insert(pResizedChunk); |
ResizedChunks.insert(pResizedChunk); |
2028 |
} |
} |
2029 |
|
|
2030 |
void File::UnlogResized(Chunk* pResizedChunk) { |
void File::UnlogResized(Chunk* pResizedChunk) { |
2031 |
_GET_RESIZED_CHUNKS()->erase(pResizedChunk); |
ResizedChunks.erase(pResizedChunk); |
2032 |
} |
} |
2033 |
|
|
2034 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |