/[svn]/libgig/trunk/src/helper.h
ViewVC logotype

Annotation of /libgig/trunk/src/helper.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2912 - (hide annotations) (download) (as text)
Tue May 17 14:30:10 2016 UTC (7 years, 11 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 10142 byte(s)
* gig.cpp/.h: GIG FORMAT EXTENSION: Added support for saving gig file
  larger than 4 GB as one single monolithic gig file. A new custom RIFF
  chunk "FFmt" was added to distinguish such monolithic large .gig files
  from old ones which were splitted over several (.gx01, .gx02, ...)
  "extension" files before.
* DLS.cpp/.h: Sample class: wave pool offsets are now 64 bits (to allow
  support for files larger than 4 GB).
* RIFF.cpp/.h: Addded support for RIFF files larger than 4 GB, by default
  the required internal RIFF file offset size is automatically detected
  (that is RIFF files < 4 GB automatically use 32 bit offsets while
  files >= 4 GB automatically use 64 bit offsets), a particular offset
  size can be forced with a new option added to the RIFF File constructor
  though.
* RIFF.cpp/.h: When saving a modified, grown RIFF file, the temporary file
  size during Save() operation will no longer be larger than the final
  grown file size.
* Automake: Set environment variable GCC_COLORS=auto to allow GCC to auto
  detect whether it (sh/c)ould output its messages in color.
* Bumped version (4.0.0.svn3).

1 schoenebeck 803 /***************************************************************************
2     * *
3 schoenebeck 933 * libgig - C++ cross-platform Gigasampler format file access library *
4 schoenebeck 803 * *
5 schoenebeck 2682 * Copyright (C) 2003-2014 by Christian Schoenebeck *
6 schoenebeck 803 * <cuse@users.sourceforge.net> *
7     * *
8     * This library is free software; you can redistribute it and/or modify *
9     * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This library is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this library; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #ifndef __LIBGIG_HELPER_H__
25     #define __LIBGIG_HELPER_H__
26    
27 schoenebeck 809 #include <string.h>
28 schoenebeck 803 #include <string>
29     #include <sstream>
30    
31 persson 1330 #if defined(WIN32) && !HAVE_CONFIG_H
32     # include "../win32/libgig_private.h" // like config.h, automatically generated by Dev-C++
33     # define PACKAGE "libgig"
34     # define VERSION VER_STRING // VER_STRING defined in libgig_private.h
35     #endif // WIN32
36    
37 schoenebeck 929 #include "RIFF.h"
38    
39 schoenebeck 803 // *************** Helper Functions **************
40     // *
41    
42     template<class T> inline std::string ToString(T o) {
43     std::stringstream ss;
44     ss << o;
45     return ss.str();
46     }
47    
48 schoenebeck 809 inline long Min(long A, long B) {
49     return (A > B) ? B : A;
50     }
51    
52     inline long Abs(long val) {
53     return (val > 0) ? val : -val;
54     }
55    
56 schoenebeck 2912 inline void swapBytes_16(void* Word) {
57     uint8_t byteCache = *((uint8_t*) Word);
58     *((uint8_t*) Word) = *((uint8_t*) Word + 1);
59     *((uint8_t*) Word + 1) = byteCache;
60     }
61    
62     inline void swapBytes_32(void* Word) {
63     uint8_t byteCache = *((uint8_t*) Word);
64     *((uint8_t*) Word) = *((uint8_t*) Word + 3);
65     *((uint8_t*) Word + 3) = byteCache;
66     byteCache = *((uint8_t*) Word + 1);
67     *((uint8_t*) Word + 1) = *((uint8_t*) Word + 2);
68     *((uint8_t*) Word + 2) = byteCache;
69     }
70    
71     inline void swapBytes_64(void* Word) {
72     uint8_t byteCache = ((uint8_t*)Word)[0];
73     ((uint8_t*)Word)[0] = ((uint8_t*)Word)[7];
74     ((uint8_t*)Word)[7] = byteCache;
75     byteCache = ((uint8_t*)Word)[1];
76     ((uint8_t*)Word)[1] = ((uint8_t*)Word)[6];
77     ((uint8_t*)Word)[6] = byteCache;
78     byteCache = ((uint8_t*)Word)[2];
79     ((uint8_t*)Word)[2] = ((uint8_t*)Word)[5];
80     ((uint8_t*)Word)[5] = byteCache;
81     byteCache = ((uint8_t*)Word)[3];
82     ((uint8_t*)Word)[3] = ((uint8_t*)Word)[4];
83     ((uint8_t*)Word)[4] = byteCache;
84     }
85    
86     inline void swapBytes(void* Word, uint64_t WordSize) {
87     uint8_t byteCache;
88     uint64_t lo = 0, hi = WordSize - 1;
89     for (; lo < hi; hi--, lo++) {
90     byteCache = *((uint8_t*) Word + lo);
91     *((uint8_t*) Word + lo) = *((uint8_t*) Word + hi);
92     *((uint8_t*) Word + hi) = byteCache;
93     }
94     }
95    
96 schoenebeck 809 /**
97 persson 1179 * Stores a 16 bit integer in memory using little-endian format.
98     *
99     * @param pData - memory pointer
100     * @param data - integer to be stored
101     */
102     inline void store16(uint8_t* pData, uint16_t data) {
103     pData[0] = data;
104     pData[1] = data >> 8;
105     }
106    
107     /**
108     * Stores a 32 bit integer in memory using little-endian format.
109     *
110     * @param pData - memory pointer
111     * @param data - integer to be stored
112     */
113     inline void store32(uint8_t* pData, uint32_t data) {
114     pData[0] = data;
115     pData[1] = data >> 8;
116     pData[2] = data >> 16;
117     pData[3] = data >> 24;
118     }
119    
120     /**
121 schoenebeck 2912 * Loads a 32 bit integer in memory using little-endian format.
122     *
123     * @param pData - memory pointer
124     * @returns 32 bit data word
125     */
126     inline uint32_t load32(uint8_t* pData) {
127     return uint32_t(pData[0]) |
128     uint32_t(pData[1]) << 8 |
129     uint32_t(pData[2]) << 16 |
130     uint32_t(pData[3]) << 24;
131     }
132    
133     /**
134 schoenebeck 809 * Swaps the order of the data words in the given memory area
135     * with a granularity given by \a WordSize.
136     *
137     * @param pData - pointer to the memory area to be swapped
138     * @param AreaSize - size of the memory area to be swapped (in bytes)
139     * @param WordSize - size of the data words (in bytes)
140     */
141     inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) {
142 schoenebeck 1875 if (!AreaSize) return; // AreaSize==0 would cause a segfault here
143 schoenebeck 809 switch (WordSize) { // TODO: unefficient
144     case 1: {
145     uint8_t* pDst = (uint8_t*) pData;
146     uint8_t cache;
147     unsigned long lo = 0, hi = AreaSize - 1;
148     for (; lo < hi; hi--, lo++) {
149     cache = pDst[lo];
150     pDst[lo] = pDst[hi];
151     pDst[hi] = cache;
152     }
153     break;
154     }
155     case 2: {
156     uint16_t* pDst = (uint16_t*) pData;
157     uint16_t cache;
158     unsigned long lo = 0, hi = (AreaSize >> 1) - 1;
159     for (; lo < hi; hi--, lo++) {
160     cache = pDst[lo];
161     pDst[lo] = pDst[hi];
162     pDst[hi] = cache;
163     }
164     break;
165     }
166     case 4: {
167     uint32_t* pDst = (uint32_t*) pData;
168     uint32_t cache;
169     unsigned long lo = 0, hi = (AreaSize >> 2) - 1;
170     for (; lo < hi; hi--, lo++) {
171     cache = pDst[lo];
172     pDst[lo] = pDst[hi];
173     pDst[hi] = cache;
174     }
175     break;
176     }
177     default: {
178     uint8_t* pCache = new uint8_t[WordSize]; // TODO: unefficient
179     unsigned long lo = 0, hi = AreaSize - WordSize;
180     for (; lo < hi; hi -= WordSize, lo += WordSize) {
181     memcpy(pCache, (uint8_t*) pData + lo, WordSize);
182     memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize);
183     memcpy((uint8_t*) pData + hi, pCache, WordSize);
184     }
185 schoenebeck 1875 if (pCache) delete[] pCache;
186 schoenebeck 809 break;
187     }
188     }
189     }
190    
191 schoenebeck 929 /** @brief Load given info field (string).
192     *
193     * Load info field string from given info chunk (\a ck) and save value to \a s.
194     */
195     inline void LoadString(RIFF::Chunk* ck, std::string& s) {
196     if (ck) {
197     const char* str = (char*)ck->LoadChunkData();
198     int size = ck->GetSize();
199     int len;
200     for (len = 0 ; len < size ; len++)
201     if (str[len] == '\0') break;
202     s.assign(str, len);
203     ck->ReleaseChunkData();
204     }
205     }
206    
207     /** @brief Apply given INFO field to the respective chunk.
208     *
209     * Apply given info value string to given info chunk, which is a
210     * subchunk of INFO list chunk \a lstINFO. If the given chunk already
211     * exists, value \a s will be applied. Otherwise if it doesn't exist yet
212     * and either \a s or \a sDefault is not an empty string, such a chunk
213     * will be created and either \a s or \a sDefault will be applied
214     * (depending on which one is not an empty string, if both are not an
215     * empty string \a s will be preferred).
216     *
217     * @param ChunkID - 32 bit RIFF chunk ID of INFO subchunk (only used in case \a ck is NULL)
218     * @param ck - INFO (sub)chunk where string should be stored to
219     * @param lstINFO - parent (INFO) RIFF list chunk
220     * @param s - current value of info field
221     * @param sDefault - default value
222     * @param bUseFixedLengthStrings - should a specific string size be forced in the chunk?
223     * @param size - wanted size of the INFO chunk. This is ignored if bUseFixedLengthStrings is false.
224     */
225     inline void SaveString(uint32_t ChunkID, RIFF::Chunk* ck, RIFF::List* lstINFO, const std::string& s, const std::string& sDefault, bool bUseFixedLengthStrings, int size) {
226     if (ck) { // if chunk exists already, use 's' as value
227     if (!bUseFixedLengthStrings) size = s.size() + 1;
228     ck->Resize(size);
229     char* pData = (char*) ck->LoadChunkData();
230     strncpy(pData, s.c_str(), size);
231 persson 1180 } else if (s != "" || sDefault != "" || bUseFixedLengthStrings) { // create chunk
232 schoenebeck 929 const std::string& sToSave = (s != "") ? s : sDefault;
233     if (!bUseFixedLengthStrings) size = sToSave.size() + 1;
234     ck = lstINFO->AddSubChunk(ChunkID, size);
235     char* pData = (char*) ck->LoadChunkData();
236     strncpy(pData, sToSave.c_str(), size);
237     }
238     }
239    
240 schoenebeck 2682 // private helper function to convert progress of a subprocess into the global progress
241     inline void __notify_progress(RIFF::progress_t* pProgress, float subprogress) {
242     if (pProgress && pProgress->callback) {
243     const float totalrange = pProgress->__range_max - pProgress->__range_min;
244     const float totalprogress = pProgress->__range_min + subprogress * totalrange;
245     pProgress->factor = totalprogress;
246     pProgress->callback(pProgress); // now actually notify about the progress
247     }
248     }
249    
250     // private helper function to divide a progress into subprogresses
251     inline void __divide_progress(RIFF::progress_t* pParentProgress, RIFF::progress_t* pSubProgress, float totalTasks, float currentTask) {
252     if (pParentProgress && pParentProgress->callback) {
253     const float totalrange = pParentProgress->__range_max - pParentProgress->__range_min;
254     pSubProgress->callback = pParentProgress->callback;
255     pSubProgress->custom = pParentProgress->custom;
256     pSubProgress->__range_min = pParentProgress->__range_min + totalrange * currentTask / totalTasks;
257     pSubProgress->__range_max = pSubProgress->__range_min + totalrange / totalTasks;
258     }
259     }
260    
261 schoenebeck 803 #endif // __LIBGIG_HELPER_H__

  ViewVC Help
Powered by ViewVC