Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 74 lines (62 sloc) 2.844 kb
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
1 // Copyright (C) 2012 Henner Zeller <h.zeller@acm.org>
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 #include <sndfile.h>
17
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
18 // A file-backed buffer for a SNDFILE, that is only filled on demand via
19 // a SoundSource.
20 // If Read() is called beyond the current available data, a callback is
21 // called to write more into the SNDFILE.
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
22 class ConversionBuffer {
23 public:
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
24 // SoundSource, a instance of which needs to be passed to the
25 // ConversionBuffer.
26 class SoundSource {
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
27 public:
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
28 virtual ~SoundSource() {}
29
30 // The soundfile is set by this conversion buffer and to be filled when
31 // requested. There can be an error in opening the sound-file, in that
32 // case SetOutputSoundfile() will be called with NULL.
33 // Ask sf_strerror() to find out why.
5a6dffa @hzeller o comment update.
authored
34 // Ownership is passed to the SoundSource, receiver needs to
35 // sf_close() the file.
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
36 virtual void SetOutputSoundfile(SNDFILE *sndfile) = 0;
37
38 // This callback is called by the ConversionBuffer if it needs more data.
39 // Rerturns 'true' if there is more, 'false' if that was the last available
40 // data.
41 virtual bool AddMoreSoundData() = 0;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
42 };
43
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
44 // Create a conversion buffer that holds "buffer_size" bytes.
45 // The "source" will be informed to what SNDFILE to write to and whenever
46 // this ConversionBuffer lusts for more data (it then calls
47 // AddMoreSoundData()).
48 // Ownership is not taken over for source.
49 ConversionBuffer(SoundSource *source, const SF_INFO &info);
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
50 ~ConversionBuffer();
51
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
52 // Read data from buffer. Can block and call the SoundSource first to get
53 // more data if needed.
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
54 ssize_t Read(char *buf, size_t size, off_t offset);
55
56 private:
57 static sf_count_t SndTell(void *userdata);
58 static sf_count_t SndWrite(const void *ptr, sf_count_t count, void *userdata);
59
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
60 // Append data. Called via the SndWrite() virtual file callback.
61 ssize_t Append(const void *data, size_t count);
62
63 // Current max file position.
64 off_t Tell() const { return total_written_; }
65
66 // Create a SNDFILE the user has to write to in the WriteToSoundfile callback.
67 // Can be NULL on error.
68 SNDFILE *CreateOutputSoundfile(const SF_INFO &info);
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
69
fd4ed76 @hzeller o write to a file instead of a buffer.
authored
70 SoundSource *const source_;
71 int tmpfile_;
72 off_t total_written_;
6e895e3 @hzeller o add conversion buffer that uses the virtual file provided
authored
73 };
Something went wrong with that request. Please try again.