source: examples/aubioonset.c @ 77494e7

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

moved jack and sndfile to aubioext

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