annotate projects/audio_in_FFT/main.cpp @ 4:f34c63568523

_new FFT example [still noisy]
author Victor Zappi <victor.zappi@qmul.ac.uk>
date Thu, 06 Nov 2014 19:02:48 +0000
parents
children 09f03ac40fcc
rev   line source
victor@4 1 /*
victor@4 2 * main.cpp
victor@4 3 *
victor@4 4 * Created on: Oct 24, 2014
victor@4 5 * Author: parallels
victor@4 6 */
victor@4 7
victor@4 8 #include <iostream>
victor@4 9 #include <cstdlib>
victor@4 10 #include <libgen.h>
victor@4 11 #include <signal.h>
victor@4 12 #include "../../include/RTAudio.h"
victor@4 13
victor@4 14 using namespace std;
victor@4 15
victor@4 16 // Handle Ctrl-C by requesting that the audio rendering stop
victor@4 17 void interrupt_handler(int var)
victor@4 18 {
victor@4 19 gShouldStop = true;
victor@4 20 }
victor@4 21
victor@4 22 // Print usage information
victor@4 23 void usage(const char * processName)
victor@4 24 {
victor@4 25 cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f frequency]" << endl;
victor@4 26 cerr << " -h: Print this menu\n";
victor@4 27 cerr << " -v: Enable verbose messages\n";
victor@4 28 cerr << " -p period: Set the period (hardware buffer) size in sensor frames\n";
victor@4 29 cerr << " -s fftsize: Set the fSize of the FFT, in samples\n";
victor@4 30 cerr << " -m: Enable the matrix (ADC and DAC) as well as audio\n";
victor@4 31 }
victor@4 32
victor@4 33 int main(int argc, char *argv[])
victor@4 34 {
victor@4 35 int periodSize = 8; // Period size in sensor frames
victor@4 36 int verbose = 0; // Verbose printing level
victor@4 37 int fftSize = 64; // Size of the FFT, in samples
victor@4 38 int useMatrix = 0; // Whether to use the matrix or just audio
victor@4 39
victor@4 40 // Parse command-line arguments
victor@4 41 while (1) {
victor@4 42 int c;
victor@4 43 if ((c = getopt(argc, argv, "hp:vf:m")) < 0)
victor@4 44 break;
victor@4 45 switch (c) {
victor@4 46 case 'h':
victor@4 47 usage(basename(argv[0]));
victor@4 48 exit(0);
victor@4 49 case 'p':
victor@4 50 periodSize = atoi(optarg);
victor@4 51 if(periodSize < 1)
victor@4 52 periodSize = 1;
victor@4 53 break;
victor@4 54 case 'v':
victor@4 55 verbose = 1;
victor@4 56 break;
victor@4 57 case 's':
victor@4 58 fftSize = atof(optarg);
victor@4 59 break;
victor@4 60 case 'm':
victor@4 61 useMatrix = 1;
victor@4 62 break;
victor@4 63 case '?':
victor@4 64 default:
victor@4 65 usage(basename(argv[0]));
victor@4 66 exit(1);
victor@4 67 }
victor@4 68 }
victor@4 69
victor@4 70
victor@4 71 // Set verbose logging information (optional by using value > 0; default is 0)
victor@4 72 setVerboseLevel(verbose);
victor@4 73
victor@4 74 if(verbose) {
victor@4 75 cout << "Starting with period size " << periodSize << " and FFT size " << fftSize << endl;
victor@4 76 if(useMatrix)
victor@4 77 cout << "Matrix enabled\n";
victor@4 78 else
victor@4 79 cout << "Matrix disabled\n";
victor@4 80 }
victor@4 81
victor@4 82 // Initialise the PRU audio device
victor@4 83 if(initAudio(periodSize, useMatrix, &fftSize) != 0) {
victor@4 84 cout << "Error: unable to initialise audio" << endl;
victor@4 85 return -1;
victor@4 86 }
victor@4 87
victor@4 88 // Start the audio device running
victor@4 89 if(startAudio()) {
victor@4 90 cout << "Error: unable to start real-time audio" << endl;
victor@4 91 return -1;
victor@4 92 }
victor@4 93
victor@4 94 // Set up interrupt handler to catch Control-C
victor@4 95 signal(SIGINT, interrupt_handler);
victor@4 96
victor@4 97 // Run until told to stop
victor@4 98 while(!gShouldStop) {
victor@4 99 usleep(100000);
victor@4 100 }
victor@4 101
victor@4 102 // Stop the audio device
victor@4 103 stopAudio();
victor@4 104
victor@4 105 if(verbose) {
victor@4 106 cout << "Cleaning up..." << endl;
victor@4 107 }
victor@4 108
victor@4 109 // Clean up any resources allocated for audio
victor@4 110 cleanupAudio();
victor@4 111
victor@4 112 // All done!
victor@4 113 return 0;
victor@4 114 }