SynthLab SDK
pcmsample.h
Go to the documentation of this file.
1 #ifndef __wavesample__
2 #define __wavesample__
3 
4 #include "stdint.h"
5 #include <string>
6 #include <iostream>
7 #include <map>
8 
9 // -----------------------------
10 // --- SynthLab SDK File --- //
11 // ----------------------------
19 // -----------------------------------------------------------------------------
20 namespace SynthLab
21 {
26  struct RIFF_CHUNK
27  {
28  char IdentifierString[4] = "";
29  uint32_t dwLength = 0;
30  };
31 
37  {
38  uint16_t wFormatTag = 0; // Format category
39  uint16_t wChannels = 0; // Number of channels
40  uint32_t dwSamplesPerSec = 0; // Sampling rate
41  uint32_t dwAvgBytesPerSec = 0; // For buffer estimation
42  uint16_t wBlockAlign = 0; // Data block size
43  uint16_t wBitsPerSample = 0;
44  };
45 
51  {
52  uint16_t wFormatTag = 0; /* format type */
53  uint16_t nChannels = 0; /* number of channels (i.e. mono, stereo...) */
54  uint32_t nSamplesPerSec = 0; /* sample rate */
55  uint32_t nAvgBytesPerSec = 0; /* for buffer estimation */
56  uint16_t nBlockAlign = 0; /* block size of data */
57  uint16_t wBitsPerSample = 0; /* number of bits per sample of mono data */
58  uint16_t cbSize = 0; /* the count in bytes of the size of */
59  } ;
60 
65  struct WAVE_SAMPLE
66  {
67  WAVEFORMATEX_WP WaveFormatEx;
68  char *pSampleData = nullptr;
69  uint32_t Index = 0;
70  uint32_t Size = 0;
71  uint32_t dwId = 0;
72  uint32_t bPlaying = 0;
73  };
74 
79  union UWaveData
80  {
81  float f = 0.0; // only need one initializer
82  double d;
83  int32_t n;
84  uint32_t u;
85  uint64_t u64;
86  };
87 
109  class PCMSample
110  {
111  public:
112  PCMSample() { }
113  ~PCMSample();// { if (pcmSampleBuffer) delete[] pcmSampleBuffer; }
114 
116  bool loadPCMSample(const char* filePath);
117  const float* getSampleBuffer() { return pcmSampleBuffer; }
118 
120  void setPitchless(bool _pitchlessSample) { pitchlessSample = _pitchlessSample; }
121  bool isPitchless() { return pitchlessSample; }
122 
124  bool isSampleLoaded() { return sampleLoaded; }
125 
127 
131  uint32_t getNumChannels() { return numChannels; }
132  uint32_t getSampleRate() { return sampleRate; }
133  uint32_t getSampleCount() { return sampleCount; }
134  uint32_t getLoopType() { return loopType; }
135  uint32_t getSmpteFormat() { return smpteFormat; }
136  uint32_t getSmpteOffset() { return smpteOffset; }
138 
140 
144  uint32_t getLoopCount() { return loopCount; }
145  void setLoopCount(uint32_t u) { loopCount = u; }
146 
147  uint32_t getLoopStartIndex() { return loopStartIndex; }
148  void setLoopStartIndex(uint32_t u) { loopStartIndex = u; }
149 
150  uint32_t getLoopEndIndex() { return loopEndIndex; }
151  void setLoopEndIndex(uint32_t u) { loopEndIndex = u; }
152 
153  uint32_t getUnityMIDINote() { return unityMIDINote; }
154  void setUnityMIDINote(uint32_t u) { unityMIDINote = u; }
155 
156  uint32_t getUnityMIDIPitchFraction() { return unityMIDIPitchFraction; }
157  void setUnityMIDIPitchFraction(uint32_t u) { unityMIDIPitchFraction = u; }
159 
160  protected:
161  uint32_t numChannels = 0;
162  uint32_t sampleRate = 0;
163  uint32_t sampleCount = 0;
164  uint32_t loopCount = 0;
165  uint32_t loopStartIndex = 0;
166  uint32_t loopEndIndex = 0;
167  uint32_t loopType = 0;
168  uint32_t unityMIDINote = 0;
169  uint32_t unityMIDIPitchFraction = 0;
170  uint32_t smpteFormat = 0;
171  uint32_t smpteOffset = 0;
172  bool sampleLoaded = false;
173 
174  // --- the WAV file converted to floats on range of -1.0 --> +1.0
175  float* pcmSampleBuffer = nullptr;
176  bool pitchlessSample = false;
177  };
178 
183  struct convertUpper {
184  void operator()(char& c) { c = toupper((unsigned char)c); }
185  };
186 
203  {
204  public: // Functions
205  //
206  // One Time Initialization
207  // waveFolder is the FULLY qualified file name + additional path info
208  // VALID Examples: audio.wav
209  // //samples//audio.wav
210  WaveFolder(const char* _waveFolderPath,
211  const char* _waveFolderName) {
212  waveFolderPath = _waveFolderPath;
213  waveFolderName = _waveFolderName;
214  buildNoteTables();
215  }
216  ~WaveFolder() {}
217 
218  // --- get next folder
219  uint32_t parseFolder(PCMSample** sampleSet, bool pitchlessLoops, bool aubioSlices = false);
220 
221  // --- add file and information about it to a map, used for parsing files in second step
222  void addNextFileToMap(std::string fileFolderPath, std::string fileName, bool aubioSlices, std::map<int, std::string>* wavFilePaths, int& fileCount);
223 
224  // --- helper
225  void eraseSubStr(std::string & mainStr, const std::string & toErase)
226  {
227  // Search for the substring in string
228  size_t pos = mainStr.find(toErase);
229  if (pos != std::string::npos)
230  {
231  // If found then erase it from string
232  mainStr.erase(pos, toErase.length());
233  }
234  }
235 
236  protected:
237  const char* waveFolderPath;
238  const char* waveFolderName;
239  void buildNoteTables();
240  std::string noteTableSharps[120];
241  std::string noteTableFlats[120];
242  int32_t findNoteNumberInName(const char* filename, bool shiftUpOctave = true);
243  };
244 
245 } // namespace
246 #endif
Definition: pcmsample.h:79
Opens a folder full of WAV files and gleans information about the files to prep them for parsing and ...
Definition: pcmsample.h:202
Definition: pcmsample.h:36
Opens a WAV file and extracts contents into a floating point buffer, regardless of original datatypes...
Definition: pcmsample.h:109
uint32_t getSmpteOffset()
Immutable variables can only be set during file parse.
Definition: pcmsample.h:136
uint32_t parseFolder(PCMSample **sampleSet, bool pitchlessLoops, bool aubioSlices=false)
The main function that opens a folder, creates the WAV information map, and then parses the files in ...
Definition: pcmsample.cpp:822
std::string noteTableFlats[120]
table with characters used to decode filenames with flats
Definition: pcmsample.h:241
void buildNoteTables()
Sets up the tables for trying to suss out the MIDI note number from a filename that attempts to inclu...
Definition: pcmsample.cpp:675
void setUnityMIDIPitchFraction(uint32_t u)
Mutable variables that may need to be re-calculated after parsing.
Definition: pcmsample.h:157
Definition: addosccore.cpp:4
uint32_t getNumChannels()
Immutable variables can only be set during file parse.
Definition: pcmsample.h:131
void addNextFileToMap(std::string fileFolderPath, std::string fileName, bool aubioSlices, std::map< int, std::string > *wavFilePaths, int &fileCount)
Adds information about a WAV file in the folder to a map that is later used to parse the files in suc...
Definition: pcmsample.cpp:752
Definition: pcmsample.h:50
uint32_t getUnityMIDINote()
Mutable variables that may need to be re-calculated after parsing.
Definition: pcmsample.h:153
Definition: pcmsample.h:26
uint32_t getUnityMIDIPitchFraction()
Mutable variables that may need to be re-calculated after parsing.
Definition: pcmsample.h:156
uint32_t getLoopEndIndex()
Mutable variables that may need to be re-calculated after parsing.
Definition: pcmsample.h:150
uint32_t getSampleRate()
Immutable variables can only be set during file parse.
Definition: pcmsample.h:132
bool loadPCMSample(const char *filePath)
Opens a WAV file and extracts the audio guts into a buffer of floats. Anytime later, you can use isSampleLoaded( ) to see if the sample data is valid.
Definition: pcmsample.cpp:40
uint32_t getLoopStartIndex()
Mutable variables that may need to be re-calculated after parsing.
Definition: pcmsample.h:147
uint32_t getSmpteFormat()
Immutable variables can only be set during file parse.
Definition: pcmsample.h:135
uint32_t getSampleCount()
Immutable variables can only be set during file parse.
Definition: pcmsample.h:133
int32_t findNoteNumberInName(const char *filename, bool shiftUpOctave=true)
figure out MIDI note number from string
Definition: pcmsample.cpp:710
void setLoopEndIndex(uint32_t u)
Mutable variables that may need to be re-calculated after parsing.
Definition: pcmsample.h:151
void setLoopCount(uint32_t u)
Mutable variables that may need to be re-calculated after parsing.
Definition: pcmsample.h:145
Definition: pcmsample.h:65
uint32_t getLoopType()
Immutable variables can only be set during file parse.
Definition: pcmsample.h:134
void setLoopStartIndex(uint32_t u)
Mutable variables that may need to be re-calculated after parsing.
Definition: pcmsample.h:148
void setPitchless(bool _pitchlessSample)
Definition: pcmsample.h:120
void setUnityMIDINote(uint32_t u)
Mutable variables that may need to be re-calculated after parsing.
Definition: pcmsample.h:154
uint32_t getLoopCount()
Mutable variables that may need to be re-calculated after parsing.
Definition: pcmsample.h:144
std::string noteTableSharps[120]
table with characters used to decode filenames with sharps
Definition: pcmsample.h:240
bool isSampleLoaded()
Definition: pcmsample.h:124
Definition: pcmsample.h:183