robert@464: /* robert@464: ____ _____ _ _ robert@464: | __ )| ____| | / \ robert@464: | _ \| _| | | / _ \ robert@464: | |_) | |___| |___ / ___ \ robert@464: |____/|_____|_____/_/ \_\ robert@464: robert@464: The platform for ultra-low latency audio and sensor processing robert@464: robert@464: http://bela.io robert@464: robert@464: A project of the Augmented Instruments Laboratory within the robert@464: Centre for Digital Music at Queen Mary University of London. robert@464: http://www.eecs.qmul.ac.uk/~andrewm robert@464: robert@464: (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, robert@464: Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, robert@464: Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. robert@464: robert@464: The Bela software is distributed under the GNU Lesser General Public License robert@464: (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt robert@464: */ robert@464: robert@464: #include robert@464: #include robert@464: #include robert@464: #include robert@464: #include robert@464: #include robert@464: #include // to load audio files robert@464: robert@464: #include robert@464: #include "SampleData.h" robert@464: robert@464: using namespace std; robert@464: robert@464: // Load samples from file robert@464: int initFile(string file, SampleData *smp)//float *& smp) robert@464: { robert@464: SNDFILE *sndfile ; robert@464: SF_INFO sfinfo ; robert@464: robert@464: if (!(sndfile = sf_open (file.c_str(), SFM_READ, &sfinfo))) { robert@464: cout << "Couldn't open file " << file << endl; robert@464: return 1; robert@464: } robert@464: robert@464: int numChan = sfinfo.channels; robert@464: if(numChan != 1) robert@464: { robert@464: cout << "Error: " << file << " is not a mono file" << endl; robert@464: return 1; robert@464: } robert@464: robert@464: smp->sampleLen = sfinfo.frames * numChan; robert@464: smp->samples = new float[smp->sampleLen]; robert@464: if(smp == NULL){ robert@464: cout << "Could not allocate buffer" << endl; robert@464: return 1; robert@464: } robert@464: robert@464: int subformat = sfinfo.format & SF_FORMAT_SUBMASK; robert@464: int readcount = sf_read_float(sndfile, smp->samples, smp->sampleLen); robert@464: robert@464: // Pad with zeros in case we couldn't read whole file robert@464: for(int k = readcount; k sampleLen; k++) robert@464: smp->samples[k] = 0; robert@464: robert@464: if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) { robert@464: double scale ; robert@464: int m ; robert@464: robert@464: sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ; robert@464: if (scale < 1e-10) robert@464: scale = 1.0 ; robert@464: else robert@464: scale = 32700.0 / scale ; robert@464: cout << "File samples scale = " << scale << endl; robert@464: robert@464: for (m = 0; m < smp->sampleLen; m++) robert@464: smp->samples[m] *= scale; robert@464: } robert@464: robert@464: sf_close(sndfile); robert@464: robert@464: return 0; robert@464: } robert@464: robert@464: robert@464: // Handle Ctrl-C by requesting that the audio rendering stop robert@464: void interrupt_handler(int var) robert@464: { robert@464: //rt_task_delete ((RT_TASK *) &gTriggerSamplesTask); robert@464: gShouldStop = true; robert@464: } robert@464: robert@464: // Print usage information robert@464: void usage(const char * processName) robert@464: { robert@464: cerr << "Usage: " << processName << " [options]" << endl; robert@464: robert@464: Bela_usage(); robert@464: robert@464: cerr << " --file [-f] filename: Name of the file to load (default is \"longsample.wav\")\n"; robert@464: cerr << " --help [-h]: Print this menu\n"; robert@464: } robert@464: robert@464: int main(int argc, char *argv[]) robert@464: { robert@464: BelaInitSettings settings; // Standard audio settings robert@464: string fileName; // Name of the sample to load robert@464: robert@464: SampleData sampleData; // User define structure to pass data retrieved from file to render function robert@464: sampleData.samples = 0; robert@464: sampleData.sampleLen = -1; robert@464: robert@464: robert@464: struct option customOptions[] = robert@464: { robert@464: {"help", 0, NULL, 'h'}, robert@464: {"file", 1, NULL, 'f'}, robert@464: {NULL, 0, NULL, 0} robert@464: }; robert@464: robert@464: // Set default settings robert@464: Bela_defaultSettings(&settings); robert@464: robert@464: // Parse command-line arguments robert@464: while (1) { robert@464: int c; robert@464: if ((c = Bela_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0) robert@464: break; robert@464: switch (c) { robert@464: case 'h': robert@464: usage(basename(argv[0])); robert@464: exit(0); robert@464: case 'f': robert@464: fileName = string((char *)optarg); robert@464: break; robert@464: case '?': robert@464: default: robert@464: usage(basename(argv[0])); robert@464: exit(1); robert@464: } robert@464: } robert@464: robert@464: if(fileName.empty()){ robert@464: fileName = "filter/longsample.wav"; robert@464: } robert@464: robert@464: if(settings.verbose) { robert@464: cout << "Loading file " << fileName << endl; robert@464: } robert@464: robert@464: // Load file robert@464: if(initFile(fileName, &sampleData) != 0) robert@464: { robert@464: cout << "Error: unable to load samples " << endl; robert@464: return -1; robert@464: } robert@464: robert@464: if(settings.verbose) robert@464: cout << "File contains " << sampleData.sampleLen << " samples" << endl; robert@464: robert@464: robert@464: // Initialise the PRU audio device robert@464: if(Bela_initAudio(&settings, &sampleData) != 0) { robert@464: cout << "Error: unable to initialise audio" << endl; robert@464: return -1; robert@464: } robert@464: robert@464: // Start the audio device running robert@464: if(Bela_startAudio()) { robert@464: cout << "Error: unable to start real-time audio" << endl; robert@464: return -1; robert@464: } robert@464: robert@464: // Set up interrupt handler to catch Control-C and SIGTERM robert@464: signal(SIGINT, interrupt_handler); robert@464: signal(SIGTERM, interrupt_handler); robert@464: robert@464: // Run until told to stop robert@464: while(!gShouldStop) { robert@464: usleep(100000); robert@464: } robert@464: robert@464: // Stop the audio device robert@464: Bela_stopAudio(); robert@464: robert@464: // Clean up any resources allocated for audio robert@464: Bela_cleanupAudio(); robert@464: robert@464: // All done! robert@464: return 0; robert@464: } robert@464: