source: examples/aubioonset.c @ aa3c8b7

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

cleaned up verbose output

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2   Copyright (C) 2003 Paul Brossier
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#include <stdio.h>
20#include <stdlib.h>
21#include <stdarg.h>
22#include <getopt.h>
23#include <unistd.h>
24#include "aubio.h"
25#include "utils.h"
26
27int aubio_process(float **input, float **output, int nframes);
28
29const char * output_filename = NULL;
30const char * input_filename  = NULL;
31const char * onset_filename  = "/usr/share/sounds/aubio/woodblock.aiff";
32
33/* settings */
34int verbose = 0;
35int usejack = 0;
36int usedoubled = 1;
37
38
39/* energy,specdiff,hfc,complexdomain,phase */
40aubio_onsetdetection_type type_onset  = hfc;
41aubio_onsetdetection_type type_onset2 = complexdomain;
42smpl_t threshold                      = 0.1;
43smpl_t threshold2                     = -90.;
44uint_t buffer_size                    = 1024;
45uint_t overlap_size                   = 512;
46uint_t channels                       = 1;
47uint_t samplerate                     = 44100;
48
49/* global objects */
50aubio_pvoc_t * pv;
51fvec_t * ibuf;
52fvec_t * obuf;
53cvec_t * fftgrain;
54fvec_t * woodblock;
55aubio_onsetdetection_t *o;
56aubio_onsetdetection_t *o2;
57fvec_t *onset;
58fvec_t *onset2;
59int isonset = 0;
60aubio_pickpeak_t * parms;
61
62int aubio_process(float **input, float **output, int nframes) {
63  unsigned int i;       /*channels*/
64  unsigned int j;       /*frames*/
65  unsigned int pos = 0; /*frames%dspblocksize*/
66  for (j=0;j<nframes;j++) {
67    if(usejack) {
68      for (i=0;i<channels;i++) {
69        /* write input to datanew */
70        fvec_write_sample(ibuf, input[i][j], i, pos);
71        /* put synthnew in output */
72        output[i][j] = fvec_read_sample(obuf, i, pos);
73      }
74    }
75    /*time for fft*/
76    if (pos == overlap_size-1) {         
77      /* block loop */
78      aubio_pvoc_do (pv,ibuf, fftgrain);
79      aubio_onsetdetection(o,fftgrain, onset);
80      if (usedoubled) {
81        aubio_onsetdetection(o2,fftgrain, onset2);
82        onset->data[0][0] *= onset2->data[0][0];
83      }
84      isonset = aubio_peakpick_pimrt(onset,parms);
85      if (isonset) {
86        /* test for silence */
87        if (aubio_silence_detection(ibuf, threshold2)==1)
88          isonset=0;
89        else
90          for (pos = 0; pos < overlap_size; pos++){
91            obuf->data[0][pos] = woodblock->data[0][pos];
92          }
93      } else {
94        for (pos = 0; pos < overlap_size; pos++)
95          obuf->data[0][pos] = 0.;
96      }
97      //aubio_pvoc_rdo(pv,fftgrain, obuf);
98      /* end of block loop */
99      pos = -1; /* so it will be zero next j loop */
100    }
101    pos++;
102  }
103  return 1;
104}
105
106int main(int argc, char **argv) {
107  int frames;
108
109  aubio_file_t * file = NULL;
110  aubio_file_t * fileout = NULL;
111
112  aubio_file_t * onsetfile = new_file_ro(onset_filename);
113  parse_args(argc, argv);
114
115  if(!usejack)
116  {
117    debug("Opening files ...\n");
118    file = new_file_ro (input_filename);
119    if (verbose) file_info(file);
120    channels = aubio_file_channels(file);
121    if (output_filename != NULL)
122      fileout = new_file_wo(file, output_filename);
123  }
124
125  ibuf      = new_fvec(overlap_size, channels);
126  obuf      = new_fvec(overlap_size, channels);
127  woodblock = new_fvec(buffer_size,1);
128  fftgrain  = new_cvec(buffer_size, channels);
129
130  /* read the output sound once */
131  file_read(onsetfile, overlap_size, woodblock);
132
133  /* phase vocoder */
134  debug("Phase voc init ... \n");
135  pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
136
137  /* onsets */
138  parms = new_aubio_peakpicker(threshold);
139  o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
140  onset = new_fvec(1, channels);
141  if (usedoubled)    {
142    o2 = new_aubio_onsetdetection(type_onset2,buffer_size,channels);
143    onset2 = new_fvec(1 , channels);
144  }
145
146  if(usejack) {
147#ifdef JACK_SUPPORT
148    aubio_jack_t * jack_setup;
149    debug("Jack init ...\n");
150    jack_setup = new_aubio_jack(channels, channels,
151          (aubio_process_func_t)aubio_process);
152    debug("Jack activation ...\n");
153    aubio_jack_activate(jack_setup);
154    debug("Processing (Ctrl+C to quit) ...\n");
155    pause();
156    aubio_jack_close(jack_setup);
157#else
158    outmsg("Compiled without jack output, exiting.");
159#endif
160
161  } else {
162    /* phasevoc */
163    debug("Processing ...\n");
164
165    frames = 0;
166
167    while (overlap_size == file_read(file, overlap_size, ibuf))
168    {
169      isonset=0;
170      aubio_process(ibuf->data, obuf->data, overlap_size);
171      /* output times in seconds, taking back some
172       * delay to ensure the label is _before_ the
173       * actual onset */
174      if (isonset && output_filename == NULL) {
175        outmsg("%f\n",(frames-4)*overlap_size/(float)samplerate);
176      }
177      if (output_filename != NULL) {
178        file_write(fileout,overlap_size,obuf);
179      }
180      frames++;
181    }
182
183    debug("Processed %d frames of %d samples.\n", frames, buffer_size);
184    del_file(file);
185
186    if (output_filename != NULL)
187      del_file(fileout);
188
189  }
190
191  del_aubio_pvoc(pv);
192  del_fvec(obuf);
193  del_fvec(ibuf);
194  del_cvec(fftgrain);
195  del_fvec(onset);
196
197  debug("End of program.\n");
198  fflush(stderr);
199  return 0;
200}
201
Note: See TracBrowser for help on using the repository browser.