+ EQ-5: refactoring to eliminate code duplication, side effect: stereo meters
[calf.git] / src / calf / wave.h
blobfa42fd61edb81a55a9eb82957ba76a5fb162b6e8
1 /* Calf DSP Library
2 * Primitive interface to NAS WAV file operations.
3 * Not really useful for now, I've used it for testing previously.
5 * Copyright (C) 2007 Krzysztof Foltman
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this program; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02111-1307, USA.
23 #include <audio/wave.h>
25 namespace dsp {
27 template<class T>
28 bool load_wave(dsp::dynamic_buffer<T> &dest, const char *file_name) {
29 WaveInfo *file = WaveOpenFileForReading(file_name);
30 typedef dsp::sample_traits<T> st;
31 if (file->channels == st::channels && file->bitsPerSample == st::bps) {
32 dest.resize(file->numSamples);
33 WaveReadFile((char *)dest.data(), dest.size()*sizeof(T), file);
34 WaveCloseFile(file);
35 return true;
37 WaveCloseFile(file);
38 fprintf(stderr, "Sorry, need a %d channels and %d bps, got %d channels and %d bps\n", st::channels, st::bps, file->channels, file->bitsPerSample);
39 return false;
42 template<class T>
43 class wave_player {
44 T *data;
45 bool is_active;
46 unsigned int size;
47 wpos pos, rate;
48 public:
49 void set_wave(T *_data, int _size) {
50 data = _data;
51 size = _size;
52 is_active = false;
54 void get_wave(T *&_data, int &_size) {
55 _data = data;
56 _size = size;
58 void set_pos(wpos _pos) {
59 pos = _pos;
60 is_active = true;
62 wpos get_pos() {
63 return pos;
65 void set_rate(wpos _rate) {
66 rate = _rate;
68 template<class U>U get() {
69 U result;
70 if (!is_active) {
71 dsp::zero(result);
72 return result;
74 unsigned int ipos = pos.ipart();
75 if (ipos>=size) {
76 dsp::zero(result);
77 is_active = false;
78 return result;
80 result = pos.lerp_ptr_lookup_int<T, 12, int >(data);
81 pos += rate;
82 return result;
84 bool get_active() {
85 return is_active;