1 | /* |
---|
2 | Copyright (C) 2003 Paul Brossier |
---|
3 | |
---|
4 | This program is free software; you can redistribute it and/or modify |
---|
5 | it under the terms of the GNU General Public License as published by |
---|
6 | the Free Software Foundation; either version 2 of the License, or |
---|
7 | (at your option) any later version. |
---|
8 | |
---|
9 | This program is distributed in the hope that it will be useful, |
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | GNU General Public License for more details. |
---|
13 | |
---|
14 | You should have received a copy of the GNU General Public License |
---|
15 | along with this program; if not, write to the Free Software |
---|
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
17 | */ |
---|
18 | |
---|
19 | #include "aubio_priv.h" |
---|
20 | #include "sample.h" |
---|
21 | #include "phasevoc.h" |
---|
22 | #include "mathutils.h" |
---|
23 | //#include "filter.h" |
---|
24 | #include "pitchmcomb.h" |
---|
25 | #include "pitchyin.h" |
---|
26 | #include "pitchfcomb.h" |
---|
27 | #include "pitchschmitt.h" |
---|
28 | #include "pitchdetection.h" |
---|
29 | |
---|
30 | typedef smpl_t (*aubio_pitchdetection_func_t)(aubio_pitchdetection_t *p, |
---|
31 | fvec_t * ibuf); |
---|
32 | void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf); |
---|
33 | |
---|
34 | struct _aubio_pitchdetection_t { |
---|
35 | aubio_pitchdetection_type type; |
---|
36 | aubio_pitchdetection_mode mode; |
---|
37 | uint_t srate; |
---|
38 | uint_t bufsize; |
---|
39 | /* for mcomb */ |
---|
40 | aubio_pvoc_t * pv; |
---|
41 | cvec_t * fftgrain; |
---|
42 | aubio_pitchmcomb_t * mcomb; |
---|
43 | aubio_pitchfcomb_t * fcomb; |
---|
44 | aubio_pitchschmitt_t * schmitt; |
---|
45 | //aubio_filter_t * filter; |
---|
46 | /* for yin */ |
---|
47 | fvec_t * buf; |
---|
48 | fvec_t * yin; |
---|
49 | aubio_pitchdetection_func_t callback; |
---|
50 | }; |
---|
51 | |
---|
52 | aubio_pitchdetection_t * new_aubio_pitchdetection(uint_t bufsize, |
---|
53 | uint_t hopsize, |
---|
54 | uint_t channels, |
---|
55 | uint_t samplerate, |
---|
56 | aubio_pitchdetection_type type, |
---|
57 | aubio_pitchdetection_mode mode) |
---|
58 | { |
---|
59 | aubio_pitchdetection_t *p = AUBIO_NEW(aubio_pitchdetection_t); |
---|
60 | p->srate = samplerate; |
---|
61 | p->type = type; |
---|
62 | p->bufsize = bufsize; |
---|
63 | switch(p->type) { |
---|
64 | case aubio_yin: |
---|
65 | p->buf = new_fvec(bufsize,channels); |
---|
66 | p->yin = new_fvec(bufsize/2,channels); |
---|
67 | p->callback = aubio_pitchdetection_yin; |
---|
68 | break; |
---|
69 | case aubio_mcomb: |
---|
70 | p->pv = new_aubio_pvoc(bufsize, hopsize, channels); |
---|
71 | p->fftgrain = new_cvec(bufsize, channels); |
---|
72 | p->mcomb = new_aubio_pitchmcomb(bufsize,channels); |
---|
73 | p->callback = aubio_pitchdetection_mcomb; |
---|
74 | break; |
---|
75 | case aubio_fcomb: |
---|
76 | p->buf = new_fvec(bufsize,channels); |
---|
77 | p->fcomb = new_aubio_pitchfcomb(bufsize,samplerate); |
---|
78 | p->callback = aubio_pitchdetection_fcomb; |
---|
79 | break; |
---|
80 | case aubio_schmitt: |
---|
81 | p->buf = new_fvec(bufsize,channels); |
---|
82 | p->schmitt = new_aubio_pitchschmitt(bufsize,samplerate); |
---|
83 | p->callback = aubio_pitchdetection_schmitt; |
---|
84 | break; |
---|
85 | default: |
---|
86 | break; |
---|
87 | } |
---|
88 | return p; |
---|
89 | } |
---|
90 | |
---|
91 | void del_aubio_pitchdetection(aubio_pitchdetection_t * p) { |
---|
92 | switch(p->type) { |
---|
93 | case aubio_yin: |
---|
94 | del_fvec(p->yin); |
---|
95 | del_fvec(p->buf); |
---|
96 | break; |
---|
97 | case aubio_mcomb: |
---|
98 | del_aubio_pvoc(p->pv); |
---|
99 | del_cvec(p->fftgrain); |
---|
100 | del_aubio_pitchmcomb(p->mcomb); |
---|
101 | break; |
---|
102 | case aubio_schmitt: |
---|
103 | del_fvec(p->buf); |
---|
104 | del_aubio_pitchschmitt(p->schmitt); |
---|
105 | break; |
---|
106 | case aubio_fcomb: |
---|
107 | del_fvec(p->buf); |
---|
108 | del_aubio_pitchfcomb(p->fcomb); |
---|
109 | break; |
---|
110 | default: |
---|
111 | break; |
---|
112 | } |
---|
113 | AUBIO_FREE(p); |
---|
114 | } |
---|
115 | |
---|
116 | void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf){ |
---|
117 | uint_t i,j = 0, overlap_size = 0; |
---|
118 | overlap_size = p->buf->length-ibuf->length; |
---|
119 | for (i=0;i<p->buf->channels;i++){ |
---|
120 | for (j=0;j<overlap_size;j++){ |
---|
121 | p->buf->data[i][j] = |
---|
122 | p->buf->data[i][j+ibuf->length]; |
---|
123 | } |
---|
124 | } |
---|
125 | for (i=0;i<ibuf->channels;i++){ |
---|
126 | for (j=0;j<ibuf->length;j++){ |
---|
127 | p->buf->data[i][j+overlap_size] = |
---|
128 | ibuf->data[i][j]; |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | smpl_t aubio_pitchdetection(aubio_pitchdetection_t *p, fvec_t * ibuf) { |
---|
134 | return p->callback(p,ibuf); |
---|
135 | } |
---|
136 | |
---|
137 | smpl_t aubio_pitchdetection_mcomb(aubio_pitchdetection_t *p, fvec_t *ibuf) { |
---|
138 | smpl_t pitch = 0.; |
---|
139 | aubio_pvoc_do(p->pv,ibuf,p->fftgrain); |
---|
140 | pitch = aubio_pitchmcomb_detect(p->mcomb,p->fftgrain); |
---|
141 | /** \bug should move the >0 check within bintofreq */ |
---|
142 | if (pitch>0.) { |
---|
143 | pitch = bintofreq(pitch,p->srate,p->bufsize); |
---|
144 | } else { |
---|
145 | pitch = 0.; |
---|
146 | } |
---|
147 | return pitch; |
---|
148 | } |
---|
149 | |
---|
150 | smpl_t aubio_pitchdetection_yin(aubio_pitchdetection_t *p, fvec_t *ibuf) { |
---|
151 | smpl_t pitch = 0.; |
---|
152 | aubio_pitchdetection_slideblock(p,ibuf); |
---|
153 | pitch = aubio_pitchyin_getpitchfast(p->buf,p->yin, 0.5); |
---|
154 | if (pitch>0) { |
---|
155 | pitch = p->srate/(pitch+0.); |
---|
156 | } else { |
---|
157 | pitch = 0.; |
---|
158 | } |
---|
159 | return pitch; |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | smpl_t aubio_pitchdetection_fcomb(aubio_pitchdetection_t *p, fvec_t *ibuf){ |
---|
164 | aubio_pitchdetection_slideblock(p,ibuf); |
---|
165 | return aubio_pitchfcomb_detect(p->fcomb,p->buf); |
---|
166 | } |
---|
167 | |
---|
168 | smpl_t aubio_pitchdetection_schmitt(aubio_pitchdetection_t *p, fvec_t *ibuf){ |
---|
169 | aubio_pitchdetection_slideblock(p,ibuf); |
---|
170 | return aubio_pitchschmitt_detect(p->schmitt,p->buf); |
---|
171 | } |
---|