source: src/pitch/pitchschmitt.c @ e6a78ea

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since e6a78ea was e6a78ea, checked in by Paul Brossier <piem@piem.org>, 14 years ago

src: update all headers to GPLv3

  • Property mode set to 100644
File size: 3.3 KB
Line 
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
26smpl_t aubio_schmittS16LE (aubio_pitchschmitt_t *p, uint_t nframes, signed short int *indata);
27
28struct _aubio_pitchschmitt_t {
29        uint_t blockSize;
30        uint_t rate;
31        signed short int *schmittBuffer;
32        signed short int *schmittPointer;
33        signed short int *buf;
34};
35
36aubio_pitchschmitt_t * new_aubio_pitchschmitt (uint_t size)
37{
38  aubio_pitchschmitt_t * p = AUBIO_NEW(aubio_pitchschmitt_t);
39  p->blockSize = size;
40  p->schmittBuffer = AUBIO_ARRAY(signed short int,p->blockSize);
41  p->buf = AUBIO_ARRAY(signed short int,p->blockSize);
42  p->schmittPointer = p->schmittBuffer;
43  return p;
44}
45
46void
47aubio_pitchschmitt_do (aubio_pitchschmitt_t * p, fvec_t * input,
48    fvec_t * output)
49{
50  uint_t i, j;
51  for (i = 0; i < input->channels; i++) {
52    for (j = 0; j < input->length; j++) {
53      p->buf[j] = input->data[i][j] * 32768.;
54    }
55    output->data[i][0] = aubio_schmittS16LE (p, input->length, p->buf);
56  }
57}
58
59smpl_t aubio_schmittS16LE (aubio_pitchschmitt_t *p, uint_t nframes, signed short int *indata)
60{
61  uint_t i, j;
62  uint_t blockSize = p->blockSize;
63  signed short int *schmittBuffer = p->schmittBuffer;
64  signed short int *schmittPointer = p->schmittPointer;
65
66  smpl_t period = 0., trigfact = 0.6;
67
68  for (i=0; i<nframes; i++) {
69    *schmittPointer++ = indata[i];
70    if (schmittPointer-schmittBuffer >= (sint_t)blockSize) {
71      sint_t endpoint, startpoint, t1, t2, A1, A2, tc, schmittTriggered;
72
73      schmittPointer = schmittBuffer;
74
75      for (j=0,A1=0,A2=0; j<blockSize; j++) {
76        if (schmittBuffer[j]>0 && A1<schmittBuffer[j])  A1 = schmittBuffer[j];
77        if (schmittBuffer[j]<0 && A2<-schmittBuffer[j]) A2 = -schmittBuffer[j];
78      }
79      t1 =   (sint_t)( A1 * trigfact + 0.5);
80      t2 = - (sint_t)( A2 * trigfact + 0.5);
81      startpoint=0;
82      for (j=1; schmittBuffer[j]<=t1 && j<blockSize; j++);
83      for (; !(schmittBuffer[j]  >=t2 &&
84               schmittBuffer[j+1]< t2) && j<blockSize; j++);
85      startpoint=j;
86      schmittTriggered=0;
87      endpoint=startpoint+1;
88      for(j=startpoint,tc=0; j<blockSize; j++) {
89        if (!schmittTriggered) {
90          schmittTriggered = (schmittBuffer[j] >= t1);
91        } else if (schmittBuffer[j]>=t2 && schmittBuffer[j+1]<t2) {
92          endpoint=j;
93          tc++;
94          schmittTriggered = 0;
95        }
96      }
97      if ((endpoint > startpoint) && (tc > 0)) {
98        period = (smpl_t)(endpoint-startpoint)/tc;
99      }
100    }
101  }
102
103  p->schmittBuffer  = schmittBuffer;
104  p->schmittPointer = schmittPointer;
105  return period;
106}
107
108void del_aubio_pitchschmitt (aubio_pitchschmitt_t *p)
109{
110  AUBIO_FREE(p->schmittBuffer);
111  AUBIO_FREE(p);
112}
113
Note: See TracBrowser for help on using the repository browser.