source: examples/aubionotes.c @ 20d8266

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

examples: more header updates to GPLv3

  • Property mode set to 100644
File size: 5.3 KB
RevLine 
[96fb8ad]1/*
[6a2478c]2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
[96fb8ad]3
[6a2478c]4  This file is part of aubio.
[96fb8ad]5
[6a2478c]6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
[96fb8ad]18
19*/
20
[299ad52]21#define AUBIO_UNSTABLE 1 // for fvec_median_channel
22
[96fb8ad]23#include "utils.h"
24
[d4c5de7]25/* pitch objects */
26smpl_t pitch = 0.;
27
28uint_t median = 6;
29smpl_t curlevel = 0.;
30
[b14107f]31aubio_pitch_t *pitchdet;
[d4c5de7]32
33fvec_t *note_buffer = NULL;
34fvec_t *note_buffer2 = NULL;
35
36smpl_t curnote = 0.;
37smpl_t newnote = 0.;
38uint_t isready = 0;
[1f40359]39unsigned int pos = 0; /*frames%dspblocksize*/
[a0fd4e4]40
[b14107f]41aubio_pitch_t *pitchdet;
[d4c5de7]42aubio_onset_t *o;
43fvec_t *onset;
44fvec_t *pitch_obuf;
45
46/** append new note candidate to the note_buffer and return filtered value. we
47 * need to copy the input array as fvec_median destroy its input data.*/
48void note_append (fvec_t * note_buffer, smpl_t curnote);
49uint_t get_note (fvec_t * note_buffer, fvec_t * note_buffer2);
50
51static int aubio_process(smpl_t **input, smpl_t **output, int nframes) {
[96fb8ad]52  unsigned int i;       /*channels*/
53  unsigned int j;       /*frames*/
[af35ed0]54  for (j=0;j<(unsigned)nframes;j++) {
[96fb8ad]55    if(usejack) {
56      for (i=0;i<channels;i++) {
57        /* write input to datanew */
58        fvec_write_sample(ibuf, input[i][j], i, pos);
59        /* put synthnew in output */
60        output[i][j] = fvec_read_sample(obuf, i, pos);
61      }
62    }
63    /*time for fft*/
64    if (pos == overlap_size-1) {         
65      /* block loop */
[d4c5de7]66      aubio_onset_do(o, ibuf, onset);
[96fb8ad]67     
[b14107f]68      aubio_pitch_do (pitchdet, ibuf, pitch_obuf);
[61316a6]69      pitch = fvec_read_sample(pitch_obuf, 0, 0);
[a0fd4e4]70      if(median){
[bd2f2ab]71              note_append(note_buffer, pitch);
[a0fd4e4]72      }
[96fb8ad]73
74      /* curlevel is negatif or 1 if silence */
[660cad22]75      curlevel = aubio_level_detection(ibuf, silence);
[d4c5de7]76      if (fvec_read_sample(onset, 0, 0)) {
[bd2f2ab]77              /* test for silence */
78              if (curlevel == 1.) {
79                      if (median) isready = 0;
80                      /* send note off */
81                      send_noteon(curnote,0);
82              } else {
83                      if (median) {
84                              isready = 1;
85                      } else {
86                              /* kill old note */
87                              send_noteon(curnote,0);
88                              /* get and send new one */
[144aff7]89                              send_noteon(pitch,127+(int)floor(curlevel));
[bd2f2ab]90                              curnote = pitch;
91                      }
92
93                      for (pos = 0; pos < overlap_size; pos++){
94                              obuf->data[0][pos] = woodblock->data[0][pos];
95                      }
96              }
[96fb8ad]97      } else {
[a0fd4e4]98              if (median) {
[bd2f2ab]99                      if (isready > 0)
100                              isready++;
101                      if (isready == median)
102                      {
103                              /* kill old note */
104                              send_noteon(curnote,0);
105                              newnote = get_note(note_buffer, note_buffer2);
106                              curnote = newnote;
107                              /* get and send new one */
108                              if (curnote>45){
[144aff7]109                                      send_noteon(curnote,127+(int)floor(curlevel));
[bd2f2ab]110                              }
111                      }
112              } // if median
[96fb8ad]113        for (pos = 0; pos < overlap_size; pos++)
114          obuf->data[0][pos] = 0.;
115      }
116      /* end of block loop */
117      pos = -1; /* so it will be zero next j loop */
118    }
119    pos++;
120  }
121  return 1;
122}
123
[d4c5de7]124static void process_print (void) {
[bd2f2ab]125      if (verbose) outmsg("%f\n",pitch);
[a0fd4e4]126}
[96fb8ad]127
[d4c5de7]128void
129note_append (fvec_t * note_buffer, smpl_t curnote)
130{
131  uint_t i = 0;
132  for (i = 0; i < note_buffer->length - 1; i++) {
133    note_buffer->data[0][i] = note_buffer->data[0][i + 1];
134  }
135  note_buffer->data[0][note_buffer->length - 1] = curnote;
136  return;
137}
138
139uint_t
140get_note (fvec_t * note_buffer, fvec_t * note_buffer2)
141{
[247ce7d]142  uint_t i;
[d4c5de7]143  for (i = 0; i < note_buffer->length; i++) {
144    note_buffer2->data[0][i] = note_buffer->data[0][i];
145  }
[247ce7d]146  return fvec_median_channel (note_buffer2, 0);
[d4c5de7]147}
148
[bd2f2ab]149int main(int argc, char **argv) {
150  examples_common_init(argc,argv);
[d4c5de7]151
152  o = new_aubio_onset (onset_mode, buffer_size, overlap_size, channels,
153          samplerate);
[40acfbc]154  if (threshold != 0.) aubio_onset_set_threshold (o, threshold);
[d4c5de7]155  onset = new_fvec (1, channels);
156
[b14107f]157  pitchdet = new_aubio_pitch (pitch_mode, buffer_size * 4,
[d4c5de7]158          overlap_size, channels, samplerate);
[b14107f]159  aubio_pitch_set_tolerance (pitchdet, 0.7);
[d4c5de7]160  pitch_obuf = new_fvec (1, channels);
161  if (median) {
162      note_buffer = new_fvec (median, 1);
163      note_buffer2 = new_fvec (median, 1);
164  }
165
[bd2f2ab]166  examples_common_process(aubio_process, process_print);
[d4c5de7]167
168  send_noteon (curnote, 0);
[b14107f]169  del_aubio_pitch (pitchdet);
[d4c5de7]170  if (median) {
171      del_fvec (note_buffer);
172      del_fvec (note_buffer2);
173  }
174  del_fvec (pitch_obuf);
175
[bd2f2ab]176  examples_common_del();
[96fb8ad]177  debug("End of program.\n");
178  fflush(stderr);
179  return 0;
180}
181
Note: See TracBrowser for help on using the repository browser.