source: src/pitch/pitchschmitt.c @ e2010b3

feature/crepe_org
Last change on this file since e2010b3 was ce3ff2b, checked in by Paul Brossier <piem@piem.org>, 9 years ago

src/pitch/: add const qualifiers, filter_do_outplace to avoid modifying input

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