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.
4  *
5  * Copyright (C) 2007 Krzysztof Foltman
6  *
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.
11  *
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.
16  *
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.
21  */
22 
23 #include <audio/wave.h>
24 
25 namespace dsp {
26 
27 template<class T>
load_wave(dsp::dynamic_buffer<T> & dest,const char * file_name)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;
36     }
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;
40 }
41 
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:
set_wave(T * _data,int _size)49     void set_wave(T *_data, int _size) {
50         data = _data;
51         size = _size;
52         is_active = false;
53     }
get_wave(T * & _data,int & _size)54     void get_wave(T *&_data, int &_size) {
55         _data = data;
56         _size = size;
57     }
set_pos(wpos _pos)58     void set_pos(wpos _pos) {
59         pos = _pos;
60         is_active = true;
61     }
get_pos()62     wpos get_pos() {
63         return pos;
64     }
set_rate(wpos _rate)65     void set_rate(wpos _rate) {
66         rate = _rate;
67     }
get()68     template<class U>U get() {
69         U result;
70         if (!is_active) {
71             dsp::zero(result);
72             return result;
73         }
74         unsigned int ipos = pos.ipart();
75         if (ipos>=size) {
76             dsp::zero(result);
77             is_active = false;
78             return result;
79         }
80         result = pos.lerp_ptr_lookup_int<T, 12, int >(data);
81         pos += rate;
82         return result;
83     }
get_active()84     bool get_active() {
85         return is_active;
86     }
87 };
88 
89 }
90