[7a04950] | 1 | /* |
---|
[e6a78ea] | 2 | Copyright (C) 2004, 2005 Mario Lang <mlang@delysid.org> |
---|
| 3 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
[7a04950] | 4 | |
---|
[e6a78ea] | 5 | This file is part of aubio. |
---|
[7a04950] | 6 | |
---|
[e6a78ea] | 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. |
---|
[7a04950] | 11 | |
---|
[e6a78ea] | 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/>. |
---|
[7a04950] | 19 | |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include "aubio_priv.h" |
---|
[6c7d49b] | 23 | #include "fvec.h" |
---|
[2d8cffa] | 24 | #include "pitch/pitchschmitt.h" |
---|
[7a04950] | 25 | |
---|
[ad04467] | 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; |
---|
[7a04950] | 36 | }; |
---|
| 37 | |
---|
[ad04467] | 38 | aubio_pitchschmitt_t * |
---|
| 39 | new_aubio_pitchschmitt (uint_t size) |
---|
[7a04950] | 40 | { |
---|
[ad04467] | 41 | aubio_pitchschmitt_t *p = AUBIO_NEW (aubio_pitchschmitt_t); |
---|
[7a04950] | 42 | p->blockSize = size; |
---|
[ad04467] | 43 | p->schmittBuffer = AUBIO_ARRAY (signed short int, p->blockSize); |
---|
| 44 | p->buf = AUBIO_ARRAY (signed short int, p->blockSize); |
---|
[7a04950] | 45 | p->schmittPointer = p->schmittBuffer; |
---|
| 46 | return p; |
---|
| 47 | } |
---|
| 48 | |
---|
[f162cf9] | 49 | void |
---|
| 50 | aubio_pitchschmitt_do (aubio_pitchschmitt_t * p, fvec_t * input, |
---|
| 51 | fvec_t * output) |
---|
[7a04950] | 52 | { |
---|
[f162cf9] | 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); |
---|
[7a04950] | 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
[ad04467] | 62 | smpl_t |
---|
| 63 | aubio_schmittS16LE (aubio_pitchschmitt_t * p, uint_t nframes, |
---|
| 64 | signed short int *indata) |
---|
[7a04950] | 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 | |
---|
[f162cf9] | 71 | smpl_t period = 0., trigfact = 0.6; |
---|
[7a04950] | 72 | |
---|
[ad04467] | 73 | for (i = 0; i < nframes; i++) { |
---|
[7a04950] | 74 | *schmittPointer++ = indata[i]; |
---|
[ad04467] | 75 | if (schmittPointer - schmittBuffer >= (sint_t) blockSize) { |
---|
[7a04950] | 76 | sint_t endpoint, startpoint, t1, t2, A1, A2, tc, schmittTriggered; |
---|
| 77 | |
---|
| 78 | schmittPointer = schmittBuffer; |
---|
| 79 | |
---|
[ad04467] | 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]; |
---|
[7a04950] | 85 | } |
---|
[ad04467] | 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 | } |
---|
[7a04950] | 103 | } |
---|
[f162cf9] | 104 | if ((endpoint > startpoint) && (tc > 0)) { |
---|
[ad04467] | 105 | period = (smpl_t) (endpoint - startpoint) / tc; |
---|
[7a04950] | 106 | } |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
[ad04467] | 110 | p->schmittBuffer = schmittBuffer; |
---|
[7a04950] | 111 | p->schmittPointer = schmittPointer; |
---|
[f162cf9] | 112 | return period; |
---|
[7a04950] | 113 | } |
---|
| 114 | |
---|
[ad04467] | 115 | void |
---|
| 116 | del_aubio_pitchschmitt (aubio_pitchschmitt_t * p) |
---|
[7a04950] | 117 | { |
---|
[ad04467] | 118 | AUBIO_FREE (p->schmittBuffer); |
---|
| 119 | AUBIO_FREE (p); |
---|
[7a04950] | 120 | } |
---|