1 | /* |
---|
2 | Copyright (C) 2004, 2005 Mario Lang <mlang@delysid.org> |
---|
3 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
4 | |
---|
5 | This file is part of aubio. |
---|
6 | |
---|
7 | aubio is free software: you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation, either version 3 of the License, or |
---|
10 | (at your option) any later version. |
---|
11 | |
---|
12 | aubio is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | GNU General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU General Public License |
---|
18 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | |
---|
20 | */ |
---|
21 | |
---|
22 | #include "aubio_priv.h" |
---|
23 | #include "fvec.h" |
---|
24 | #include "pitch/pitchschmitt.h" |
---|
25 | |
---|
26 | smpl_t aubio_schmittS16LE (aubio_pitchschmitt_t * p, uint_t nframes, |
---|
27 | signed short int *indata); |
---|
28 | |
---|
29 | struct _aubio_pitchschmitt_t |
---|
30 | { |
---|
31 | uint_t blockSize; |
---|
32 | uint_t rate; |
---|
33 | signed short int *schmittBuffer; |
---|
34 | signed short int *schmittPointer; |
---|
35 | signed short int *buf; |
---|
36 | }; |
---|
37 | |
---|
38 | aubio_pitchschmitt_t * |
---|
39 | new_aubio_pitchschmitt (uint_t size) |
---|
40 | { |
---|
41 | aubio_pitchschmitt_t *p = AUBIO_NEW (aubio_pitchschmitt_t); |
---|
42 | p->blockSize = size; |
---|
43 | p->schmittBuffer = AUBIO_ARRAY (signed short int, p->blockSize); |
---|
44 | p->buf = AUBIO_ARRAY (signed short int, p->blockSize); |
---|
45 | p->schmittPointer = p->schmittBuffer; |
---|
46 | return p; |
---|
47 | } |
---|
48 | |
---|
49 | void |
---|
50 | aubio_pitchschmitt_do (aubio_pitchschmitt_t * p, fvec_t * input, |
---|
51 | fvec_t * output) |
---|
52 | { |
---|
53 | uint_t i, j; |
---|
54 | for (i = 0; i < input->channels; i++) { |
---|
55 | for (j = 0; j < input->length; j++) { |
---|
56 | p->buf[j] = input->data[i][j] * 32768.; |
---|
57 | } |
---|
58 | output->data[i][0] = aubio_schmittS16LE (p, input->length, p->buf); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | smpl_t |
---|
63 | aubio_schmittS16LE (aubio_pitchschmitt_t * p, uint_t nframes, |
---|
64 | signed short int *indata) |
---|
65 | { |
---|
66 | uint_t i, j; |
---|
67 | uint_t blockSize = p->blockSize; |
---|
68 | signed short int *schmittBuffer = p->schmittBuffer; |
---|
69 | signed short int *schmittPointer = p->schmittPointer; |
---|
70 | |
---|
71 | smpl_t period = 0., trigfact = 0.6; |
---|
72 | |
---|
73 | for (i = 0; i < nframes; i++) { |
---|
74 | *schmittPointer++ = indata[i]; |
---|
75 | if (schmittPointer - schmittBuffer >= (sint_t) blockSize) { |
---|
76 | sint_t endpoint, startpoint, t1, t2, A1, A2, tc, schmittTriggered; |
---|
77 | |
---|
78 | schmittPointer = schmittBuffer; |
---|
79 | |
---|
80 | for (j = 0, A1 = 0, A2 = 0; j < blockSize; j++) { |
---|
81 | if (schmittBuffer[j] > 0 && A1 < schmittBuffer[j]) |
---|
82 | A1 = schmittBuffer[j]; |
---|
83 | if (schmittBuffer[j] < 0 && A2 < -schmittBuffer[j]) |
---|
84 | A2 = -schmittBuffer[j]; |
---|
85 | } |
---|
86 | t1 = (sint_t) (A1 * trigfact + 0.5); |
---|
87 | t2 = -(sint_t) (A2 * trigfact + 0.5); |
---|
88 | startpoint = 0; |
---|
89 | for (j = 1; schmittBuffer[j] <= t1 && j < blockSize; j++); |
---|
90 | for (; !(schmittBuffer[j] >= t2 && |
---|
91 | schmittBuffer[j + 1] < t2) && j < blockSize; j++); |
---|
92 | startpoint = j; |
---|
93 | schmittTriggered = 0; |
---|
94 | endpoint = startpoint + 1; |
---|
95 | for (j = startpoint, tc = 0; j < blockSize; j++) { |
---|
96 | if (!schmittTriggered) { |
---|
97 | schmittTriggered = (schmittBuffer[j] >= t1); |
---|
98 | } else if (schmittBuffer[j] >= t2 && schmittBuffer[j + 1] < t2) { |
---|
99 | endpoint = j; |
---|
100 | tc++; |
---|
101 | schmittTriggered = 0; |
---|
102 | } |
---|
103 | } |
---|
104 | if ((endpoint > startpoint) && (tc > 0)) { |
---|
105 | period = (smpl_t) (endpoint - startpoint) / tc; |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | p->schmittBuffer = schmittBuffer; |
---|
111 | p->schmittPointer = schmittPointer; |
---|
112 | return period; |
---|
113 | } |
---|
114 | |
---|
115 | void |
---|
116 | del_aubio_pitchschmitt (aubio_pitchschmitt_t * p) |
---|
117 | { |
---|
118 | AUBIO_FREE (p->schmittBuffer); |
---|
119 | AUBIO_FREE (p); |
---|
120 | } |
---|