1 /*
2  * Zaz
3  * Copyright (C) Remigiusz Dybka 2009 <remigiusz.dybka@gmail.com>
4  *
5  Zaz is free software: you can redistribute it and/or modify it
6  under the terms of the GNU General Public License as published by the
7  Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Zaz is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  See the GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef SAMPLE_H_INCLUDED
20 #define SAMPLE_H_INCLUDED
21 
22 #include "common.h"
23 #include <vorbis/codec.h>
24 #include <vorbis/vorbisfile.h>
25 
26 namespace Scenes
27 {
28 class Sample
29 {
30     // the sample data is always 16bit signed interlaced left / right channel
31 public:
32     virtual Sint16 * getSampleData(uint requested_length, uint &returned_length) = 0; // if returned_length < requested length - end of sample
33     virtual bool getStreaming() = 0; // if not seekable, the sample will be used only once
34     virtual uint getNumChannels() = 0; // 1 or 2
35 
36     virtual uint getLength() = 0; // * length in samples
37     virtual void Restart() = 0; // may be called for streaming sample
~Sample()38     virtual ~Sample() {};
39     // * - will not be called if getSeekable() == false
40 };
41 
42 
43 class WaveSample : Sample
44 {
45 private:
46     SDL_AudioSpec spec;
47     Uint32 length;
48     Uint32 offs;
49     Sint16 *buff;
50     bool loaded;
51 
52 public:
53     WaveSample(const string filename);
54     ~WaveSample();
55 
56     uint getLength();
57     Sint16 * getSampleData(uint requested_length, uint &returned_length); // if returned_length < requested length - end of sample
58     bool getStreaming(); // if not seekable, the sample will be used only once
59     uint getNumChannels(); // 1 or 2
60     void Restart(); // may be called for unseekable sample
61 
62 };
63 
64 class OggSample : Sample
65 {
66 private:
67     Uint32 length;
68     Uint32 offs;
69     Sint16 *buff;
70     int channels;
71     bool loaded;
72 
73 public:
74     OggSample(const string filename);
75     ~OggSample();
76 
77     uint getLength();
78     Sint16 * getSampleData(uint requested_length, uint &returned_length); // if returned_length < requested length - end of sample
79     bool getStreaming(); // if not seekable, the sample will be used only once
80     uint getNumChannels(); // 1 or 2
81     void Restart(); // may be called for unseekable sample
82 };
83 
84 class StreamingOggSample : Sample
85 {
86 private:
87     Uint64 length;
88     Sint16 *buff;
89     uint bufflen;
90     int channels;
91     bool loaded;
92     vorbis_info *vi;
93     OggVorbis_File vf;
94     FILE *inphile;
95 
96 public:
97     StreamingOggSample(const string filename);
98     ~StreamingOggSample();
99 
100     uint getLength();
101     Sint16 * getSampleData(uint requested_length, uint &returned_length); // if returned_length < requested length - end of sample
102     bool getStreaming(); // if not seekable, the sample will be used only once
103     uint getNumChannels(); // 1 or 2
104     void Restart(); // may be called for unseekable sample*/
105 };
106 }
107 
108 
109 #endif // SAMPLE_H_INCLUDED
110