To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

The primary repository for this project is hosted at svn://svn.code.sf.net/p/onsetsds/code/ .
This repository is a read-only copy which is updated automatically every hour.

Statistics Download as Zip
| Branch: | Revision:

root / src / onsetsdshelpers.h

History | View | Annotate | Download (7.22 KB)

1
/*
2
        OnsetsDS - real time musical onset detection library.
3
    Copyright (c) 2007 Dan Stowell. All rights reserved.
4

5
    This program is free software; you can redistribute it and/or modify
6
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation; either version 2 of the License, or
8
    (at your option) any later version.
9

10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
14

15
    You should have received a copy of the GNU General Public License
16
    along with this program; if not, write to the Free Software
17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
*/
19

    
20
/** \file */
21

    
22
/**
23
 * \defgroup HelperFuncs        Convenience functions to apply OnsetsDS to a chunk of audio data, or to an audio file.
24
 * 
25
 * These functions are NOT required in order to use the core OnsetsDS functionality, but provide wrappers to
26
 * make it easy to apply OnsetsDS to time-domain data (audio chunks, or audio files) without having to write the
27
 * FFT processing yourself.
28
 */
29
 //@{ 
30
 
31
#ifndef _OnsetsDSHelpers_
32
#define _OnsetsDSHelpers_
33

    
34
#ifdef __cplusplus
35
extern "C" {
36
#endif
37

    
38
#include <fftw3.h>
39
#include "onsetsds.h"
40

    
41
////////////////////////////////////////////////////////////////////////////////
42

    
43
/**
44
When using onsetsds_process_audiofile(), this specifies that your callback function should
45
take an #OnsetsDS and a double as arguments, and return void. The double will be a time
46
offset, from the beginning of the recording, at which the detected onset occurred.
47
*/
48
typedef void (*ODSFileCallback)(OnsetsDS*, double);
49

    
50
/**
51
Holds all the state data required by onsetsds_process_audiodata(), including a pointer to an #OnsetsDS
52
as well as the time-domain/freq-domain buffers.
53
Also remembers the FFT frame size, hop size.
54
*/
55
typedef struct OnsetsDSAudioBuf{
56
        OnsetsDS* ods;
57
        
58
        size_t buflen;
59
        size_t hopsize;
60
        size_t writepos;
61
        float *data;         // size will be buflen
62
        float *window;       // size will be buflen
63
        float *windoweddata; // size will be buflen
64
        float *fftbuf; // size will be buflen
65
        fftwf_plan fftplan;
66
        
67
        // Whole-file-only things (i.e. unused when you're pushing audio blocks yourself):
68
        long sampsElapsed;
69
        double samplerate;
70
        ODSFileCallback filecallback;
71
} OnsetsDSAudioBuf;
72

    
73
/**
74
When using onsetsds_process_audiodata(), this specifies that your callback function should
75
take an #OnsetsDSAudioBuf and a size_t as arguments, and return void. The size_t will be a sample
76
offset at which the detected onset occurred, within the audio frame that was just passed in. (More than 
77
one onset per audio frame is possible, depending on how much data you're passing in at a time.)
78
*/
79
typedef void (*ODSDataCallback)(OnsetsDSAudioBuf*, size_t);
80

    
81
/**
82
This data structure stores statistics derived from using onsetsds_evaluate_audiofile(), describing how well
83
the onset detector matched the "ground truth" annotations.
84
*/
85
typedef struct OnsetsDSEvalData{
86
        long numGT; ///< How many ground truth annotations were provided
87
        long numAnnot; ///< How many onsets it found
88
        long numTP; ///< How many correct detections occurred
89
        long numFP; ///< How many false positives occurred
90
        long numFN; ///< How many false negatives occurred
91
        
92
        float precision; ///< 0 to 1: a measure of resistance against false positives
93
        float recall; ///< 0 to 1: a measure of resistance against false negatives
94
        float f; ///< 0 to 1: the "F-measure", a combination of the precision and recall statistics
95
        
96
        float devimean; ///< Mean of each onset's deviation from the annotated onset, a rough indicator of reacting "too quickly"/"too slowly"
97
        float deviabsmean; ///< Absolute mean of each onset's deviation from the annotated onset, a rough indicator of temporal accuracy
98
        float devisd; ///< Standard deviation re devimean, useful when combining stats
99
        float deviabssd; ///< Standard deviation re deviabsmean, useful when combining stats
100
} OnsetsDSEvalData;
101

    
102
////////////////////////////////////////////////////////////////////////////////
103

    
104
/**
105
Set up the data structures for use by onsetsds_process_audiodata().
106

107
@param odsbuf        Will be set up nicely by this function.
108
@param ods                Must have been initialised properly before calling this function.
109
@param hopsize        Hop size in samples (256 is recommended)
110
*/
111
void onsetsds_init_audiodata(OnsetsDSAudioBuf* odsbuf, OnsetsDS* ods, size_t hopsize);
112
/**
113
Correctly deallocate and destroy the #OnsetsDSAudioBuf. Use this after onsetsds_process_audiofile(), or after you've finished
114
running a series of onsetsds_process_audiodata() calls.
115

116
@param odsbuf        
117
*/
118
void onsetsds_destroy_audiodata(OnsetsDSAudioBuf* odsbuf);
119

    
120
/**
121
Process a new chunk of audio data. 
122

123
@param odsbuf        Must have been initialised properly before calling this function, using onsetsds_init_audiodata()
124
@param data                The *new* chunk of mono, time-domain audio data. Internal buffers will handle frame overlap etc. The size 
125
                        of the input data does *not* need to have a relation to the frame size or hop size.
126
@param datalen        Size of the data buffer.
127
@param callback        Name of your callback function, which will be called whenever an onset is detected. 
128
                        It will be passed the #OnsetsDSAudioBuf object and (more importantly) the sample offset at which the onset was detected 
129
                        (i.e. a value between 0 and datalen).
130
*/
131
void onsetsds_process_audiodata(OnsetsDSAudioBuf* odsbuf, float* data, size_t datalen, 
132
                        ODSDataCallback callback);
133

    
134
////////////////////////////////////////////////////////////////////////////////
135

    
136
/**
137
Process an entire file of audio data. Returns 0 if successful (may fail if it can't find/open the audio file, for example).
138

139
@param odsbuf                Must have been initialised properly before calling this function, using onsetsds_init_audiodata()
140
@param infilename        The file to be loaded.
141
@param callback                Name of your callback function, which will be called whenever an onset is detected. 
142
                        It will be passed the #OnsetsDS object and (more importantly) the time offset in seconds at which the onset was detected 
143
                        (a double-precision-floating-point value between 0 and the duration of the audio file).
144
*/
145
int onsetsds_process_audiofile(OnsetsDSAudioBuf* odsbuf, const char *infilename,
146
                        ODSFileCallback callback);
147

    
148
/**
149
Process an entire file of audio data and compare the onset detector's output against a single "ground truth" annotation of where the 
150
onsets really are. 
151

152
@param odsbuf                Must have been initialised properly before calling this function, using onsetsds_init_audiodata()
153
@param infilename        The file to be loaded.
154
@param gtfilename        The file containing a text list of ground-truth annotations, one number per line, each being an onset's
155
                                                position in seconds from the beginning of the file. The numbers must be in ascending order.
156
                                                This format can be easily exported from programs 
157
                                                such as <a href="http://www.sonicvisualiser.org/">Sonic Visualiser</a>.
158
@param results                Pointer to the #OnsetsDSEvalData where the results should be written.
159

160
*/
161
int onsetsds_evaluate_audiofile(OnsetsDSAudioBuf* odsbuf, const char *infilename, const char *gtfilename, OnsetsDSEvalData* results);
162

    
163
////////////////////////////////////////////////////////////////////////////////
164

    
165
#ifdef __cplusplus
166
}
167
#endif
168

    
169
#endif
170

    
171
//@}