-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrt-juce-basic.h
More file actions
570 lines (478 loc) · 23.1 KB
/
brt-juce-basic.h
File metadata and controls
570 lines (478 loc) · 23.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
==============================================================================
This file is part of the JUCE tutorials.
Copyright (c) 2020 - Raw Material Software Limited
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
PURPOSE, ARE DISCLAIMED.
==============================================================================
*/
/*******************************************************************************
The block below describes the properties of this PIP. A PIP is a short snippet
of code that can be read by the Projucer and used to generate a JUCE project.
BEGIN_JUCE_PIP_METADATA
name: brt-juce-basic.h
version: 1.0.0
vendor: JUCE
website: http://diana.uma.es
description: Plays audio files.
dependencies: juce_audio_basics, juce_audio_devices, juce_audio_formats,
juce_audio_processors, juce_audio_utils, juce_core,
juce_data_structures, juce_events, juce_graphics,
juce_gui_basics, juce_gui_extra
exporters: xcode_mac, vs2022
type: Component
mainClass: MainContentComponent
useLocalCopy: 1
END_JUCE_PIP_METADATA
*******************************************************************************/
#pragma once
#include <BRTLibrary.h>
//==============================================================================
constexpr int BLOCK_SIZE = 512; // Block size in samples
constexpr int HRTFRESAMPLINGSTEP = 15;
constexpr float SOURCE1_INITIAL_AZIMUTH = 3.141592653589793 / 2.0; // pi/2
constexpr float SOURCE1_INITIAL_ELEVATION = 0.f;
constexpr float SOURCE1_INITIAL_DISTANCE = 1;// 0.1f; // 10 cm.
//==============================================================================
class MainContentComponent : public juce::AudioAppComponent,
public juce::ChangeListener,
public juce::Slider::Listener,
public juce::Button::Listener
{
public:
//==========================================================================
MainContentComponent()
: state (Stopped)
{
// Add a button to open a SOFA file
addAndMakeVisible(&openSOFAButton);
openSOFAButton.setButtonText("Open SOFA file...");
openSOFAButton.onClick = [this] { openSOFAButtonClicked(); };
addAndMakeVisible (&openWavButton);
openWavButton.setButtonText ("Open mono wav file...");
openWavButton.onClick = [this] { openWavButtonClicked(); };
addAndMakeVisible (&playButton);
playButton.setButtonText ("Play");
playButton.onClick = [this] { playButtonClicked(); };
playButton.setColour (juce::TextButton::buttonColourId, juce::Colours::green);
playButton.setEnabled (false);
addAndMakeVisible (&stopButton);
stopButton.setButtonText ("Stop");
stopButton.onClick = [this] { stopButtonClicked(); };
stopButton.setColour (juce::TextButton::buttonColourId, juce::Colours::red);
stopButton.setEnabled (false);
// Add a dial to control the azimuth of the source
addAndMakeVisible(&sourceAzimuthDial);
sourceAzimuthDial.setRange(- juce::MathConstants<float>::pi / 2, juce::MathConstants<float>::pi / 2, 0.01);
sourceAzimuthDial.setValue(SOURCE1_INITIAL_AZIMUTH);
sourceAzimuthDial.setTextValueSuffix(" rad");
sourceAzimuthDial.addListener(this);
sourceAzimuthDial.setEnabled(false);
addAndMakeVisible(&sourceAzimuthLabel);
sourceAzimuthLabel.setText("Azimuth", juce::dontSendNotification);
sourceAzimuthLabel.attachToComponent(&sourceAzimuthDial, true);
sourceAzimuthLabel.setEnabled(false);
addAndMakeVisible(&sourceElevationDial);
sourceElevationDial.setRange(- juce::MathConstants<float>::pi / 4, juce::MathConstants<float>::pi / 4, 0.01);
sourceElevationDial.setValue(SOURCE1_INITIAL_ELEVATION);
sourceElevationDial.setTextValueSuffix(" rad");
sourceElevationDial.addListener(this);
sourceElevationDial.setEnabled(false);
addAndMakeVisible(&sourceElevationLabel);
sourceElevationLabel.setText("Elevation", juce::dontSendNotification);
sourceElevationLabel.attachToComponent(&sourceElevationDial, true);
sourceElevationLabel.setEnabled(false);
addAndMakeVisible(&sourceDistanceDial);
sourceDistanceDial.setRange(0.1, 10, 0.01);
sourceDistanceDial.setValue(SOURCE1_INITIAL_DISTANCE);
sourceDistanceDial.setTextValueSuffix(" m");
sourceDistanceDial.addListener(this);
sourceDistanceDial.setEnabled(false);
addAndMakeVisible(&sourceDistanceLabel);
sourceDistanceLabel.setText("Distance", juce::dontSendNotification);
sourceDistanceLabel.attachToComponent(&sourceDistanceDial, true);
sourceDistanceLabel.setEnabled(false);
formatManager.registerBasicFormats(); // [1]
transportSource.addChangeListener (this); // [2]
setAudioChannels (0, 2);
if (auto* device = deviceManager.getCurrentAudioDevice())
{
juce::AudioDeviceManager::AudioDeviceSetup setup;
deviceManager.getAudioDeviceSetup(setup);
// Show the sample rate in the GUI
addAndMakeVisible(&sampleRateLabel);
sampleRateLabel.setText("Sample Rate: " + std::to_string((int)setup.sampleRate) + " Hz", juce::dontSendNotification);
sourceDistanceLabel.attachToComponent(&sourceDistanceDial, true);
setup.bufferSize = BLOCK_SIZE; // Set the buffer size
deviceManager.setAudioDeviceSetup(setup, true);
setupBRT(setup.sampleRate, setup.bufferSize);
}
else {
juce::AlertWindow::showMessageBoxAsync(juce::AlertWindow::WarningIcon, "Error", "No audio device found", "OK");
}
setSize (400, 300);
}
//==========================================================================
~MainContentComponent() override
{
shutdownAudio();
}
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override
{
transportSource.prepareToPlay (samplesPerBlockExpected, sampleRate);
globalParameters.SetSampleRate(sampleRate);
globalParameters.SetBufferSize(samplesPerBlockExpected);
// initialise parameters needed for changing HRTFs
hrtfState = NotToBeChanged;
selectedHRTFidx = -1;
}
void getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill) override
{
// If we still haven't loaded a file, simply clear the buffer
if (readerSource.get() == nullptr)
{
bufferToFill.clearActiveBufferRegion();
return;
}
// Check if different HRTF was selected and change accordingly
if (hrtfState == ToBeChanged){
listener->SetHRTF(HRTF_list[selectedHRTFidx]);
hrtfState = NotToBeChanged;
}
// Obtener las muestras de audio del transportSource
juce::AudioBuffer<float> audioInputBuffer(1, bufferToFill.numSamples); // Buffer mono para las muestras de audio
juce::AudioSourceChannelInfo audioBufferToFill(&audioInputBuffer, 0, bufferToFill.numSamples);
transportSource.getNextAudioBlock(audioBufferToFill);
// Prepare the input buffer for source 1
CMonoBuffer<float> source1Buffer(bufferToFill.numSamples);
for (int i = 0; i < bufferToFill.numSamples; i++) {
source1Buffer[i] = audioInputBuffer.getSample(0, i);
}
// Pass the input buffer to the BRT Library source
source1BRT->SetBuffer(source1Buffer);
// Binaural processing
brtManager.ProcessAll(); // Process all sources
//// Get output stereo buffer
Common::CEarPair<CMonoBuffer<float>> outputBuffer;
listener->GetBuffers(outputBuffer.left, outputBuffer.right);
// Left output buffer is expected to be in channel 0
float* const leftBuffer = bufferToFill.buffer->getWritePointer(0, bufferToFill.startSample);
float* const rightBuffer = bufferToFill.buffer->getWritePointer(1, bufferToFill.startSample);
for (int i = 0; i < bufferToFill.numSamples; i++) {
leftBuffer[i] = outputBuffer.left[i];
rightBuffer[i] = outputBuffer.right[i];
// For test purposes just copy the sambples from source1Buffer to the output buffers
// leftBuffer[i] = source1Buffer[i];
// rightBuffer[i] = source1Buffer[i];
}
}
void releaseResources() override
{
transportSource.releaseResources();
}
void resized() override
{
openWavButton.setBounds(10, 10, getWidth() - 20, 20);
openSOFAButton.setBounds (10, 40, getWidth() - 20, 20);
playButton.setBounds (10, 70, getWidth() - 20, 20);
stopButton.setBounds (10, 100, getWidth() - 20, 20);
auto sliderLeft = 50;
// Position nicely all sliders
sourceAzimuthDial.setBounds(sliderLeft, 130, getWidth() - sliderLeft - 10, 20);
sourceElevationDial.setBounds(sliderLeft, 160, getWidth() - sliderLeft - 10, 20);
sourceDistanceDial.setBounds(sliderLeft, 190, getWidth() - sliderLeft - 10, 20);
sampleRateLabel.setBounds(getWidth()-160, 220, getWidth()-20, 20);
// Position the SOFA buttons at the bottom of the component
int y = getHeight() - 30;
for (auto* button : sofaFileButtons)
{
button->setBounds(10, y, getWidth() - 20, 20);
y -= 30;
}
}
void changeListenerCallback (juce::ChangeBroadcaster* source) override
{
if (source == &transportSource)
{
if (transportSource.isPlaying())
changeState (Playing);
else
changeState (Stopped);
}
}
void sliderValueChanged(juce::Slider* slider) override
{
// Make sure the slider is enabled before accessing BRT objects
if (!slider->isEnabled())
{
return;
}
if (slider == &sourceAzimuthDial) {
sourceAzimuth = sourceAzimuthDial.getValue();
Common::CTransform sourceTransform = source1BRT->GetCurrentSourceTransform();
Common::CVector3 listenerPosition = listener->GetListenerTransform().GetPosition();
Common::CVector3 sourcePosition = Common::CVector3(sourceDistance * std::cos(sourceAzimuth) * std::cos(sourceElevation) + listenerPosition.x,
sourceDistance * std::sin(sourceAzimuth) * std::cos(sourceElevation) + listenerPosition.y,
sourceDistance * std::sin(sourceElevation) + listenerPosition.z);
sourceTransform.SetPosition(sourcePosition);
source1BRT->SetSourceTransform(sourceTransform);
}
else if (slider == &sourceElevationDial) {
sourceElevation = sourceElevationDial.getValue();
Common::CTransform sourceTransform = source1BRT->GetCurrentSourceTransform();
Common::CVector3 listenerPosition = listener->GetListenerTransform().GetPosition();
Common::CVector3 sourcePosition = Common::CVector3(sourceDistance * std::cos(sourceAzimuth) * std::cos(sourceElevation) + listenerPosition.x,
sourceDistance * std::sin(sourceAzimuth) * std::cos(sourceElevation) + listenerPosition.y,
sourceDistance * std::sin(sourceElevation) + listenerPosition.z);
sourceTransform.SetPosition(sourcePosition);
source1BRT->SetSourceTransform(sourceTransform);
}
else if (slider == &sourceDistanceDial) {
sourceDistance = sourceDistanceDial.getValue();
Common::CTransform sourceTransform = source1BRT->GetCurrentSourceTransform();
Common::CVector3 listenerPosition = listener->GetListenerTransform().GetPosition();
Common::CVector3 sourcePosition = Common::CVector3(sourceDistance * std::cos(sourceAzimuth) * std::cos(sourceElevation) + listenerPosition.x,
sourceDistance * std::sin(sourceAzimuth) * std::cos(sourceElevation) + listenerPosition.y,
sourceDistance * std::sin(sourceElevation) + listenerPosition.z);
sourceTransform.SetPosition(sourcePosition);
source1BRT->SetSourceTransform(sourceTransform);
}
}
void buttonClicked(juce::Button* button) override
{
if (button->getToggleState())
{
// Find the button that was clicked in the list of SOFA file buttons
for (int i = 0; i < sofaFileButtons.size(); ++i)
{
if (button == sofaFileButtons[i])
{
// Check if the button is not the same as previously selected
if (i != selectedHRTFidx)
{
//Set the listener HRTF to the selected SOFA file
selectedHRTFidx = i;
hrtfState = ToBeChanged;
}
break;
}
}
}
}
private:
enum TransportState
{
Stopped,
Starting,
Playing,
Stopping
};
// States used when HRTF is being changed
enum HRTFState
{
ToBeChanged,
NotToBeChanged
};
void changeState (TransportState newState)
{
if (state != newState)
{
state = newState;
switch (state)
{
case Stopped: // [3]
stopButton.setEnabled (false);
playButton.setEnabled (true);
transportSource.setPosition (0.0);
break;
case Starting: // [4]
playButton.setEnabled (false);
transportSource.start();
break;
case Playing: // [5]
stopButton.setEnabled (true);
break;
case Stopping: // [6]
transportSource.stop();
break;
}
}
}
//==========================================================================
/// Setup BRT Library
void setupBRT(int sampleRate, int bufferSize) {
// Global parameters
globalParameters.SetSampleRate(sampleRate);
globalParameters.SetBufferSize(bufferSize);
// Listener creation
brtManager.BeginSetup();
listener = brtManager.CreateListener<BRTListenerModel::CListenerHRTFbasedModel>("listener1");
brtManager.EndSetup();
// Place the listener in (0,0,0)
Common::CTransform listenerPosition = Common::CTransform();
listenerPosition.SetPosition(Common::CVector3(0, 0, 0));
listener->SetListenerTransform(listenerPosition);
}
//==========================================================================
/// Load a SOFA file and add it to the HRTF list
bool LoadSOFAFile(const juce::File& file) {
bool result = false;
std::shared_ptr<BRTServices::CHRTF> hrtf = std::make_shared<BRTServices::CHRTF>();
// Try to get sample rate in SOFA
int sampleRateInSOFAFile = sofaReader.GetSampleRateFromSofa(file.getFullPathName().toStdString());
if (sampleRateInSOFAFile == -1) {
juce::AlertWindow::showMessageBoxAsync(juce::AlertWindow::WarningIcon, "Error", "The SOFA file does not contain a valid sample rate", "OK");
result = false;
}
else {
// Make sure sample rate is same as selected in app.
if (sampleRateInSOFAFile != globalParameters.GetSampleRate()) {
juce::AlertWindow::showMessageBoxAsync(juce::AlertWindow::WarningIcon, "Error", "The SOFA file sample rate does not match the selected sample rate", "OK");
result = false;
}
else {
// Load SOFA file
result = sofaReader.ReadHRTFFromSofa(file.getFullPathName().toStdString(), hrtf, HRTFRESAMPLINGSTEP, "NearestPoint");
if (result) {
HRTF_list.push_back(hrtf);
}
else {
juce::AlertWindow::showMessageBoxAsync(juce::AlertWindow::WarningIcon, "Error", "Error loading SOFA file", "OK");
}
}
}
return result;
}
//==========================================================================
// Load a source in the BRT Library
void LoadSource(const String& name, float azimuth, float elevation, float distance) {
// Create a source
brtManager.BeginSetup();
source1BRT = brtManager.CreateSoundSource<BRTSourceModel::CSourceSimpleModel>(name.toStdString());
listener->ConnectSoundSource(source1BRT);
brtManager.EndSetup();
// Set the source position
Common::CVector3 listenerPosition = listener->GetListenerTransform().GetPosition();
Common::CTransform sourceTransform;
Common::CVector3 sourcePosition = Common::CVector3(distance * std::cos(azimuth) * std::cos(elevation) + listenerPosition.x,
distance * std::sin(azimuth) * std::cos(elevation) + listenerPosition.y,
distance * std::sin(elevation) + listenerPosition.z);
sourceTransform.SetPosition(sourcePosition);
source1BRT->SetSourceTransform(sourceTransform);
}
// Open a SOFA file using a file chooser
void openSOFAButtonClicked()
{
chooser = std::make_unique<juce::FileChooser> ("Select a SOFA file to load...",
juce::File{},
"*.sofa"); // Filter for SOFA files
auto chooserFlags = juce::FileBrowserComponent::openMode
| juce::FileBrowserComponent::canSelectFiles;
chooser->launchAsync (chooserFlags, [this] (const juce::FileChooser& fc) // Launch the file chooser
{
auto file = fc.getResult();
if (file != juce::File{})
{
// Load the SOFA file
if (LoadSOFAFile(file)) {
juce::AlertWindow::showMessageBoxAsync(juce::AlertWindow::InfoIcon, "Success", "SOFA file loaded successfully", "OK");
sourceAzimuthDial.setEnabled(true);
sourceElevationDial.setEnabled(true);
sourceDistanceDial.setEnabled(true);
// Create a new ToggleButton for the new SOFA file
ToggleButton* sofaFileButton = new ToggleButton(file.getFileNameWithoutExtension());
sofaFileButton->setRadioGroupId(1);
sofaFileButtons.add(sofaFileButton);
addAndMakeVisible(sofaFileButton);
sofaFileButton->addListener(this);
sofaFileButton->setToggleState(true, juce::NotificationType::dontSendNotification);
// Call resized() to update the layout
resized();
// Set the listener HRTF to the last loaded HRTF
selectedHRTFidx = (int) HRTF_list.size() - 1;
hrtfState = ToBeChanged; // The HRTF will be changed in the next buffer
}
}
});
}
// Open a mono wav file using a file chooser
void openWavButtonClicked()
{
chooser = std::make_unique<juce::FileChooser> ("Select a Wave file to play...",
juce::File{},
"*.wav"); // [7]
auto chooserFlags = juce::FileBrowserComponent::openMode
| juce::FileBrowserComponent::canSelectFiles;
chooser->launchAsync (chooserFlags, [this] (const juce::FileChooser& fc) // [8]
{
auto file = fc.getResult();
if (file != juce::File{}) // [9]
{
auto* reader = formatManager.createReaderFor (file); // [10]
if (reader != nullptr)
{
// Get the number of channels of the audio file
int numChannels = reader->numChannels;
// if the number of channels is different from 1, aleert the user and return
if (numChannels != 1)
{
juce::AlertWindow::showMessageBoxAsync(juce::AlertWindow::WarningIcon, "Error", "The audio file must be mono", "OK");
return;
}
auto newSource = std::make_unique<juce::AudioFormatReaderSource> (reader, true); // [11]
transportSource.setSource (newSource.get(), 0, nullptr, reader->sampleRate); // [12]
playButton.setEnabled (true); // [13]
readerSource.reset (newSource.release()); // [14]
String sourceName = file.getFileNameWithoutExtension();
LoadSource(sourceName, SOURCE1_INITIAL_AZIMUTH, SOURCE1_INITIAL_ELEVATION, SOURCE1_INITIAL_DISTANCE);
juce::AlertWindow::showMessageBoxAsync(juce::AlertWindow::InfoIcon, "Success", "Audio file loaded successfully", "OK");
}
}
});
}
void playButtonClicked()
{
changeState (Starting);
}
void stopButtonClicked()
{
changeState (Stopping);
}
//==========================================================================
juce::TextButton openSOFAButton;
juce::TextButton openWavButton;
juce::TextButton playButton;
juce::TextButton stopButton;
juce::Label sourceAzimuthLabel;
juce::Slider sourceAzimuthDial;
juce::Label sourceElevationLabel;
juce::Slider sourceElevationDial;
juce::Label sourceDistanceLabel;
juce::Slider sourceDistanceDial;
juce::OwnedArray<Button> sofaFileButtons;
juce::Label sampleRateLabel;
std::unique_ptr<juce::FileChooser> chooser;
juce::AudioFormatManager formatManager;
std::unique_ptr<juce::AudioFormatReaderSource> readerSource;
juce::AudioTransportSource transportSource;
TransportState state;
//==========================================================================
Common::CGlobalParameters globalParameters; // Global BRT parameters
BRTBase::CBRTManager brtManager; // BRT global manager interface
std::shared_ptr<BRTListenerModel::CListenerHRTFbasedModel> listener; // Pointer to listener model
std::shared_ptr<BRTSourceModel::CSourceSimpleModel> source1BRT; // Pointer to audio source model
float sourceAzimuth{ SOURCE1_INITIAL_AZIMUTH };
float sourceElevation{ SOURCE1_INITIAL_ELEVATION };
float sourceDistance{ SOURCE1_INITIAL_DISTANCE };
BRTReaders::CSOFAReader sofaReader; // SOFA reader provided by BRT Library
std::vector<std::shared_ptr<BRTServices::CHRTF>> HRTF_list; // List of HRTFs loaded
int selectedHRTFidx;
HRTFState hrtfState;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
};