/[svn]/libgig/trunk/src/RIFF.cpp
ViewVC logotype

Diff of /libgig/trunk/src/RIFF.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3412 by schoenebeck, Wed Jan 24 00:01:19 2018 UTC revision 3901 by schoenebeck, Wed May 12 17:35:38 2021 UTC
# Line 2  Line 2 
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-2018 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2021 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  *
# Line 64  namespace RIFF { Line 64  namespace RIFF {
64          __range_max = 1.0f;          __range_max = 1.0f;
65      }      }
66    
67        /**
68         * Divides this progress task into the requested amount of equal weighted
69         * sub-progress tasks and returns a vector with those subprogress tasks.
70         *
71         * @param iSubtasks - total amount sub tasks this task should be subdivided
72         * @returns subtasks
73         */
74        std::vector<progress_t> progress_t::subdivide(int iSubtasks) {
75            std::vector<progress_t> v;
76            for (int i = 0; i < iSubtasks; ++i) {
77                progress_t p;
78                __divide_progress(this, &p, iSubtasks, i);
79                v.push_back(p);
80            }
81            return v;
82        }
83    
84        /**
85         * Divides this progress task into the requested amount of sub-progress
86         * tasks, where each one of those new sub-progress tasks is created with its
87         * requested individual weight / portion, and finally returns a vector
88         * with those new subprogress tasks.
89         *
90         * The amount of subprogresses to be created is determined by this method
91         * by calling @c vSubTaskPortions.size() .
92         *
93         * Example: consider you wanted to create 3 subprogresses where the 1st
94         * subtask should be assigned 10% of the new 3 subprogresses' overall
95         * progress, the 2nd subtask should be assigned 50% of the new 3
96         * subprogresses' overall progress, and the 3rd subtask should be assigned
97         * 40%, then you might call this method like this:
98         * @code
99         * std::vector<progress_t> subprogresses = progress.subdivide({0.1, 0.5, 0.4});
100         * @endcode
101         *
102         * @param vSubTaskPortions - amount and individual weight of subtasks to be
103         *                           created
104         * @returns subtasks
105         */
106        std::vector<progress_t> progress_t::subdivide(std::vector<float> vSubTaskPortions) {
107            float fTotal = 0.f; // usually 1.0, but we sum the portions up below to be sure
108            for (int i = 0; i < vSubTaskPortions.size(); ++i)
109                fTotal += vSubTaskPortions[i];
110    
111            float fLow = 0.f, fHigh = 0.f;
112            std::vector<progress_t> v;
113            for (int i = 0; i < vSubTaskPortions.size(); ++i) {
114                fLow  = fHigh;
115                fHigh = vSubTaskPortions[i];
116                progress_t p;
117                __divide_progress(this, &p, fTotal, fLow, fHigh);
118                v.push_back(p);
119            }
120            return v;
121        }
122    
123    
124    
125  // *************** Chunk **************  // *************** Chunk **************
# Line 838  namespace RIFF { Line 894  namespace RIFF {
894              uint8_t* pNewBuffer = new uint8_t[ullNewChunkSize];              uint8_t* pNewBuffer = new uint8_t[ullNewChunkSize];
895              if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(ullNewChunkSize) + " bytes");              if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(ullNewChunkSize) + " bytes");
896              memset(pNewBuffer, 0 , ullNewChunkSize);              memset(pNewBuffer, 0 , ullNewChunkSize);
897              memcpy(pNewBuffer, pChunkData, ullChunkDataSize);              if (pChunkData) {
898              delete[] pChunkData;                  memcpy(pNewBuffer, pChunkData, ullChunkDataSize);
899                    delete[] pChunkData;
900                }
901              pChunkData       = pNewBuffer;              pChunkData       = pNewBuffer;
902              ullChunkDataSize = ullNewChunkSize;              ullChunkDataSize = ullNewChunkSize;
903          }          }
# Line 970  namespace RIFF { Line 1028  namespace RIFF {
1028          ullCurrentChunkSize = ullNewChunkSize;          ullCurrentChunkSize = ullNewChunkSize;
1029          WriteHeader(ullOriginalPos);          WriteHeader(ullOriginalPos);
1030    
1031          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
1032                __notify_progress(pProgress, 1.0); // notify done
1033    
1034          // update chunk's position pointers          // update chunk's position pointers
1035          ullStartPos = ullOriginalPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);          ullStartPos = ullOriginalPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
# Line 1457  namespace RIFF { Line 1516  namespace RIFF {
1516              }              }
1517              SetPos(ullOriginalPos); // restore position before this call              SetPos(ullOriginalPos); // restore position before this call
1518          }          }
1519          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
1520                __notify_progress(pProgress, 1.0); // notify done
1521      }      }
1522    
1523      void List::LoadSubChunksRecursively(progress_t* pProgress) {      void List::LoadSubChunksRecursively(progress_t* pProgress) {
1524          const int n = (int) CountSubLists();          const int n = (int) CountSubLists();
1525          int i = 0;          int i = 0;
1526          for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) {          for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) {
1527              // divide local progress into subprogress              if (pProgress) {
1528              progress_t subprogress;                  // divide local progress into subprogress
1529              __divide_progress(pProgress, &subprogress, n, i);                  progress_t subprogress;
1530              // do the actual work                  __divide_progress(pProgress, &subprogress, n, i);
1531              pList->LoadSubChunksRecursively(&subprogress);                  // do the actual work
1532                    pList->LoadSubChunksRecursively(&subprogress);
1533                } else
1534                    pList->LoadSubChunksRecursively(NULL);
1535          }          }
1536          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
1537                __notify_progress(pProgress, 1.0); // notify done
1538      }      }
1539    
1540      /** @brief Write list chunk persistently e.g. to disk.      /** @brief Write list chunk persistently e.g. to disk.
# Line 1500  namespace RIFF { Line 1564  namespace RIFF {
1564              size_t i = 0;              size_t i = 0;
1565              const size_t n = pSubChunks->size();              const size_t n = pSubChunks->size();
1566              for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) {              for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) {
1567                  // divide local progress into subprogress for loading current Instrument                  if (pProgress) {
1568                  progress_t subprogress;                      // divide local progress into subprogress for loading current Instrument
1569                  __divide_progress(pProgress, &subprogress, n, i);                      progress_t subprogress;
1570                  // do the actual work                      __divide_progress(pProgress, &subprogress, n, i);
1571                  ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, &subprogress);                      // do the actual work
1572                        ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, &subprogress);
1573                    } else
1574                        ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, NULL);
1575              }              }
1576          }          }
1577    
# Line 1515  namespace RIFF { Line 1582  namespace RIFF {
1582          // offset of this list chunk in new written file may have changed          // offset of this list chunk in new written file may have changed
1583          ullStartPos = ullOriginalPos + LIST_HEADER_SIZE(pFile->FileOffsetSize);          ullStartPos = ullOriginalPos + LIST_HEADER_SIZE(pFile->FileOffsetSize);
1584    
1585           __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
1586                __notify_progress(pProgress, 1.0); // notify done
1587    
1588          return ullWritePos;          return ullWritePos;
1589      }      }
# Line 1864  namespace RIFF { Line 1932  namespace RIFF {
1932              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");
1933    
1934          // make sure the RIFF tree is built (from the original file)          // make sure the RIFF tree is built (from the original file)
1935          {          if (pProgress) {
1936              // divide progress into subprogress              // divide progress into subprogress
1937              progress_t subprogress;              progress_t subprogress;
1938              __divide_progress(pProgress, &subprogress, 3.f, 0.f); // arbitrarily subdivided into 1/3 of total progress              __divide_progress(pProgress, &subprogress, 3.f, 0.f); // arbitrarily subdivided into 1/3 of total progress
# Line 1872  namespace RIFF { Line 1940  namespace RIFF {
1940              LoadSubChunksRecursively(&subprogress);              LoadSubChunksRecursively(&subprogress);
1941              // notify subprogress done              // notify subprogress done
1942              __notify_progress(&subprogress, 1.f);              __notify_progress(&subprogress, 1.f);
1943          }          } else
1944                LoadSubChunksRecursively(NULL);
1945    
1946          // reopen file in write mode          // reopen file in write mode
1947          SetMode(stream_mode_read_write);          SetMode(stream_mode_read_write);
# Line 1902  namespace RIFF { Line 1971  namespace RIFF {
1971    
1972              // divide progress into subprogress              // divide progress into subprogress
1973              progress_t subprogress;              progress_t subprogress;
1974              __divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress              if (pProgress)
1975                    __divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress
1976    
1977              // ... we enlarge this file first ...              // ... we enlarge this file first ...
1978              ResizeFile(newFileSize);              ResizeFile(newFileSize);
# Line 1936  namespace RIFF { Line 2006  namespace RIFF {
2006                  fseeko(hFileWrite, ullPos + positiveSizeDiff, SEEK_SET);                  fseeko(hFileWrite, ullPos + positiveSizeDiff, SEEK_SET);
2007                  iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite);                  iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite);
2008                  #endif                  #endif
2009                  if (!(iNotif % 8) && iBytesMoved > 0)                  if (pProgress && !(iNotif % 8) && iBytesMoved > 0)
2010                      __notify_progress(&subprogress, float(workingFileSize - ullPos) / float(workingFileSize));                      __notify_progress(&subprogress, float(workingFileSize - ullPos) / float(workingFileSize));
2011              }              }
2012              delete[] pCopyBuffer;              delete[] pCopyBuffer;
2013              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");
2014    
2015              __notify_progress(&subprogress, 1.f); // notify subprogress done              if (pProgress)
2016                    __notify_progress(&subprogress, 1.f); // notify subprogress done
2017          }          }
2018    
2019          // rebuild / rewrite complete RIFF tree ...          // rebuild / rewrite complete RIFF tree ...
2020    
2021          // divide progress into subprogress          // divide progress into subprogress
2022          progress_t subprogress;          progress_t subprogress;
2023          __divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress          if (pProgress)
2024                __divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress
2025          // do the actual work          // do the actual work
2026          const file_offset_t finalSize = WriteChunk(0, positiveSizeDiff, &subprogress);          const file_offset_t finalSize = WriteChunk(0, positiveSizeDiff, pProgress ? &subprogress : NULL);
2027          const file_offset_t finalActualSize = __GetFileSize(hFileWrite);          const file_offset_t finalActualSize = __GetFileSize(hFileWrite);
2028          // notify subprogress done          // notify subprogress done
2029          __notify_progress(&subprogress, 1.f);          if (pProgress)
2030                __notify_progress(&subprogress, 1.f);
2031    
2032          // resize file to the final size          // resize file to the final size
2033          if (finalSize < finalActualSize) ResizeFile(finalSize);          if (finalSize < finalActualSize) ResizeFile(finalSize);
2034    
2035          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
2036                __notify_progress(pProgress, 1.0); // notify done
2037      }      }
2038    
2039      /** @brief Save changes to another file.      /** @brief Save changes to another file.
# Line 1984  namespace RIFF { Line 2058  namespace RIFF {
2058              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");
2059    
2060          // make sure the RIFF tree is built (from the original file)          // make sure the RIFF tree is built (from the original file)
2061          {          if (pProgress) {
2062              // divide progress into subprogress              // divide progress into subprogress
2063              progress_t subprogress;              progress_t subprogress;
2064              __divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 1/2 of total progress              __divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 1/2 of total progress
# Line 1992  namespace RIFF { Line 2066  namespace RIFF {
2066              LoadSubChunksRecursively(&subprogress);              LoadSubChunksRecursively(&subprogress);
2067              // notify subprogress done              // notify subprogress done
2068              __notify_progress(&subprogress, 1.f);              __notify_progress(&subprogress, 1.f);
2069          }          } else
2070                LoadSubChunksRecursively(NULL);
2071    
2072          if (!bIsNewFile) SetMode(stream_mode_read);          if (!bIsNewFile) SetMode(stream_mode_read);
2073          // 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
# Line 2031  namespace RIFF { Line 2106  namespace RIFF {
2106    
2107          // write complete RIFF tree to the other (new) file          // write complete RIFF tree to the other (new) file
2108          file_offset_t ullTotalSize;          file_offset_t ullTotalSize;
2109          {          if (pProgress) {
2110              // divide progress into subprogress              // divide progress into subprogress
2111              progress_t subprogress;              progress_t subprogress;
2112              __divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 1/2 of total progress              __divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 1/2 of total progress
# Line 2039  namespace RIFF { Line 2114  namespace RIFF {
2114              ullTotalSize = WriteChunk(0, 0, &subprogress);              ullTotalSize = WriteChunk(0, 0, &subprogress);
2115              // notify subprogress done              // notify subprogress done
2116              __notify_progress(&subprogress, 1.f);              __notify_progress(&subprogress, 1.f);
2117          }          } else
2118                ullTotalSize = WriteChunk(0, 0, NULL);
2119    
2120          file_offset_t ullActualSize = __GetFileSize(hFileWrite);          file_offset_t ullActualSize = __GetFileSize(hFileWrite);
2121    
2122          // resize file to the final size (if the file was originally larger)          // resize file to the final size (if the file was originally larger)
# Line 2060  namespace RIFF { Line 2137  namespace RIFF {
2137          Mode = (stream_mode_t) -1;       // Just set it to an undefined mode ...          Mode = (stream_mode_t) -1;       // Just set it to an undefined mode ...
2138          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.
2139    
2140          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
2141                __notify_progress(pProgress, 1.0); // notify done
2142      }      }
2143    
2144      void File::ResizeFile(file_offset_t ullNewSize) {      void File::ResizeFile(file_offset_t ullNewSize) {

Legend:
Removed from v.3412  
changed lines
  Added in v.3901

  ViewVC Help
Powered by ViewVC