Trying to get recording to work


#1

So I’m trying to do audio recording and playback, but can’t seem to get it to work. The only reason I have audioDeviceManager commented out of my code is cause idk how to initialize it without doing it like it’s done in the demo, and the MainWindow.h keeps on giving me an architecture error whenever I try to compile. Any Help would be greatly appreciated.

/*
    ===========================================================    ===================

    This file was auto-generated!

  ===========================================================    ===================
*/

 #ifndef MAINCOMPONENT_H_INCLUDED
#define MAINCOMPONENT_H_INCLUDED

#include "../JuceLibraryCode/JuceHeader.h"
#include "Display.h"
#include "AudioVisual.h"

//#include "MainWindow.h"
//==============================================================================
/*
    This component lives inside our window, and this is where you should put all
    your controls and content.
*/

class recorda   :   public AudioIODeviceCallback
{

public:
    recorda (AudioThumbnail& thumbnailToUpdate)
        :   thumbnail(thumbnailToUpdate),
            backgroundThread ("Audio Recorder Thread"),
            sampleRate(0), nextSampleNum (0), activeWriter(nullptr)
    {
        backgroundThread.startThread();
    }
    
    ~recorda()
    {
        stop();
    }
    
    void startRec(const File& file)
    {
        stop();
        
        if (sampleRate > 0)
        {
            file.deleteFile();
            ScopedPointer<FileOutputStream> fileStream (file.createOutputStream());
            
            if  (fileStream != nullptr)
            {
                WavAudioFormat wavFormat;
                AudioFormatWriter* writer = wavFormat.createWriterFor (fileStream,sampleRate, 1, 16, StringPairArray(), 0);
                
                if (writer != nullptr)
                {
                    fileStream.release();
                    
                    threadedWriter = new AudioFormatWriter::ThreadedWriter (writer, backgroundThread, 32768);
                    
                    thumbnail.reset (writer ->getNumChannels(), writer->getSampleRate());
                    nextSampleNum = 0;
                    
                    const ScopedLock sl (writerLock);
                    activeWriter = threadedWriter;
                }
            }
        }
    }
    
    void stop()
    {
        {
            const ScopedLock sl (writerLock);
            activeWriter = nullptr;
        }
    
        threadedWriter = nullptr;
    }
    
    bool isRec() const
    {
        return activeWriter != nullptr;
    }
    
    void audioDeviceAboutToStart (AudioIODevice* device) override
    {
        sampleRate = device -> getCurrentSampleRate();
    }
    
    void audioDeviceStopped() override
    {
        sampleRate = 0;
    }
    
    void audioDeviceIOCallback (const float** inputChannelData, int /*numInputChannels*/,
                                float** outputChannelData, int numOutputChannels,
                                int numSamples) override
    {
        const ScopedLock sl (writerLock);
        
        if (activeWriter != nullptr)
        {
            activeWriter->write (inputChannelData, numSamples);
            
            const AudioSampleBuffer buffer(const_cast<float**>(inputChannelData), thumbnail.getNumChannels(), numSamples);
            thumbnail.addBlock (nextSampleNum, buffer, 0, numSamples);
            nextSampleNum += numSamples;
        }
        
        for (int i = 0; i < numOutputChannels; ++i)
            if (outputChannelData[i] != nullptr)
                FloatVectorOperations::clear (outputChannelData[i], numSamples);
    }
    
    
private:
    AudioThumbnail& thumbnail;
    TimeSliceThread backgroundThread;
    ScopedPointer<AudioFormatWriter::ThreadedWriter> threadedWriter;
    double sampleRate;
    int64 nextSampleNum;
    
    CriticalSection writerLock;
    AudioFormatWriter::ThreadedWriter* volatile activeWriter;
};
    
