1 | /* |
---|
2 | Copyright (C) 2016 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 | #include "aubio_priv.h" |
---|
22 | #include "fmat.h" |
---|
23 | |
---|
24 | uint_t |
---|
25 | aubio_io_validate_samplerate(const char_t *kind, const char_t *path, uint_t samplerate) |
---|
26 | { |
---|
27 | if ((sint_t)(samplerate) <= 0) { |
---|
28 | AUBIO_ERR("%s: failed creating %s, samplerate should be positive, not %d\n", |
---|
29 | kind, path, samplerate); |
---|
30 | return AUBIO_FAIL; |
---|
31 | } |
---|
32 | if ((sint_t)samplerate > AUBIO_MAX_SAMPLERATE) { |
---|
33 | AUBIO_ERR("%s: failed creating %s, samplerate %dHz is too large\n", |
---|
34 | kind, path, samplerate); |
---|
35 | return AUBIO_FAIL; |
---|
36 | } |
---|
37 | return AUBIO_OK; |
---|
38 | } |
---|
39 | |
---|
40 | uint_t |
---|
41 | aubio_io_validate_channels(const char_t *kind, const char_t *path, uint_t channels) |
---|
42 | { |
---|
43 | if ((sint_t)(channels) <= 0) { |
---|
44 | AUBIO_ERR("sink_%s: failed creating %s, channels should be positive, not %d\n", |
---|
45 | kind, path, channels); |
---|
46 | return AUBIO_FAIL; |
---|
47 | } |
---|
48 | if (channels > AUBIO_MAX_CHANNELS) { |
---|
49 | AUBIO_ERR("sink_%s: failed creating %s, too many channels (%d but %d available)\n", |
---|
50 | kind, path, channels, AUBIO_MAX_CHANNELS); |
---|
51 | return AUBIO_FAIL; |
---|
52 | } |
---|
53 | return AUBIO_OK; |
---|
54 | } |
---|
55 | |
---|
56 | uint_t |
---|
57 | aubio_source_validate_input_length(const char_t *kind, const char_t *path, |
---|
58 | uint_t hop_size, uint_t read_data_length) |
---|
59 | { |
---|
60 | uint_t length = hop_size; |
---|
61 | if (hop_size < read_data_length) { |
---|
62 | AUBIO_WRN("%s: partial read from %s, trying to read %d frames, but" |
---|
63 | " hop_size is %d\n", kind, path, read_data_length, hop_size); |
---|
64 | } else if (hop_size > read_data_length) { |
---|
65 | AUBIO_WRN("%s: partial read from %s, trying to read %d frames into" |
---|
66 | " a buffer of length %d\n", kind, path, hop_size, read_data_length); |
---|
67 | length = read_data_length; |
---|
68 | } |
---|
69 | return length; |
---|
70 | } |
---|
71 | |
---|
72 | uint_t |
---|
73 | aubio_source_validate_input_channels(const char_t *kind, const char_t *path, |
---|
74 | uint_t source_channels, uint_t read_data_height) |
---|
75 | { |
---|
76 | uint_t channels = source_channels; |
---|
77 | if (read_data_height < source_channels) { |
---|
78 | AUBIO_WRN("%s: partial read from %s, trying to read %d channels," |
---|
79 | " but found output of height %d\n", kind, path, source_channels, |
---|
80 | read_data_height); |
---|
81 | channels = read_data_height; |
---|
82 | } else if (read_data_height > source_channels) { |
---|
83 | // do not show a warning when trying to read into more channels than |
---|
84 | // the input source. |
---|
85 | #if 0 |
---|
86 | AUBIO_WRN("%s: partial read from %s, trying to read %d channels," |
---|
87 | " but found output of height %d\n", kind, path, source_channels, |
---|
88 | read_data_height); |
---|
89 | #endif |
---|
90 | channels = source_channels; |
---|
91 | } |
---|
92 | return channels; |
---|
93 | } |
---|
94 | |
---|
95 | void |
---|
96 | aubio_source_pad_output (fvec_t *read_data, uint_t source_read) |
---|
97 | { |
---|
98 | uint_t i = 0; |
---|
99 | if (source_read < read_data->length) { |
---|
100 | for (i = source_read; i < read_data->length; i++) { |
---|
101 | read_data->data[i] = 0.; |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | void |
---|
107 | aubio_source_pad_multi_output (fmat_t *read_data, |
---|
108 | uint_t source_channels, uint_t source_read) { |
---|
109 | uint_t i, j; |
---|
110 | if (source_read < read_data->length) { |
---|
111 | for (i = 0; i < read_data->height; i++) { |
---|
112 | for (j = source_read; j < read_data->length; j++) { |
---|
113 | read_data->data[i][j] = 0.; |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | uint_t |
---|
120 | aubio_sink_validate_input_length(const char_t *kind, const char_t *path, |
---|
121 | uint_t max_size, uint_t write_data_length, uint_t write) |
---|
122 | { |
---|
123 | uint_t can_write = write; |
---|
124 | |
---|
125 | if (write > max_size) { |
---|
126 | AUBIO_WRN("%s: partial write to %s, trying to write %d frames," |
---|
127 | " at most %d can be written at once\n", kind, path, write, max_size); |
---|
128 | can_write = max_size; |
---|
129 | } |
---|
130 | |
---|
131 | if (can_write > write_data_length) { |
---|
132 | AUBIO_WRN("%s: partial write to %s, trying to write %d frames," |
---|
133 | " but found input of length %d\n", kind, path, write, |
---|
134 | write_data_length); |
---|
135 | can_write = write_data_length; |
---|
136 | } |
---|
137 | |
---|
138 | return can_write; |
---|
139 | } |
---|
140 | |
---|
141 | uint_t |
---|
142 | aubio_sink_validate_input_channels(const char_t *kind, const char_t *path, |
---|
143 | uint_t sink_channels, uint_t write_data_height) |
---|
144 | { |
---|
145 | uint_t channels = sink_channels; |
---|
146 | if (write_data_height < sink_channels) { |
---|
147 | AUBIO_WRN("%s: partial write to %s, trying to write %d channels," |
---|
148 | " but found input of height %d\n", kind, path, sink_channels, |
---|
149 | write_data_height); |
---|
150 | channels = write_data_height; |
---|
151 | } |
---|
152 | return channels; |
---|
153 | } |
---|