src/fmat.c: new_fmat() takes height as first argument
[aubio.git] / src / synth / sampler.c
1 /*
2   Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org>
3
4   This file is part of aubio.
5
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/>.
18
19 */
20
21
22 #include "config.h"
23 #include "aubio_priv.h"
24 #include "fvec.h"
25 #include "fmat.h"
26 #include "io/source.h"
27 #include "synth/sampler.h"
28
29 struct _aubio_sampler_t {
30   uint_t samplerate;
31   uint_t blocksize;
32   aubio_source_t *source;
33   fvec_t *source_output;
34   fmat_t *source_output_multi;
35   char_t *uri;
36   uint_t playing;
37 };
38
39 aubio_sampler_t *new_aubio_sampler(uint_t samplerate, uint_t blocksize)
40 {
41   aubio_sampler_t *s = AUBIO_NEW(aubio_sampler_t);
42   s->samplerate = samplerate;
43   s->blocksize = blocksize;
44   s->source_output = new_fvec(blocksize);
45   s->source_output_multi = new_fmat(4, blocksize);
46   s->source = NULL;
47   s->playing = 0;
48   return s;
49 }
50
51 uint_t aubio_sampler_load( aubio_sampler_t * o, char_t * uri )
52 {
53   if (o->source) del_aubio_source(o->source);
54   o->uri = uri;
55   o->source = new_aubio_source(uri, o->samplerate, o->blocksize);
56   if (o->source) return 0;
57   AUBIO_ERR("sampler: failed loading %s", uri);
58   return 1;
59 }
60
61 void aubio_sampler_do ( aubio_sampler_t * o, fvec_t * input, fvec_t * output)
62 {
63   uint_t read = 0, i;
64   if (o->playing) {
65     aubio_source_do (o->source, o->source_output, &read);
66     for (i = 0; i < output->length; i++) {
67       output->data[i] += o->source_output->data[i];
68     }
69     if (read < o->blocksize) o->playing = 0;
70   }
71   if (input && input != output) {
72     for (i = 0; i < output->length; i++) {
73       output->data[i] += input->data[i];
74     }
75   }
76 }
77
78 void aubio_sampler_do_multi ( aubio_sampler_t * o, fmat_t * input, fmat_t * output)
79 {
80   uint_t read = 0, i, j;
81   if (o->playing) {
82     aubio_source_do_multi (o->source, o->source_output_multi, &read);
83     for (i = 0; i < output->height; i++) {
84       for (j = 0; j < output->length; j++) {
85         output->data[i][j] += o->source_output_multi->data[i][j];
86       }
87     }
88     if ( read < o->blocksize ) o->playing = 0;
89   }
90   if (input && input != output) {
91     for (i = 0; i < output->height; i++) {
92       for (j = 0; j < output->length; j++) {
93         output->data[i][j] += input->data[i][j];
94       }
95     }
96   }
97 }
98
99 uint_t aubio_sampler_get_playing ( aubio_sampler_t * o )
100 {
101   return o->playing;
102 }
103
104 uint_t aubio_sampler_set_playing ( aubio_sampler_t * o, uint_t playing )
105 {
106   o->playing = (playing == 1) ? 1 : 0;
107   return 0;
108 }
109
110 uint_t aubio_sampler_play ( aubio_sampler_t * o )
111 {
112   aubio_source_seek (o->source, 0);
113   return aubio_sampler_set_playing (o, 1);
114 }
115
116 uint_t aubio_sampler_stop ( aubio_sampler_t * o )
117 {
118   return aubio_sampler_set_playing (o, 0);
119 }
120
121 void del_aubio_sampler( aubio_sampler_t * o )
122 {
123   if (o->source) {
124     del_aubio_source(o->source);
125   }
126   del_fvec(o->source_output);
127   del_fmat(o->source_output_multi);
128   AUBIO_FREE(o);
129 }