class RecordingThumb    : public Component,
                          private ChangeListener
{
public:
    RecordingThumb()
        :thumbnailCache (10),
         thumbnail (512, formatManager, thumbnailCache),
         displayFullThumb(false)
         
    {
        formatManager.registerBasicFormats();
        thumbnail.addChangeListener (this);
    }
    
    ~RecordingThumb()
    {
        thumbnail.removeChangeListener (this);
    }
    
    AudioThumbnail& getAudioThumbnail() {return thumbnail;}
    
    void displayFullThumbnail (bool displayFull)
    {
        displayFullThumb = displayFull;
        repaint();
    }
    
    void paint (Graphics& g)override
    {
        g.fillAll (Colours::darkgrey);
        g.setColour (Colours::lightgrey);
        
        if (thumbnail.getTotalLength() > 0.0)
        {
            const double endTime = displayFullThumb ? thumbnail.getTotalLength() : jmax (30.0, thumbnail.getTotalLength());
            
            Rectangle<int> thumbArea (getLocalBounds());
            thumbnail.drawChannels (g, thumbArea.reduced (2), 0.0, endTime, 1.0f);
        }
        else
        {
            g.setFont(14.0f);
            g.drawFittedText("(No file recorded)", getLocalBounds(), Justification::centred, 2);
        }
    }
    
private:

    AudioFormatManager formatManager;
    AudioThumbnailCache thumbnailCache;
    AudioThumbnail thumbnail;

    bool displayFullThumb;

    void changeListenerCallback(ChangeBroadcaster* source) override
        {
            if(source == &thumbnail)
                repaint();
        }
    
        JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(RecordingThumb)
};

class MainContentComponent   : public AudioAppComponent,
                               public ChangeListener,
                               
                               public Button::Listener
{
public:

enum TransportState
    {
        Stopped,
        Starting,
        Playing,
        Stopping,
        //Recording, 
        //StopRecording
    };
    //==============================================================================
    MainContentComponent()
    : /*deviceManager(MainAppWindow::getSharedAudioDeviceManager()),*/
      recorder(recordingThumb.getAudioThumbnail())
      
    {
        
        
        addAndMakeVisible(&openButton);
        openButton.setButtonText("Open...");
        openButton.addListener(this);
        
        addAndMakeVisible (&playButton);
        playButton.setButtonText("Play");
        playButton.addListener(this);
        playButton.setColour(TextButton::buttonColourId, Colours::green);
        playButton.setEnabled(false);
        
        addAndMakeVisible(&stopButton);
        stopButton.setButtonText("Stop");
        stopButton.addListener(this);
        stopButton.setColour(TextButton::buttonColourId, Colours::red);
        stopButton.setEnabled(false);
        
        addAndMakeVisible (&recordingButton);
        recordingButton.setButtonText ("Record");
        recordingButton.addListener (this);
        recordingButton.setColour (TextButton::buttonColourId, Colour (0xffff5c6f));
        recordingButton.setColour (TextButton::textColourOnId, Colours::black);
        recordingButton.setEnabled(true);
        
        addAndMakeVisible(display);
        addAndMakeVisible(display2);
        addAndMakeVisible(display3);
        addAndMakeVisible(display4);
        setSize (800, 600);
        
        formatManager.registerBasicFormats();
        transportSource.addChangeListener(this);
        //deviceManager.removeAudioCallback(&audioVis);
        //deviceManager.removeAudioCallback(&recorder);

        // specify the number of input and output channels that we want to open
        setAudioChannels (2, 2);
    }

    ~MainContentComponent()
    {
        shutdownAudio();
        //deviceManager.removeAudioCallback (&recorder);
        
        //deviceManager.removeAudioCallback(&audioVis);
    }
    
    void changeListenerCallback (ChangeBroadcaster* source) override
    {
        /*if(source == &transportSource)
        {
            if(transportSource.isPlaying())
                changeState(Playing);
            else
            changeState (Stopped);
        }*/
        
    }
    
   /* void changeState (TransportState newState)
    {
        if (state != newState)
        {
            state = newState;
            
            switch (state)
            {
                case Stopped:
                
                stopButton.setEnabled(false);
                playButton.setEnabled(true);
                transportSource.setPosition(0.0);
                break;
                
                case Starting:
                playButton.setEnabled(false);
                transportSource.start();
                break;
                
                case Playing:
                stopButton.setEnabled(true);
                break;
                
                case Stopping:
                transportSource.stop();
                break;
                
                
            }
        }
    }*/

    //=======================================================================
   void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override
    {
       
        
        //transportSource.prepareToPlay (samplesPerBlockExpected, sampleRate);
    }

    void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
    {
       
        /*if(readerSource == nullptr)
        {
        bufferToFill.clearActiveBufferRegion();
        return;
        }
        transportSource.getNextAudioBlock(bufferToFill);*/
    }


    void releaseResources() override
    {
        
        /*transportSource.releaseResources();*/
    }

    //=======================================================================
    void paint (Graphics& g) override
    {
        // (Our component is opaque, so we must completely fill the background with a solid colour)
        g.fillAll (Colours::lightblue);


        // You can add your drawing code here!
    }

    void resized() override
    {
       //openButton.setBounds(10, 15, (getWidth()/4) - 20, 20);
       //playButton.setBounds(10, 45, (getWidth()/4) - 20, 20);
       //stopButton.setBounds(10, 75, (getWidth()/4) -20, 20);
        
        //display.setBounds(200,5,500,200);
        //display2.setBounds(200,150,500,200);
        //display3.setBounds(200,275,500,249);
        //display4.setBounds(200,400,500,200);
        
        Rectangle<int> stuff (getLocalBounds());
        //audioVis.setBounds(stuff.removeFromTop(80).reduced (8));
        recordingThumb.setBounds(stuff.removeFromTop(80).reduced(8));
        recordingButton.setBounds (stuff.removeFromTop (80).removeFromLeft (140).reduced (8));
        //openButton.setBounds(stuff.removeFromTop(50).removeFromLeft(140).reduced(8));
        //playButton.setBounds(stuff.removeFromTop(36).removeFromLeft(140).reduced(8));
         
        
        
    }
    
    void buttonClicked (Button* button) override
    {
        //if(button == &openButton) openButtonClicked();
        //if(button == &playButton) playButtonClicked();
        //if(button == &stopButton) stopButtonClicked();
        if (button == &recordingButton)
        {
            if(recorder.isRec())
                stopRecording();
            else
            startRecording();
        }
    }
    
    void openButtonClicked()
    {
        FileChooser chooser("Select a Wave file to play...", File::nonexistent, "*.wav");
        
        if(chooser.browseForFileToOpen())
        {
            File file(chooser.getResult());
            AudioFormatReader* reader = formatManager.createReaderFor(file);
            
            if (reader != nullptr)
            {
                ScopedPointer<AudioFormatReaderSource> newSource = new AudioFormatReaderSource(reader, true);
                transportSource.setSource (newSource, 0, nullptr, reader->sampleRate);
                playButton.setEnabled(true);
                readerSource = newSource.release();
            }
        }
    }
    
    void startRecording()
    {
       
        const File file (File::getSpecialLocation(File::userDocumentsDirectory).getNonexistentChildFile("Recording",".wav"));
        recorder.startRec(file);
        recordingButton.setButtonText("Stop");
        recordingThumb.displayFullThumbnail(false);
    }
    void stopRecording()
    {
        
        recorder.stop();
        recordingButton.setButtonText("Record");
        recordingThumb.displayFullThumbnail(true);
    }
    
    
            
    
   /* void playButtonClicked()
    {
        changeState(Starting);
        recordingThumb.displayFullThumbnail(true);
    }
    
    void stopButtonClicked()
    {
        changeState (Stopping);
        recordingThumb.displayFullThumbnail(true);
    }*/


private:
    //==============================================================================

    // Your private member variables go here...

    AudioFormatManager formatManager;
    //AudioDeviceManager& deviceManager;
    AudioVisual audioVis;
    RecordingThumb recordingThumb;
    recorda recorder;
    //LiveScrollingAudioDisplay liveAudioScroller;
    ScopedPointer<AudioFormatReaderSource> readerSource;
    AudioTransportSource transportSource;
    TransportState state;
    TextButton openButton;
    TextButton playButton;
    TextButton stopButton;
    TextButton recordingButton;
    Display display;
    Display display2;
    Display display3;
    Display display4;
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
};


// (This function is called by the app startup code to create our main component)
Component* createMainContentComponent()     { return new MainContentComponent(); }


#endif  // MAINCOMPONENT_H_INCLUDED

#2

Please add four spaces to the left of each of your lines of code, that will automatically turn on code formatting and will make your post readable


#3

done. Sorry about that