2 ==============================================================================
4 This is an automatically generated file created by the Jucer!
6 Creation date: 1 May 2011 12:08:25pm
8 Be careful when adding custom code to these files, as only the code within
9 the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded
14 ------------------------------------------------------------------------------
16 The Jucer is part of the JUCE library - "Jules' Utility Class Extensions"
17 Copyright 2004-6 by Raw Material Software ltd.
19 ==============================================================================
22 //[Headers] You can add your own extra header files here...
25 #include "AudioDemoRecordPage.h"
28 //[MiscUserDefs] You can add your own user definitions and misc code here...
31 //==============================================================================
32 /** A simple class that acts as an AudioIODeviceCallback and writes the
33 incoming audio data to a WAV file.
35 class AudioRecorder
: public AudioIODeviceCallback
39 : backgroundThread ("Audio Recorder Thread"),
40 sampleRate (0), activeWriter (0)
42 backgroundThread
.startThread();
50 //==============================================================================
51 void startRecording (const File
& file
)
57 // Create an OutputStream to write to our destination file...
59 ScopedPointer
<FileOutputStream
> fileStream (file
.createOutputStream());
63 // Now create a WAV writer object that writes to our output stream...
64 WavAudioFormat wavFormat
;
65 AudioFormatWriter
* writer
= wavFormat
.createWriterFor (fileStream
, sampleRate
, 1, 16, StringPairArray(), 0);
69 fileStream
.release(); // (passes responsibility for deleting the stream to the writer object that is now using it)
71 // Now we'll create one of these helper objects which will act as a FIFO buffer, and will
72 // write the data to disk on our background thread.
73 threadedWriter
= new AudioFormatWriter::ThreadedWriter (writer
, backgroundThread
, 32768);
75 // And now, swap over our active writer pointer so that the audio callback will start using it..
76 const ScopedLock
sl (writerLock
);
77 activeWriter
= threadedWriter
;
85 // First, clear this pointer to stop the audio callback from using our writer object..
87 const ScopedLock
sl (writerLock
);
91 // Now we can delete the writer object. It's done in this order because the deletion could
92 // take a little time while remaining data gets flushed to disk, so it's best to avoid blocking
93 // the audio callback while this happens.
97 bool isRecording() const
99 return activeWriter
!= 0;
102 //==============================================================================
103 void audioDeviceAboutToStart (AudioIODevice
* device
)
105 sampleRate
= device
->getCurrentSampleRate();
108 void audioDeviceStopped()
113 void audioDeviceIOCallback (const float** inputChannelData
, int /*numInputChannels*/,
114 float** outputChannelData
, int numOutputChannels
,
117 const ScopedLock
sl (writerLock
);
119 if (activeWriter
!= 0)
120 activeWriter
->write (inputChannelData
, numSamples
);
122 // We need to clear the output buffers, in case they're full of junk..
123 for (int i
= 0; i
< numOutputChannels
; ++i
)
124 if (outputChannelData
[i
] != 0)
125 zeromem (outputChannelData
[i
], sizeof (float) * numSamples
);
129 TimeSliceThread backgroundThread
; // the thread that will write our audio data to disk
130 ScopedPointer
<AudioFormatWriter::ThreadedWriter
> threadedWriter
; // the FIFO used to buffer the incoming data
133 CriticalSection writerLock
;
134 AudioFormatWriter::ThreadedWriter
* volatile activeWriter
;
139 //==============================================================================
140 AudioDemoRecordPage::AudioDemoRecordPage (AudioDeviceManager
& deviceManager_
)
141 : deviceManager (deviceManager_
),
142 liveAudioDisplayComp (0),
143 explanationLabel (0),
146 addAndMakeVisible (liveAudioDisplayComp
= new LiveAudioInputDisplayComp());
148 addAndMakeVisible (explanationLabel
= new Label (String::empty
,
149 L
"This page demonstrates how to record a wave file from the live audio input..\n\nPressing record will start recording a file in your \"Documents\" folder."));
150 explanationLabel
->setFont (Font (15.0000f
, Font::plain
));
151 explanationLabel
->setJustificationType (Justification::topLeft
);
152 explanationLabel
->setEditable (false, false, false);
153 explanationLabel
->setColour (TextEditor::textColourId
, Colours::black
);
154 explanationLabel
->setColour (TextEditor::backgroundColourId
, Colour (0x0));
156 addAndMakeVisible (recordButton
= new TextButton (String::empty
));
157 recordButton
->setButtonText (L
"Record");
158 recordButton
->addListener (this);
159 recordButton
->setColour (TextButton::buttonColourId
, Colour (0xffff5c5c));
160 recordButton
->setColour (TextButton::textColourOnId
, Colours::black
);
169 //[Constructor] You can add your own custom stuff here..
170 recorder
= new AudioRecorder();
171 deviceManager
.addAudioCallback (recorder
);
172 deviceManager
.addAudioCallback (liveAudioDisplayComp
);
176 AudioDemoRecordPage::~AudioDemoRecordPage()
178 //[Destructor_pre]. You can add your own custom destruction code here..
179 deviceManager
.removeAudioCallback (recorder
);
180 deviceManager
.removeAudioCallback (liveAudioDisplayComp
);
184 deleteAndZero (liveAudioDisplayComp
);
185 deleteAndZero (explanationLabel
);
186 deleteAndZero (recordButton
);
189 //[Destructor]. You can add your own custom destruction code here..
193 //==============================================================================
194 void AudioDemoRecordPage::paint (Graphics
& g
)
196 //[UserPrePaint] Add your own custom painting code here..
199 g
.fillAll (Colours::lightgrey
);
201 //[UserPaint] Add your own custom painting code here..
205 void AudioDemoRecordPage::resized()
207 liveAudioDisplayComp
->setBounds (8, 8, getWidth() - 16, 64);
208 explanationLabel
->setBounds (160, 88, getWidth() - 169, 216);
209 recordButton
->setBounds (8, 88, 136, 40);
210 //[UserResized] Add your own custom resize handling here..
214 void AudioDemoRecordPage::buttonClicked (Button
* buttonThatWasClicked
)
216 //[UserbuttonClicked_Pre]
217 //[/UserbuttonClicked_Pre]
219 if (buttonThatWasClicked
== recordButton
)
221 //[UserButtonCode_recordButton] -- add your button handler code here..
222 if (recorder
->isRecording())
228 File
file (File::getSpecialLocation (File::userDocumentsDirectory
)
229 .getNonexistentChildFile ("Juce Demo Audio Recording", ".wav"));
231 recorder
->startRecording (file
);
234 if (recorder
->isRecording())
235 recordButton
->setButtonText ("Stop");
237 recordButton
->setButtonText ("Record");
239 //[/UserButtonCode_recordButton]
242 //[UserbuttonClicked_Post]
243 //[/UserbuttonClicked_Post]
246 void AudioDemoRecordPage::visibilityChanged()
248 //[UserCode_visibilityChanged] -- Add your code here...
250 recordButton
->setButtonText ("Record");
251 //[/UserCode_visibilityChanged]
256 //[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
260 //==============================================================================
262 /* -- Jucer information section --
264 This is where the Jucer puts all of its metadata, so don't change anything in here!
268 <JUCER_COMPONENT documentType="Component" className="AudioDemoRecordPage" componentName=""
269 parentClasses="public Component" constructorParams="AudioDeviceManager& deviceManager_"
270 variableInitialisers="deviceManager (deviceManager_)" snapPixels="8"
271 snapActive="1" snapShown="1" overlayOpacity="0.330000013" fixedSize="0"
272 initialWidth="600" initialHeight="400">
274 <METHOD name="visibilityChanged()"/>
276 <BACKGROUND backgroundColour="ffd3d3d3"/>
277 <GENERICCOMPONENT name="" id="7d70eb2617f56220" memberName="liveAudioDisplayComp"
278 virtualName="" explicitFocusOrder="0" pos="8 8 16M 64" class="LiveAudioInputDisplayComp"
280 <LABEL name="" id="1162fb2599a768b4" memberName="explanationLabel" virtualName=""
281 explicitFocusOrder="0" pos="160 88 169M 216" edTextCol="ff000000"
282 edBkgCol="0" labelText="This page demonstrates how to record a wave file from the live audio input.. Pressing record will start recording a file in your "Documents" folder."
283 editableSingleClick="0" editableDoubleClick="0" focusDiscardsChanges="0"
284 fontname="Default font" fontsize="15" bold="0" italic="0" justification="9"/>
285 <TEXTBUTTON name="" id="2c10a0ba9fad39da" memberName="recordButton" virtualName=""
286 explicitFocusOrder="0" pos="8 88 136 40" bgColOff="ffff5c5c"
287 textCol="ff000000" buttonText="Record" connectedEdges="0" needsCallback="1"