[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 | |
---|
| 34 | aubio_pitchschmitt_t * new_aubio_pitchschmitt (uint_t size, uint_t samplerate) |
---|
| 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 | p->rate = samplerate; |
---|
| 42 | return p; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | smpl_t aubio_pitchschmitt_detect (aubio_pitchschmitt_t *p, fvec_t * input) |
---|
| 46 | { |
---|
| 47 | uint_t i; |
---|
| 48 | for (i=0; i<input->length; i++) { |
---|
[5288874] | 49 | p->buf[i] = input->data[0][i]*32768.; |
---|
[7a04950] | 50 | } |
---|
[5288874] | 51 | return aubio_schmittS16LE(p, input->length, p->buf); |
---|
[7a04950] | 52 | } |
---|
| 53 | |
---|
| 54 | smpl_t aubio_schmittS16LE (aubio_pitchschmitt_t *p, uint_t nframes, signed short int *indata) |
---|
| 55 | { |
---|
| 56 | uint_t i, j; |
---|
| 57 | uint_t blockSize = p->blockSize; |
---|
| 58 | signed short int *schmittBuffer = p->schmittBuffer; |
---|
| 59 | signed short int *schmittPointer = p->schmittPointer; |
---|
| 60 | |
---|
| 61 | smpl_t freq = 0., trigfact = 0.6; |
---|
| 62 | |
---|
| 63 | for (i=0; i<nframes; i++) { |
---|
| 64 | *schmittPointer++ = indata[i]; |
---|
[98893c5] | 65 | if (schmittPointer-schmittBuffer >= (sint_t)blockSize) { |
---|
[7a04950] | 66 | sint_t endpoint, startpoint, t1, t2, A1, A2, tc, schmittTriggered; |
---|
| 67 | |
---|
| 68 | schmittPointer = schmittBuffer; |
---|
| 69 | |
---|
| 70 | for (j=0,A1=0,A2=0; j<blockSize; j++) { |
---|
| 71 | if (schmittBuffer[j]>0 && A1<schmittBuffer[j]) A1 = schmittBuffer[j]; |
---|
| 72 | if (schmittBuffer[j]<0 && A2<-schmittBuffer[j]) A2 = -schmittBuffer[j]; |
---|
| 73 | } |
---|
| 74 | t1 = (sint_t)( A1 * trigfact + 0.5); |
---|
| 75 | t2 = - (sint_t)( A2 * trigfact + 0.5); |
---|
| 76 | startpoint=0; |
---|
| 77 | for (j=1; schmittBuffer[j]<=t1 && j<blockSize; j++); |
---|
| 78 | for (; !(schmittBuffer[j] >=t2 && |
---|
| 79 | schmittBuffer[j+1]< t2) && j<blockSize; j++); |
---|
| 80 | startpoint=j; |
---|
| 81 | schmittTriggered=0; |
---|
| 82 | endpoint=startpoint+1; |
---|
| 83 | for(j=startpoint,tc=0; j<blockSize; j++) { |
---|
| 84 | if (!schmittTriggered) { |
---|
| 85 | schmittTriggered = (schmittBuffer[j] >= t1); |
---|
| 86 | } else if (schmittBuffer[j]>=t2 && schmittBuffer[j+1]<t2) { |
---|
| 87 | endpoint=j; |
---|
| 88 | tc++; |
---|
| 89 | schmittTriggered = 0; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | if (endpoint > startpoint) { |
---|
| 93 | freq = ((smpl_t)p->rate*(tc/(smpl_t)(endpoint-startpoint))); |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | p->schmittBuffer = schmittBuffer; |
---|
| 99 | p->schmittPointer = schmittPointer; |
---|
| 100 | return freq; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | void del_aubio_pitchschmitt (aubio_pitchschmitt_t *p) |
---|
| 104 | { |
---|
| 105 | AUBIO_FREE(p->schmittBuffer); |
---|
| 106 | AUBIO_FREE(p); |
---|
| 107 | } |
---|
| 108 | |
---|