1 /************************** BEGIN JuceReader.h **************************/
2 /************************************************************************
3  FAUST Architecture File
4  Copyright (C) 2018 GRAME, Centre National de Creation Musicale
5  ---------------------------------------------------------------------
6  This Architecture section is free software; you can redistribute it
7  and/or modify it under the terms of the GNU General Public License
8  as published by the Free Software Foundation; either version 3 of
9  the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; If not, see <http://www.gnu.org/licenses/>.
18 
19  EXCEPTION : As a special exception, you may create a larger work
20  that contains this FAUST architecture section and distribute
21  that work under terms of your choice, so long as this FAUST
22  architecture section is not modified.
23  ************************************************************************/
24 
25 #ifndef __JuceReader__
26 #define __JuceReader__
27 
28 #include <assert.h>
29 
30 #include "../JuceLibraryCode/JuceHeader.h"
31 
32 #include "faust/gui/Soundfile.h"
33 
34 struct JuceReader : public SoundfileReader {
35 
36     juce::AudioFormatManager fFormatManager;
37 
JuceReaderJuceReader38     JuceReader() { fFormatManager.registerBasicFormats(); }
~JuceReaderJuceReader39     virtual ~JuceReader()
40     {}
41 
checkFileJuceReader42     bool checkFile(const std::string& path_name)
43     {
44         juce::File file(path_name);
45         if (file.existsAsFile()) {
46             return true;
47         } else {
48             std::cerr << "ERROR : cannot open '" << path_name << "'" << std::endl;
49             return false;
50         }
51     }
52 
getParamsFileJuceReader53     void getParamsFile(const std::string& path_name, int& channels, int& length)
54     {
55         std::unique_ptr<juce::AudioFormatReader> formatReader (fFormatManager.createReaderFor (juce::File (path_name)));
56         channels = int(formatReader->numChannels);
57         length = int(formatReader->lengthInSamples);
58     }
59 
readFileJuceReader60     void readFile(Soundfile* soundfile, const std::string& path_name, int part, int& offset, int max_chan)
61     {
62         std::unique_ptr<juce::AudioFormatReader> formatReader (fFormatManager.createReaderFor (juce::File (path_name)));
63 
64         soundfile->fLength[part] = int(formatReader->lengthInSamples);
65         soundfile->fSR[part] = int(formatReader->sampleRate);
66         soundfile->fOffset[part] = offset;
67 
68         void* buffers;
69         if (soundfile->fIsDouble) {
70             buffers = alloca(soundfile->fChannels * sizeof(double*));
71             soundfile->getBuffersOffsetReal<double>(buffers, offset);
72         } else {
73             buffers = alloca(soundfile->fChannels * sizeof(float*));
74             soundfile->getBuffersOffsetReal<float>(buffers, offset);
75         }
76 
77         if (formatReader->read(reinterpret_cast<int *const *>(buffers), int(formatReader->numChannels), 0, int(formatReader->lengthInSamples), false)) {
78 
79             // Possibly convert samples
80             if (!formatReader->usesFloatingPointData) {
81                 for (int chan = 0; chan < int(formatReader->numChannels); ++chan) {
82                     if (soundfile->fIsDouble) {
83                         // TODO
84                     } else {
85                         float* buffer = &(static_cast<float**>(soundfile->fBuffers))[chan][soundfile->fOffset[part]];
86                         juce::FloatVectorOperations::convertFixedToFloat(buffer, reinterpret_cast<const int*>(buffer),
87                                                                          1.0f/0x7fffffff, int(formatReader->lengthInSamples));
88                     }
89                 }
90             }
91 
92         } else {
93             std::cerr << "Error reading the file : " << path_name << std::endl;
94         }
95 
96         // Update offset
97         offset += soundfile->fLength[part];
98     }
99 
100 };
101 
102 #endif
103 /**************************  END  JuceReader.h **************************/
104