annotate projects/samples/main.cpp @ 269:ac8eb07afcf5

Oxygen text added to each render.cpp file for the default projects. Text includes project explanation from Wiki, edited in places. Empty project added as a default project. Doxyfile updated. Each of the project locations added to INPUT configuration option. Consider just watching the whole project file so all new projects are automatically pulled through.
author Robert Jack <robert.h.jack@gmail.com>
date Tue, 17 May 2016 15:40:16 +0100
parents 3c3a1357657d
children
rev   line source
victor@1 1 /*
victor@1 2 * main.cpp
victor@1 3 *
victor@1 4 * Created on: Oct 24, 2014
victor@1 5 * Author: Andrew McPherson and Victor Zappi
victor@1 6 */
victor@1 7
victor@1 8 #include <iostream>
victor@1 9 #include <cstdlib>
victor@1 10 #include <libgen.h>
victor@1 11 #include <signal.h>
victor@1 12 #include <string>
andrewm@5 13 #include <getopt.h>
victor@1 14 #include <sndfile.h> // to load audio files
andrewm@56 15
andrewm@56 16 #include <BeagleRT.h>
victor@1 17 #include "SampleData.h"
victor@1 18
victor@1 19 using namespace std;
victor@1 20
victor@1 21 // Load samples from file
victor@1 22 int initFile(string file, SampleData *smp)//float *& smp)
victor@1 23 {
victor@1 24 SNDFILE *sndfile ;
victor@1 25 SF_INFO sfinfo ;
victor@1 26
victor@1 27 if (!(sndfile = sf_open (file.c_str(), SFM_READ, &sfinfo))) {
victor@1 28 cout << "Couldn't open file " << file << endl;
victor@1 29 return 1;
victor@1 30 }
victor@1 31
victor@1 32 int numChan = sfinfo.channels;
victor@1 33 if(numChan != 1)
victor@1 34 {
victor@1 35 cout << "Error: " << file << " is not a mono file" << endl;
victor@1 36 return 1;
victor@1 37 }
victor@1 38
victor@1 39 smp->sampleLen = sfinfo.frames * numChan;
victor@1 40 smp->samples = new float[smp->sampleLen];
victor@1 41 if(smp == NULL){
victor@1 42 cout << "Could not allocate buffer" << endl;
victor@1 43 return 1;
victor@1 44 }
victor@1 45
victor@1 46 int subformat = sfinfo.format & SF_FORMAT_SUBMASK;
victor@1 47 int readcount = sf_read_float(sndfile, smp->samples, smp->sampleLen);
victor@1 48
victor@1 49 // Pad with zeros in case we couldn't read whole file
victor@1 50 for(int k = readcount; k <smp->sampleLen; k++)
victor@1 51 smp->samples[k] = 0;
victor@1 52
victor@1 53 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) {
victor@1 54 double scale ;
victor@1 55 int m ;
victor@1 56
victor@1 57 sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ;
victor@1 58 if (scale < 1e-10)
victor@1 59 scale = 1.0 ;
victor@1 60 else
victor@1 61 scale = 32700.0 / scale ;
victor@1 62 cout << "File samples scale = " << scale << endl;
victor@1 63
victor@1 64 for (m = 0; m < smp->sampleLen; m++)
victor@1 65 smp->samples[m] *= scale;
victor@1 66 }
victor@1 67
victor@1 68 sf_close(sndfile);
victor@1 69
victor@1 70 return 0;
victor@1 71 }
victor@1 72
victor@1 73
victor@1 74 // Handle Ctrl-C by requesting that the audio rendering stop
victor@1 75 void interrupt_handler(int var)
victor@1 76 {
victor@1 77 //rt_task_delete ((RT_TASK *) &gTriggerSamplesTask);
victor@1 78 gShouldStop = true;
victor@1 79 }
victor@1 80
victor@1 81 // Print usage information
victor@1 82 void usage(const char * processName)
victor@1 83 {
andrewm@5 84 cerr << "Usage: " << processName << " [options]" << endl;
andrewm@5 85
andrewm@5 86 BeagleRT_usage();
andrewm@5 87
andrewm@5 88 cerr << " --file [-f] filename: Name of the file to load (default is \"sample.wav\")\n";
andrewm@5 89 cerr << " --help [-h]: Print this menu\n";
victor@1 90 }
victor@1 91
victor@1 92 int main(int argc, char *argv[])
victor@1 93 {
andrewm@52 94 BeagleRTInitSettings settings; // Standard audio settings
victor@1 95 string fileName; // Name of the sample to load
victor@1 96
victor@1 97 SampleData sampleData; // User define structure to pass data retrieved from file to render function
victor@1 98 sampleData.samples = 0;
victor@1 99 sampleData.sampleLen = -1;
victor@1 100
andrewm@5 101
andrewm@5 102 struct option customOptions[] =
andrewm@5 103 {
andrewm@5 104 {"help", 0, NULL, 'h'},
andrewm@5 105 {"file", 1, NULL, 'f'},
andrewm@5 106 {NULL, 0, NULL, 0}
andrewm@5 107 };
andrewm@5 108
andrewm@5 109 // Set default settings
andrewm@5 110 BeagleRT_defaultSettings(&settings);
andrewm@5 111
victor@1 112 // Parse command-line arguments
victor@1 113 while (1) {
victor@1 114 int c;
andrewm@5 115 if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
victor@1 116 break;
victor@1 117 switch (c) {
victor@1 118 case 'h':
victor@1 119 usage(basename(argv[0]));
victor@1 120 exit(0);
victor@1 121 case 'f':
victor@1 122 fileName = string((char *)optarg);
victor@1 123 break;
victor@1 124 case '?':
victor@1 125 default:
victor@1 126 usage(basename(argv[0]));
victor@1 127 exit(1);
victor@1 128 }
victor@1 129 }
victor@1 130
victor@1 131 if(fileName.empty()){
victor@8 132 fileName = "samples/sample.wav";
victor@1 133 }
victor@1 134
andrewm@5 135 if(settings.verbose) {
victor@1 136 cout << "Loading file " << fileName << endl;
victor@1 137 }
victor@1 138
victor@1 139 // Load file
victor@1 140 if(initFile(fileName, &sampleData) != 0)
victor@1 141 {
victor@1 142 cout << "Error: unable to load samples " << endl;
victor@1 143 return -1;
victor@1 144 }
victor@1 145
andrewm@5 146 if(settings.verbose)
victor@1 147 cout << "File contains " << sampleData.sampleLen << " samples" << endl;
victor@1 148
andrewm@5 149
victor@1 150 // Initialise the PRU audio device
andrewm@5 151 if(BeagleRT_initAudio(&settings, &sampleData) != 0) {
victor@1 152 cout << "Error: unable to initialise audio" << endl;
victor@1 153 return -1;
victor@1 154 }
victor@1 155
victor@1 156 // Start the audio device running
andrewm@5 157 if(BeagleRT_startAudio()) {
victor@1 158 cout << "Error: unable to start real-time audio" << endl;
victor@1 159 return -1;
victor@1 160 }
victor@1 161
andrewm@15 162 // Set up interrupt handler to catch Control-C and SIGTERM
victor@1 163 signal(SIGINT, interrupt_handler);
andrewm@15 164 signal(SIGTERM, interrupt_handler);
victor@1 165
victor@1 166 // Run until told to stop
victor@1 167 while(!gShouldStop) {
victor@1 168 usleep(100000);
victor@1 169 }
victor@1 170
victor@1 171 // Stop the audio device
andrewm@5 172 BeagleRT_stopAudio();
victor@1 173
victor@1 174 // Clean up any resources allocated for audio
andrewm@5 175 BeagleRT_cleanupAudio();
victor@1 176
victor@1 177 // All done!
victor@1 178 return 0;
victor@1 179 }