source: src/pitch/pitchschmitt.c @ d6001bf

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

src/pitch/pitchschmitt.c: avoid out of bound reads

  • Property mode set to 100644
File size: 3.4 KB
RevLine 
[7a04950]1/*
[e6a78ea]2  Copyright (C) 2004, 2005  Mario Lang <mlang@delysid.org>
3  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
[7a04950]4
[e6a78ea]5  This file is part of aubio.
[7a04950]6
[e6a78ea]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.
[7a04950]11
[e6a78ea]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/>.
[7a04950]19
20*/
21
22#include "aubio_priv.h"
[6c7d49b]23#include "fvec.h"
[2d8cffa]24#include "pitch/pitchschmitt.h"
[7a04950]25
[ad04467]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;
[7a04950]36};
37
[ad04467]38aubio_pitchschmitt_t *
39new_aubio_pitchschmitt (uint_t size)
[7a04950]40{
[ad04467]41  aubio_pitchschmitt_t *p = AUBIO_NEW (aubio_pitchschmitt_t);
[7a04950]42  p->blockSize = size;
[ad04467]43  p->schmittBuffer = AUBIO_ARRAY (signed short int, p->blockSize);
44  p->buf = AUBIO_ARRAY (signed short int, p->blockSize);
[7a04950]45  p->schmittPointer = p->schmittBuffer;
46  return p;
47}
48
[f162cf9]49void
50aubio_pitchschmitt_do (aubio_pitchschmitt_t * p, fvec_t * input,
51    fvec_t * output)
[7a04950]52{
[168337e]53  uint_t j;
54  for (j = 0; j < input->length; j++) {
55    p->buf[j] = input->data[j] * 32768.;
[7a04950]56  }
[168337e]57  output->data[0] = aubio_schmittS16LE (p, input->length, p->buf);
[7a04950]58}
59
[ad04467]60smpl_t
61aubio_schmittS16LE (aubio_pitchschmitt_t * p, uint_t nframes,
62    signed short int *indata)
[7a04950]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
[f162cf9]69  smpl_t period = 0., trigfact = 0.6;
[7a04950]70
[ad04467]71  for (i = 0; i < nframes; i++) {
[7a04950]72    *schmittPointer++ = indata[i];
[ad04467]73    if (schmittPointer - schmittBuffer >= (sint_t) blockSize) {
[7a04950]74      sint_t endpoint, startpoint, t1, t2, A1, A2, tc, schmittTriggered;
75
76      schmittPointer = schmittBuffer;
77
[ad04467]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];
[7a04950]83      }
[ad04467]84      t1 = (sint_t) (A1 * trigfact + 0.5);
85      t2 = -(sint_t) (A2 * trigfact + 0.5);
86      startpoint = 0;
[e7e11bf]87      for (j = 1; j < blockSize && schmittBuffer[j] <= t1; j++);
88      for (     ; j < blockSize - 1 && !(schmittBuffer[j] >= t2 &&
89             schmittBuffer[j + 1] < t2); j++);
[ad04467]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        }
[7a04950]101      }
[f162cf9]102      if ((endpoint > startpoint) && (tc > 0)) {
[ad04467]103        period = (smpl_t) (endpoint - startpoint) / tc;
[7a04950]104      }
105    }
106  }
107
[ad04467]108  p->schmittBuffer = schmittBuffer;
[7a04950]109  p->schmittPointer = schmittPointer;
[f162cf9]110  return period;
[7a04950]111}
112
[ad04467]113void
114del_aubio_pitchschmitt (aubio_pitchschmitt_t * p)
[7a04950]115{
[ad04467]116  AUBIO_FREE (p->schmittBuffer);
[860ef4b]117  AUBIO_FREE (p->buf);
[ad04467]118  AUBIO_FREE (p);
[7a04950]119}
Note: See TracBrowser for help on using the repository browser.