[7a04950] | 1 | /* |
---|
| 2 | Copyright (C) 2004, 2005 Mario Lang <mlang@delysid.org> |
---|
| 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 | |
---|
| 20 | #include "aubio_priv.h" |
---|
[6c7d49b] | 21 | #include "fvec.h" |
---|
[2d8cffa] | 22 | #include "pitch/pitchschmitt.h" |
---|
[7a04950] | 23 | |
---|
| 24 | smpl_t aubio_schmittS16LE (aubio_pitchschmitt_t *p, uint_t nframes, signed short int *indata); |
---|
| 25 | |
---|
| 26 | struct _aubio_pitchschmitt_t { |
---|
| 27 | uint_t blockSize; |
---|
| 28 | uint_t rate; |
---|
| 29 | signed short int *schmittBuffer; |
---|
| 30 | signed short int *schmittPointer; |
---|
[5288874] | 31 | signed short int *buf; |
---|
[7a04950] | 32 | }; |
---|
| 33 | |
---|
[f162cf9] | 34 | aubio_pitchschmitt_t * new_aubio_pitchschmitt (uint_t size) |
---|
[7a04950] | 35 | { |
---|
| 36 | aubio_pitchschmitt_t * p = AUBIO_NEW(aubio_pitchschmitt_t); |
---|
| 37 | p->blockSize = size; |
---|
| 38 | p->schmittBuffer = AUBIO_ARRAY(signed short int,p->blockSize); |
---|
[5288874] | 39 | p->buf = AUBIO_ARRAY(signed short int,p->blockSize); |
---|
[7a04950] | 40 | p->schmittPointer = p->schmittBuffer; |
---|
| 41 | return p; |
---|
| 42 | } |
---|
| 43 | |
---|
[f162cf9] | 44 | void |
---|
| 45 | aubio_pitchschmitt_do (aubio_pitchschmitt_t * p, fvec_t * input, |
---|
| 46 | fvec_t * output) |
---|
[7a04950] | 47 | { |
---|
[f162cf9] | 48 | uint_t i, j; |
---|
| 49 | for (i = 0; i < input->channels; i++) { |
---|
| 50 | for (j = 0; j < input->length; j++) { |
---|
| 51 | p->buf[j] = input->data[i][j] * 32768.; |
---|
| 52 | } |
---|
| 53 | output->data[i][0] = aubio_schmittS16LE (p, input->length, p->buf); |
---|
[7a04950] | 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | smpl_t aubio_schmittS16LE (aubio_pitchschmitt_t *p, uint_t nframes, signed short int *indata) |
---|
| 58 | { |
---|
| 59 | uint_t i, j; |
---|
| 60 | uint_t blockSize = p->blockSize; |
---|
| 61 | signed short int *schmittBuffer = p->schmittBuffer; |
---|
| 62 | signed short int *schmittPointer = p->schmittPointer; |
---|
| 63 | |
---|
[f162cf9] | 64 | smpl_t period = 0., trigfact = 0.6; |
---|
[7a04950] | 65 | |
---|
| 66 | for (i=0; i<nframes; i++) { |
---|
| 67 | *schmittPointer++ = indata[i]; |
---|
[98893c5] | 68 | if (schmittPointer-schmittBuffer >= (sint_t)blockSize) { |
---|
[7a04950] | 69 | sint_t endpoint, startpoint, t1, t2, A1, A2, tc, schmittTriggered; |
---|
| 70 | |
---|
| 71 | schmittPointer = schmittBuffer; |
---|
| 72 | |
---|
| 73 | for (j=0,A1=0,A2=0; j<blockSize; j++) { |
---|
| 74 | if (schmittBuffer[j]>0 && A1<schmittBuffer[j]) A1 = schmittBuffer[j]; |
---|
| 75 | if (schmittBuffer[j]<0 && A2<-schmittBuffer[j]) A2 = -schmittBuffer[j]; |
---|
| 76 | } |
---|
| 77 | t1 = (sint_t)( A1 * trigfact + 0.5); |
---|
| 78 | t2 = - (sint_t)( A2 * trigfact + 0.5); |
---|
| 79 | startpoint=0; |
---|
| 80 | for (j=1; schmittBuffer[j]<=t1 && j<blockSize; j++); |
---|
| 81 | for (; !(schmittBuffer[j] >=t2 && |
---|
| 82 | schmittBuffer[j+1]< t2) && j<blockSize; j++); |
---|
| 83 | startpoint=j; |
---|
| 84 | schmittTriggered=0; |
---|
| 85 | endpoint=startpoint+1; |
---|
| 86 | for(j=startpoint,tc=0; j<blockSize; j++) { |
---|
| 87 | if (!schmittTriggered) { |
---|
| 88 | schmittTriggered = (schmittBuffer[j] >= t1); |
---|
| 89 | } else if (schmittBuffer[j]>=t2 && schmittBuffer[j+1]<t2) { |
---|
| 90 | endpoint=j; |
---|
| 91 | tc++; |
---|
| 92 | schmittTriggered = 0; |
---|
| 93 | } |
---|
| 94 | } |
---|
[f162cf9] | 95 | if ((endpoint > startpoint) && (tc > 0)) { |
---|
| 96 | period = (smpl_t)(endpoint-startpoint)/tc; |
---|
[7a04950] | 97 | } |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | p->schmittBuffer = schmittBuffer; |
---|
| 102 | p->schmittPointer = schmittPointer; |
---|
[f162cf9] | 103 | return period; |
---|
[7a04950] | 104 | } |
---|
| 105 | |
---|
| 106 | void del_aubio_pitchschmitt (aubio_pitchschmitt_t *p) |
---|
| 107 | { |
---|
| 108 | AUBIO_FREE(p->schmittBuffer); |
---|
| 109 | AUBIO_FREE(p); |
---|
| 110 | } |
---|
| 111 | |
---|