Merge branch 'develop' into notes
[aubio.git] / src / notes / notes.c
1 /*
2   Copyright (C) 2014 Paul Brossier <piem@aubio.org>
3
4   This file is part of aubio.
5
6   aubio is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   aubio is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "aubio_priv.h"
22 #include "fvec.h"
23 #include "pitch/pitch.h"
24 #include "onset/onset.h"
25 #include "notes/notes.h"
26
27 struct _aubio_notes_t {
28
29   uint_t onset_buf_size;
30   uint_t pitch_buf_size;
31   uint_t hop_size;
32
33   uint_t samplerate;
34
35   uint_t median;
36   fvec_t *note_buffer;
37   fvec_t *note_buffer2;
38
39   aubio_pitch_t *pitch;
40   aubio_onset_t *onset;
41   fvec_t *onset_output;
42   fvec_t *pitch_output;
43
44   smpl_t curnote;
45   smpl_t newnote;
46 };
47
48 aubio_notes_t * new_aubio_notes (char_t * notes_method,
49     uint_t buf_size, uint_t hop_size, uint_t samplerate) {
50   aubio_notes_t *o = AUBIO_NEW(aubio_notes_t);
51
52   o->onset_buf_size = buf_size;
53   o->pitch_buf_size = buf_size * 4;
54   o->hop_size = hop_size;
55
56   o->samplerate = samplerate;
57
58   o->median = 9;
59
60   if (strcmp(notes_method, "default") != 0) {
61     AUBIO_ERR("unknown notes detection method %s, using default.\n",
62        notes_method);
63     goto fail;
64   }
65   o->note_buffer = new_fvec(o->median);
66   o->note_buffer2 = new_fvec(o->median);
67
68   o->curnote = -1.;
69   o->newnote = -1.;
70
71   return o;
72
73 fail:
74   del_aubio_notes(o);
75   return NULL;
76 }
77
78 void del_aubio_notes (aubio_notes_t *o) {
79   if (o->note_buffer) del_fvec(o->note_buffer);
80   if (o->note_buffer2) del_fvec(o->note_buffer2);
81   AUBIO_FREE(o);
82 }