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 | #define AUBIO_UNSTABLE 1 // for fvec_median_channel |
---|
20 | |
---|
21 | #include "utils.h" |
---|
22 | |
---|
23 | /* pitch objects */ |
---|
24 | smpl_t pitch = 0.; |
---|
25 | |
---|
26 | uint_t median = 6; |
---|
27 | smpl_t curlevel = 0.; |
---|
28 | |
---|
29 | aubio_pitch_t *pitchdet; |
---|
30 | |
---|
31 | fvec_t *note_buffer = NULL; |
---|
32 | fvec_t *note_buffer2 = NULL; |
---|
33 | |
---|
34 | smpl_t curnote = 0.; |
---|
35 | smpl_t newnote = 0.; |
---|
36 | uint_t isready = 0; |
---|
37 | unsigned int pos = 0; /*frames%dspblocksize*/ |
---|
38 | |
---|
39 | aubio_pitch_t *pitchdet; |
---|
40 | aubio_onset_t *o; |
---|
41 | fvec_t *onset; |
---|
42 | fvec_t *pitch_obuf; |
---|
43 | |
---|
44 | /** append new note candidate to the note_buffer and return filtered value. we |
---|
45 | * need to copy the input array as fvec_median destroy its input data.*/ |
---|
46 | void note_append (fvec_t * note_buffer, smpl_t curnote); |
---|
47 | uint_t get_note (fvec_t * note_buffer, fvec_t * note_buffer2); |
---|
48 | |
---|
49 | static int aubio_process(smpl_t **input, smpl_t **output, int nframes) { |
---|
50 | unsigned int i; /*channels*/ |
---|
51 | unsigned int j; /*frames*/ |
---|
52 | for (j=0;j<(unsigned)nframes;j++) { |
---|
53 | if(usejack) { |
---|
54 | for (i=0;i<channels;i++) { |
---|
55 | /* write input to datanew */ |
---|
56 | fvec_write_sample(ibuf, input[i][j], i, pos); |
---|
57 | /* put synthnew in output */ |
---|
58 | output[i][j] = fvec_read_sample(obuf, i, pos); |
---|
59 | } |
---|
60 | } |
---|
61 | /*time for fft*/ |
---|
62 | if (pos == overlap_size-1) { |
---|
63 | /* block loop */ |
---|
64 | aubio_onset_do(o, ibuf, onset); |
---|
65 | |
---|
66 | aubio_pitch_do (pitchdet, ibuf, pitch_obuf); |
---|
67 | pitch = fvec_read_sample(pitch_obuf, 0, 0); |
---|
68 | if(median){ |
---|
69 | note_append(note_buffer, pitch); |
---|
70 | } |
---|
71 | |
---|
72 | /* curlevel is negatif or 1 if silence */ |
---|
73 | curlevel = aubio_level_detection(ibuf, silence); |
---|
74 | if (fvec_read_sample(onset, 0, 0)) { |
---|
75 | /* test for silence */ |
---|
76 | if (curlevel == 1.) { |
---|
77 | if (median) isready = 0; |
---|
78 | /* send note off */ |
---|
79 | send_noteon(curnote,0); |
---|
80 | } else { |
---|
81 | if (median) { |
---|
82 | isready = 1; |
---|
83 | } else { |
---|
84 | /* kill old note */ |
---|
85 | send_noteon(curnote,0); |
---|
86 | /* get and send new one */ |
---|
87 | send_noteon(pitch,127+(int)floor(curlevel)); |
---|
88 | curnote = pitch; |
---|
89 | } |
---|
90 | |
---|
91 | for (pos = 0; pos < overlap_size; pos++){ |
---|
92 | obuf->data[0][pos] = woodblock->data[0][pos]; |
---|
93 | } |
---|
94 | } |
---|
95 | } else { |
---|
96 | if (median) { |
---|
97 | if (isready > 0) |
---|
98 | isready++; |
---|
99 | if (isready == median) |
---|
100 | { |
---|
101 | /* kill old note */ |
---|
102 | send_noteon(curnote,0); |
---|
103 | newnote = get_note(note_buffer, note_buffer2); |
---|
104 | curnote = newnote; |
---|
105 | /* get and send new one */ |
---|
106 | if (curnote>45){ |
---|
107 | send_noteon(curnote,127+(int)floor(curlevel)); |
---|
108 | } |
---|
109 | } |
---|
110 | } // if median |
---|
111 | for (pos = 0; pos < overlap_size; pos++) |
---|
112 | obuf->data[0][pos] = 0.; |
---|
113 | } |
---|
114 | /* end of block loop */ |
---|
115 | pos = -1; /* so it will be zero next j loop */ |
---|
116 | } |
---|
117 | pos++; |
---|
118 | } |
---|
119 | return 1; |
---|
120 | } |
---|
121 | |
---|
122 | static void process_print (void) { |
---|
123 | if (verbose) outmsg("%f\n",pitch); |
---|
124 | } |
---|
125 | |
---|
126 | void |
---|
127 | note_append (fvec_t * note_buffer, smpl_t curnote) |
---|
128 | { |
---|
129 | uint_t i = 0; |
---|
130 | for (i = 0; i < note_buffer->length - 1; i++) { |
---|
131 | note_buffer->data[0][i] = note_buffer->data[0][i + 1]; |
---|
132 | } |
---|
133 | note_buffer->data[0][note_buffer->length - 1] = curnote; |
---|
134 | return; |
---|
135 | } |
---|
136 | |
---|
137 | uint_t |
---|
138 | get_note (fvec_t * note_buffer, fvec_t * note_buffer2) |
---|
139 | { |
---|
140 | uint_t i; |
---|
141 | for (i = 0; i < note_buffer->length; i++) { |
---|
142 | note_buffer2->data[0][i] = note_buffer->data[0][i]; |
---|
143 | } |
---|
144 | return fvec_median_channel (note_buffer2, 0); |
---|
145 | } |
---|
146 | |
---|
147 | int main(int argc, char **argv) { |
---|
148 | examples_common_init(argc,argv); |
---|
149 | |
---|
150 | o = new_aubio_onset (onset_mode, buffer_size, overlap_size, channels, |
---|
151 | samplerate); |
---|
152 | if (threshold != 0.) aubio_onset_set_threshold (o, threshold); |
---|
153 | onset = new_fvec (1, channels); |
---|
154 | |
---|
155 | pitchdet = new_aubio_pitch (pitch_mode, buffer_size * 4, |
---|
156 | overlap_size, channels, samplerate); |
---|
157 | aubio_pitch_set_tolerance (pitchdet, 0.7); |
---|
158 | pitch_obuf = new_fvec (1, channels); |
---|
159 | if (median) { |
---|
160 | note_buffer = new_fvec (median, 1); |
---|
161 | note_buffer2 = new_fvec (median, 1); |
---|
162 | } |
---|
163 | |
---|
164 | examples_common_process(aubio_process, process_print); |
---|
165 | |
---|
166 | send_noteon (curnote, 0); |
---|
167 | del_aubio_pitch (pitchdet); |
---|
168 | if (median) { |
---|
169 | del_fvec (note_buffer); |
---|
170 | del_fvec (note_buffer2); |
---|
171 | } |
---|
172 | del_fvec (pitch_obuf); |
---|
173 | |
---|
174 | examples_common_del(); |
---|
175 | debug("End of program.\n"); |
---|
176 | fflush(stderr); |
---|
177 | return 0; |
---|
178 | } |
---|
179 | |
---|