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

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

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

revision 1179 by persson, Sat May 12 11:25:04 2007 UTC revision 2912 by schoenebeck, Tue May 17 14:30:10 2016 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-2006 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2014 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 28  Line 28 
28  #include <string>  #include <string>
29  #include <sstream>  #include <sstream>
30    
31    #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  #include "RIFF.h"  #include "RIFF.h"
38    
39  // *************** Helper Functions **************  // *************** Helper Functions **************
# Line 47  inline long Abs(long val) { Line 53  inline long Abs(long val) {
53      return (val > 0) ? val : -val;      return (val > 0) ? val : -val;
54  }  }
55    
56    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  /**  /**
97   * Stores a 16 bit integer in memory using little-endian format.   * Stores a 16 bit integer in memory using little-endian format.
98   *   *
# Line 72  inline void store32(uint8_t* pData, uint Line 118  inline void store32(uint8_t* pData, uint
118  }  }
119    
120  /**  /**
121     * 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   * Swaps the order of the data words in the given memory area   * Swaps the order of the data words in the given memory area
135   * with a granularity given by \a WordSize.   * with a granularity given by \a WordSize.
136   *   *
# Line 80  inline void store32(uint8_t* pData, uint Line 139  inline void store32(uint8_t* pData, uint
139   * @param WordSize - size of the data words (in bytes)   * @param WordSize - size of the data words (in bytes)
140   */   */
141  inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) {  inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) {
142        if (!AreaSize) return; // AreaSize==0 would cause a segfault here
143      switch (WordSize) { // TODO: unefficient      switch (WordSize) { // TODO: unefficient
144          case 1: {          case 1: {
145              uint8_t* pDst = (uint8_t*) pData;              uint8_t* pDst = (uint8_t*) pData;
# Line 122  inline void SwapMemoryArea(void* pData, Line 182  inline void SwapMemoryArea(void* pData,
182                  memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize);                  memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize);
183                  memcpy((uint8_t*) pData + hi, pCache, WordSize);                  memcpy((uint8_t*) pData + hi, pCache, WordSize);
184              }              }
185              delete[] pCache;              if (pCache) delete[] pCache;
186              break;              break;
187          }          }
188      }      }
# Line 168  inline void SaveString(uint32_t ChunkID, Line 228  inline void SaveString(uint32_t ChunkID,
228          ck->Resize(size);          ck->Resize(size);
229          char* pData = (char*) ck->LoadChunkData();          char* pData = (char*) ck->LoadChunkData();
230          strncpy(pData, s.c_str(), size);          strncpy(pData, s.c_str(), size);
231      } else if (s != "" || sDefault != "") { // create chunk      } else if (s != "" || sDefault != "" || bUseFixedLengthStrings) { // create chunk
232          const std::string& sToSave = (s != "") ? s : sDefault;          const std::string& sToSave = (s != "") ? s : sDefault;
233          if (!bUseFixedLengthStrings) size = sToSave.size() + 1;          if (!bUseFixedLengthStrings) size = sToSave.size() + 1;
234          ck = lstINFO->AddSubChunk(ChunkID, size);          ck = lstINFO->AddSubChunk(ChunkID, size);
# Line 177  inline void SaveString(uint32_t ChunkID, Line 237  inline void SaveString(uint32_t ChunkID,
237      }      }
238  }  }
239    
240    // 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  #endif // __LIBGIG_HELPER_H__  #endif // __LIBGIG_HELPER_H__

Legend:
Removed from v.1179  
changed lines
  Added in v.2912

  ViewVC Help
Powered by ViewVC