[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 | /* |
---|
| 21 | |
---|
| 22 | This file was taken from the tuneit project, in the file |
---|
| 23 | tuneit.c -- Detect fundamental frequency of a sound |
---|
| 24 | see http://delysid.org/tuneit.html |
---|
| 25 | |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | #include "aubio_priv.h" |
---|
| 29 | #include "sample.h" |
---|
| 30 | #include "pitchschmitt.h" |
---|
| 31 | |
---|
| 32 | smpl_t aubio_schmittS16LE (aubio_pitchschmitt_t *p, uint_t nframes, signed short int *indata); |
---|
| 33 | |
---|
| 34 | struct _aubio_pitchschmitt_t { |
---|
| 35 | uint_t blockSize; |
---|
| 36 | uint_t rate; |
---|
| 37 | signed short int *schmittBuffer; |
---|
| 38 | signed short int *schmittPointer; |
---|
| 39 | }; |
---|
| 40 | |
---|
| 41 | aubio_pitchschmitt_t * new_aubio_pitchschmitt (uint_t size, uint_t samplerate) |
---|
| 42 | { |
---|
| 43 | aubio_pitchschmitt_t * p = AUBIO_NEW(aubio_pitchschmitt_t); |
---|
| 44 | p->blockSize = size; |
---|
| 45 | p->schmittBuffer = AUBIO_ARRAY(signed short int,p->blockSize); |
---|
| 46 | p->schmittPointer = p->schmittBuffer; |
---|
| 47 | p->rate = samplerate; |
---|
| 48 | return p; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | smpl_t aubio_pitchschmitt_detect (aubio_pitchschmitt_t *p, fvec_t * input) |
---|
| 52 | { |
---|
| 53 | signed short int buf[input->length]; |
---|
| 54 | uint_t i; |
---|
| 55 | for (i=0; i<input->length; i++) { |
---|
| 56 | buf[i] = input->data[0][i]*32768.; |
---|
| 57 | } |
---|
| 58 | return aubio_schmittS16LE(p, input->length, buf); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | smpl_t aubio_schmittS16LE (aubio_pitchschmitt_t *p, uint_t nframes, signed short int *indata) |
---|
| 62 | { |
---|
| 63 | uint_t i, j; |
---|
| 64 | uint_t blockSize = p->blockSize; |
---|
| 65 | signed short int *schmittBuffer = p->schmittBuffer; |
---|
| 66 | signed short int *schmittPointer = p->schmittPointer; |
---|
| 67 | |
---|
| 68 | smpl_t freq = 0., trigfact = 0.6; |
---|
| 69 | |
---|
| 70 | for (i=0; i<nframes; i++) { |
---|
| 71 | *schmittPointer++ = indata[i]; |
---|
| 72 | if (schmittPointer-schmittBuffer >= blockSize) { |
---|
| 73 | sint_t endpoint, startpoint, t1, t2, A1, A2, tc, schmittTriggered; |
---|
| 74 | |
---|
| 75 | schmittPointer = schmittBuffer; |
---|
| 76 | |
---|
| 77 | for (j=0,A1=0,A2=0; j<blockSize; j++) { |
---|
| 78 | if (schmittBuffer[j]>0 && A1<schmittBuffer[j]) A1 = schmittBuffer[j]; |
---|
| 79 | if (schmittBuffer[j]<0 && A2<-schmittBuffer[j]) A2 = -schmittBuffer[j]; |
---|
| 80 | } |
---|
| 81 | t1 = (sint_t)( A1 * trigfact + 0.5); |
---|
| 82 | t2 = - (sint_t)( A2 * trigfact + 0.5); |
---|
| 83 | startpoint=0; |
---|
| 84 | for (j=1; schmittBuffer[j]<=t1 && j<blockSize; j++); |
---|
| 85 | for (; !(schmittBuffer[j] >=t2 && |
---|
| 86 | schmittBuffer[j+1]< t2) && j<blockSize; j++); |
---|
| 87 | startpoint=j; |
---|
| 88 | schmittTriggered=0; |
---|
| 89 | endpoint=startpoint+1; |
---|
| 90 | for(j=startpoint,tc=0; j<blockSize; j++) { |
---|
| 91 | if (!schmittTriggered) { |
---|
| 92 | schmittTriggered = (schmittBuffer[j] >= t1); |
---|
| 93 | } else if (schmittBuffer[j]>=t2 && schmittBuffer[j+1]<t2) { |
---|
| 94 | endpoint=j; |
---|
| 95 | tc++; |
---|
| 96 | schmittTriggered = 0; |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | if (endpoint > startpoint) { |
---|
| 100 | freq = ((smpl_t)p->rate*(tc/(smpl_t)(endpoint-startpoint))); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | p->schmittBuffer = schmittBuffer; |
---|
| 106 | p->schmittPointer = schmittPointer; |
---|
| 107 | return freq; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void del_aubio_pitchschmitt (aubio_pitchschmitt_t *p) |
---|
| 111 | { |
---|
| 112 | AUBIO_FREE(p->schmittBuffer); |
---|
| 113 | AUBIO_FREE(p); |
---|
| 114 | } |
---|
| 115 | |
---|