Mercurial > hg > beaglert
changeset 2:021ac8a1a4f9
_new FIR filter example
author | Victor Zappi <victor.zappi@qmul.ac.uk> |
---|---|
date | Thu, 06 Nov 2014 15:59:16 +0000 |
parents | 24fc8026ae8e |
children | 6810f166482f |
files | .cproject projects/filter_FIR/FIRfilter.h projects/filter_FIR/SampleData.h projects/filter_FIR/longsample.wav projects/filter_FIR/main.cpp projects/filter_FIR/render.cpp projects/samples/main.cpp |
diffstat | 7 files changed, 438 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/.cproject Thu Nov 06 14:23:26 2014 +0000 +++ b/.cproject Thu Nov 06 15:59:16 2014 +0000 @@ -82,9 +82,9 @@ </toolChain> </folderInfo> <sourceEntries> + <entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="include"/> + <entry excluding="oscillator_bank|samples|basic_sensor|d-box|basic_analog_output|basic" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="projects"/> <entry excluding="audio_routines_old.S" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="core"/> - <entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="include"/> - <entry excluding="oscillator_bank|d-box|basic_sensor|basic|basic_analog_output" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="projects"/> </sourceEntries> </configuration> </storageModule> @@ -155,9 +155,9 @@ </toolChain> </folderInfo> <sourceEntries> + <entry excluding="oscillator_bank|samples|basic_sensor|d-box|basic_analog_output|basic" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="projects"/> + <entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="include"/> <entry excluding="audio_routines_old.S" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="core"/> - <entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="include"/> - <entry excluding="oscillator_bank|d-box|basic_sensor|basic|basic_analog_output" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="projects"/> </sourceEntries> </configuration> </storageModule>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/filter_FIR/FIRfilter.h Thu Nov 06 15:59:16 2014 +0000 @@ -0,0 +1,51 @@ +/* + * FIRfilter.h + * + * Created on: Aug 5, 2014 + * Author: Victor Zappi and Andrew McPherson + */ + +#ifndef FIRFILTER_H_ +#define FIRFILTER_H_ + + +#include <NE10.h> + +#define FILTER_TAP_NUM 31 + +// Coefficients for FIR High Pass Filter at 3 KHz +ne10_float32_t filterTaps[FILTER_TAP_NUM] = { + -0.000055, + 0.000318, + 0.001401, + 0.003333, + 0.005827, + 0.007995, + 0.008335, + 0.004991, + -0.003764, + -0.018906, + -0.040112, + -0.065486, + -0.091722, + -0.114710, + -0.130454, + 0.863946, + -0.130454, + -0.114710, + -0.091722, + -0.065486, + -0.040112, + -0.018906, + -0.003764, + 0.004991, + 0.008335, + 0.007995, + 0.005827, + 0.003333, + 0.001401, + 0.000318, + -0.000055 +}; + +#endif /* FIRFILTER_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/filter_FIR/SampleData.h Thu Nov 06 15:59:16 2014 +0000 @@ -0,0 +1,19 @@ +/* + * SampleData.h + * + * Created on: Nov 5, 2014 + * Author: Victor Zappi + */ + +#ifndef SAMPLEDATA_H_ +#define SAMPLEDATA_H_ + +// User defined structure to pass between main and rendere complex data retrieved from file +struct SampleData { + float *samples; // Samples in file + int sampleLen; // Total nume of samples +}; + + + +#endif /* SAMPLEDATA_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/filter_FIR/main.cpp Thu Nov 06 15:59:16 2014 +0000 @@ -0,0 +1,190 @@ +/* + * main.cpp + * + * Created on: Oct 24, 2014 + * Author: Andrew McPherson and Victor Zappi + */ + +#include <iostream> +#include <cstdlib> +#include <libgen.h> +#include <signal.h> +#include <string> +#include <sndfile.h> // to load audio files +#include "../../include/RTAudio.h" +#include "SampleData.h" + +using namespace std; + +int gPeriodSize = 8; // Period size in sensor frames + +// Load samples from file +int initFile(string file, SampleData *smp)//float *& smp) +{ + SNDFILE *sndfile ; + SF_INFO sfinfo ; + + if (!(sndfile = sf_open (file.c_str(), SFM_READ, &sfinfo))) { + cout << "Couldn't open file " << file << endl; + return 1; + } + + int numChan = sfinfo.channels; + if(numChan != 1) + { + cout << "Error: " << file << " is not a mono file" << endl; + return 1; + } + + smp->sampleLen = sfinfo.frames * numChan; + smp->samples = new float[smp->sampleLen]; + if(smp == NULL){ + cout << "Could not allocate buffer" << endl; + return 1; + } + + int subformat = sfinfo.format & SF_FORMAT_SUBMASK; + int readcount = sf_read_float(sndfile, smp->samples, smp->sampleLen); + + // Pad with zeros in case we couldn't read whole file + for(int k = readcount; k <smp->sampleLen; k++) + smp->samples[k] = 0; + + if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) { + double scale ; + int m ; + + sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ; + if (scale < 1e-10) + scale = 1.0 ; + else + scale = 32700.0 / scale ; + cout << "File samples scale = " << scale << endl; + + for (m = 0; m < smp->sampleLen; m++) + smp->samples[m] *= scale; + } + + sf_close(sndfile); + + return 0; +} + + +// Handle Ctrl-C by requesting that the audio rendering stop +void interrupt_handler(int var) +{ + //rt_task_delete ((RT_TASK *) &gTriggerSamplesTask); + gShouldStop = true; +} + +// Print usage information +void usage(const char * processName) +{ + cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f frequency]" << endl; + cerr << " -h: Print this menu\n"; + cerr << " -v: Enable verbose messages\n"; + cerr << " -p period: Set the period (hardware buffer) size in sensor frames\n"; + cerr << " -m: Enable the matrix (ADC and DAC) as well as audio\n"; + cerr << " -f filename: Name of the file to load (default is \"sample.wav\")\n"; +} + +int main(int argc, char *argv[]) +{ + int verbose = 0; // Verbose printing level + int useMatrix = 0; // Whether to use the matrix or just audio + string fileName; // Name of the sample to load + + SampleData sampleData; // User define structure to pass data retrieved from file to render function + sampleData.samples = 0; + sampleData.sampleLen = -1; + + // Parse command-line arguments + while (1) { + int c; + if ((c = getopt(argc, argv, "hp:vms:")) < 0) + break; + switch (c) { + case 'h': + usage(basename(argv[0])); + exit(0); + case 'p': + gPeriodSize = atoi(optarg); + if(gPeriodSize < 1) + gPeriodSize = 1; + break; + case 'v': + verbose = 1; + break; + case 'm': + useMatrix = 1; + break; + case 'f': + fileName = string((char *)optarg); + break; + case '?': + default: + usage(basename(argv[0])); + exit(1); + } + } + + if(fileName.empty()){ + fileName = "filter/longsample.wav"; + } + + // Set verbose logging information (optional by using value > 0; default is 0) + setVerboseLevel(verbose); + + if(verbose) { + cout << "Starting with period size " << gPeriodSize << endl; + if(useMatrix) + cout << "Matrix enabled\n"; + else + cout << "Matrix disabled\n"; + cout << "Loading file " << fileName << endl; + } + + // Load file + if(initFile(fileName, &sampleData) != 0) + { + cout << "Error: unable to load samples " << endl; + return -1; + } + + if(verbose) + cout << "File contains " << sampleData.sampleLen << " samples" << endl; + + // Initialise the PRU audio device + if(initAudio(gPeriodSize, useMatrix, &sampleData) != 0) { + cout << "Error: unable to initialise audio" << endl; + return -1; + } + + // Start the audio device running + if(startAudio()) { + cout << "Error: unable to start real-time audio" << endl; + return -1; + } + + // Set up interrupt handler to catch Control-C + signal(SIGINT, interrupt_handler); + + // Run until told to stop + while(!gShouldStop) { + usleep(100000); + } + + // Stop the audio device + stopAudio(); + + if(verbose) { + cout << "Cleaning up..." << endl; + } + + // Clean up any resources allocated for audio + cleanupAudio(); + + // All done! + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/filter_FIR/render.cpp Thu Nov 06 15:59:16 2014 +0000 @@ -0,0 +1,174 @@ +/* + * render.cpp + * + * Created on: Oct 24, 2014 + * Author: Andrew McPherson and Victor Zappi + */ + + +#include "../../include/render.h" +#include "../../include/RTAudio.h" // to schedule lower prio parallel process +#include <rtdk.h> +#include <cmath> +#include <stdio.h> +#include <NE10.h> // neon library +#include "SampleData.h" +#include "FIRfilter.h" + +SampleData gSampleData; // User defined structure to get complex data from main +int gReadPtr; // Position of last read sample from file +int gNumChannels; + + +// filter vars +ne10_fir_instance_f32_t gFIRfilter; +ne10_float32_t *gFIRfilterIn; +ne10_float32_t *gFIRfilterOut; +ne10_uint32_t blockSize; +ne10_float32_t *gFIRfilterState; + +void initialise_filter(); + + +// Task for handling the update of the frequencies using the matrix +AuxiliaryTask gTriggerSamplesTask; + +bool initialise_trigger(); +void trigger_samples(); + +extern int gPeriodSize; // Period size in sensor frames + + +// initialise_render() is called once before the audio rendering starts. +// Use it to perform any initialisation and allocation which is dependent +// on the period size or sample rate. +// +// userData holds an opaque pointer to a data structure that was passed +// in from the call to initAudio(). +// +// Return true on success; returning false halts the program. + +bool initialise_render(int numChannels, int numMatrixFramesPerPeriod, + int numAudioFramesPerPeriod, float matrixSampleRate, + float audioSampleRate, void *userData) +{ + + // Retrieve a parameter passed in from the initAudio() call + gSampleData = *(SampleData *)userData; + + gReadPtr = -1; + gNumChannels = numChannels; + + initialise_filter(); + + // Initialise auxiliary tasks + if(!initialise_trigger()) + return false; + + return true; +} + +// render() is called regularly at the highest priority by the audio engine. +// Input and output are given from the audio hardware and the other +// ADCs and DACs (if available). If only audio is available, numMatrixFrames +// will be 0. + +void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut, + uint16_t *matrixIn, uint16_t *matrixOut) +{ + for(int n = 0; n < numAudioFrames; n++) { + float in = 0; + + // If triggered... + if(gReadPtr != -1) + in += gSampleData.samples[gReadPtr++]; // ...read each sample... + + if(gReadPtr >= gSampleData.sampleLen) + gReadPtr = -1; + + gFIRfilterIn[n] = in; + } + + ne10_fir_float_neon(&gFIRfilter, gFIRfilterIn, gFIRfilterOut, blockSize); + + for(int n = 0; n < numAudioFrames; n++) { + for(int channel = 0; channel < gNumChannels; channel++) + audioOut[n * gNumChannels + channel] = gFIRfilterOut[n]; // ...and put it in both left and right channel + } + + + // Request that the lower-priority task run at next opportunity + scheduleAuxiliaryTask(gTriggerSamplesTask); +} + +// Initialise NE10 data structures to define FIR filter + +void initialise_filter() +{ + blockSize = 2*gPeriodSize; + gFIRfilterState = (ne10_float32_t *) NE10_MALLOC ((FILTER_TAP_NUM+blockSize-1) * sizeof (ne10_float32_t)); + gFIRfilterIn = (ne10_float32_t *) NE10_MALLOC (blockSize * sizeof (ne10_float32_t)); + gFIRfilterOut = (ne10_float32_t *) NE10_MALLOC (blockSize * sizeof (ne10_float32_t)); + ne10_fir_init_float(&gFIRfilter, FILTER_TAP_NUM, filterTaps, gFIRfilterState, blockSize); +} + + +// Initialise the auxiliary task +// and print info + +bool initialise_trigger() +{ + if((gTriggerSamplesTask = createAuxiliaryTaskLoop(&trigger_samples, 50, "beaglert-trigger-samples")) == 0) + return false; + + rt_printf("Press 'a' to trigger sample, 's' to stop\n"); + rt_printf("Press 'q' to quit\n"); + + return true; +} + +// This is a lower-priority call to periodically read keyboard input +// and trigger samples. By placing it at a lower priority, +// it has minimal effect on the audio performance but it will take longer to +// complete if the system is under heavy audio load. + +void trigger_samples() +{ + // This is not a real-time task! + // Cos getchar is a system call, not handled by Xenomai. + // This task will be automatically down graded. + + char keyStroke = '.'; + + keyStroke = getchar(); + while(getchar()!='\n'); // to read the first stroke + + switch (keyStroke) + { + case 'a': + gReadPtr = 0; + break; + case 's': + gReadPtr = -1; + break; + case 'q': + gShouldStop = true; + break; + default: + break; + } +} + + + +// cleanup_render() is called once at the end, after the audio has stopped. +// Release any resources that were allocated in initialise_render(). + +void cleanup_render() +{ + delete[] gSampleData.samples; + + NE10_FREE(gFIRfilterState); + NE10_FREE(gFIRfilterIn); + NE10_FREE(gFIRfilterOut); +}
--- a/projects/samples/main.cpp Thu Nov 06 14:23:26 2014 +0000 +++ b/projects/samples/main.cpp Thu Nov 06 15:59:16 2014 +0000 @@ -12,13 +12,10 @@ #include <string> #include <sndfile.h> // to load audio files #include "../../include/RTAudio.h" -//#include <native/task.h> // to kill sample trigger task #include "SampleData.h" using namespace std; -//extern AuxiliaryTask gTriggerSamplesTask; - // Load samples from file int initFile(string file, SampleData *smp)//float *& smp) {