#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include "AudioFile.h"

/*
   data=importdata("sine-wave.dat");
   plot(data(':',1),data(':',2))
   xlabel('t/s')
   ylabel('amplitude/AU')
*/

using namespace std;

int main()
{
     //---------------------------------------------------------------
     // 1. Set a file path to an audio file on your machine

     const std::string base   = "sine-wave";
     const std::string input  = base+".wav";
     const std::string output = base+".dat";
        
     //---------------------------------------------------------------
     // 2. Create an AudioFile object and load the audio file
        
     AudioFile<float> a;
     bool loadedOK = a.load(input);
     assert (loadedOK);

     cout << "Read: "              << input                  << endl;
     cout << "Bit Depth: "         << a.getBitDepth()        << endl;
     cout << "Sample Rate: "       << a.getSampleRate()      << endl;
     cout << "Num Channels: "      << a.getNumChannels()     << endl;
     cout << "Length in Seconds: " << a.getLengthInSeconds() << endl;    
     cout << "Saving to: "         << output                 << endl;

     double rate=a.getSampleRate();

     //---------------------------------------------------------------
     // 3. Let's save this wave for plotting
        
     ofstream fo(output);
     int channel=0;     
     for (int i = 0; i < a.getNumSamplesPerChannel(); i++)
        {
            double t=1.0*i/rate;
            fo <<  t << " " << a.samples[channel][i] << endl;
        }
    fo.close();
}
