Logging Sensor Data
This sketch demonstrates how to log sensor data for later processing or analysis. The file can be written as either a binary file (using 32-bit floats) or as a formatted text file (again, only using floats). An optional header and footer can be added to text files, to make your file immediately ready to be opened in your favourite data analysis program.
Make sure you do not fill up your disk while logging. Current disk usage can be obtained by typing at the console: 
$ df -h .
While the space occupied by a given file can be obtained with 
$ ls -lh path/to/filename
To learn how to obtain more space on your filesystem, check out the wiki.
If you write text files, it is a good idea keep your lines short, as this usually makes it faster for other programs to process the file. The text file in this example breaks a line after each sample is logged (note the \n at the end of the format string).
If you plan to write large amount of data, binary files are preferrable as they take less space on disk and are therefore less resource-consuming to write. They are also generally faster to read and parse than text files.
It is safe to call WriteFile::log() from the audio thread, as the data is added to a buffer which is then processed in the background and written to disk from a lower-priority non-realtime task.
The files generated by this program can be opened, e.g.: in GNU Octave or Matlab, with
filename = 'out.bin';
numChannels = 4; % num of channels in the binary file
fid=fopen([filename],'r');
A = fread(fid, 'float'); % load in A the values logged to the binary file
fclose(fid);
A = vec2mat(A, numChannels); % transform the array A into a matrix with numChannels columns
 
run('out.m'); % this will create a variable myar with the data logged to the text file
T =  myvar(:,1); % the first column of the text file is timestamp
plot(1:size(A, 1), A, T, myvar(:,2));
legend('analog0','analog1', 'analog2', 'analog3', 'analog4')
 
#include <libraries/WriteFile/WriteFile.h>
 
 
{
        {
                fprintf(stderr, "Error: this example requires that we use interleaved buffers\n");
                return false;
        }
 
        {
                fprintf(stderr, "Error: this example requires at least 5 analog inputs to be enabled\n");
                return false;
        }
 
        file1.setEchoInterval(10000); 
        file1.setFileType(kBinary);
        
        
        
        file1.setFormat("binary: %.4f %.4f %.4f %.4f\n");
 
        file2.setup("out.m"); 
        file2.setHeader("myvar=[\n"); 
        file2.setFooter("];\n"); 
        file2.setFormat("%.4f %.4f\n"); 
        file2.setFileType(kText);
        file2.setEchoInterval(10000); 
 
        return true;
}
 
{
        static int count = 0;
                
                
        }
        file2.log(count);
}
 
{
}
#define BELA_FLAG_INTERLEAVED
Definition Bela.h:207
Definition WriteFile.h:22
void setup(const char *filename, bool overwrite=false, bool append=false)
Definition WriteFile.cpp:111
static float analogRead(BelaContext *context, int frame, int channel)
Read an analog input, specifying the frame number (when to read) and the channel.
Definition Bela.h:1480
void render(BelaContext *context, void *userData)
User-defined callback function to process audio and sensor data.
Definition render.cpp:68
bool setup(BelaContext *context, void *userData)
User-defined initialisation function which runs before audio rendering begins.
Definition render.cpp:51
void cleanup(BelaContext *context, void *userData)
User-defined cleanup function which runs when the program finishes.
Definition render.cpp:96
Structure holding audio and sensor settings and pointers to I/O data buffers.
Definition Bela.h:231
const float *const analogIn
Buffer holding analog input samples.
Definition Bela.h:283
const uint32_t analogFrames
The number of analog frames per block.
Definition Bela.h:341
const uint32_t flags
Other audio/sensor settings.
Definition Bela.h:414
const uint32_t analogInChannels
The number of analog input channels.
Definition Bela.h:346