1 /*  $Id: SoundRenderer.h,v 1.1 2012/07/08 00:45:59 sarrazip Exp $
2 
3     flatzebra - SDL-based sound renderer
4     Copyright (C) 2011 Pierre Sarrazin <http://sarrazip.com/>
5 
6     This program is free software; you can redistribute it and/or
7     modify it under the terms of the GNU General Public License
8     as published by the Free Software Foundation; either version 2
9     of the License, or (at your option) any later version.
10 
11     This program 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
17     License along with this program; if not, write to the Free
18     Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19     Boston, MA  02110-1301, USA.
20 */
21 
22 #pragma once
23 
24 #include <roundbeetle/AutoLocker.h>
25 #include <roundbeetle/FrameSourceAdder.h>
26 
27 
28 namespace roundbeetle {
29 
30 
31 class SoundRenderer
32 {
33 protected:
34 
35     static SoundRenderer *inst;
36 
37     SoundRenderer(float _rendererFreqInHz);
38 
39 public:
40 
41     // waitForEnd() must have been called successfully.
42     //
43     virtual ~SoundRenderer();
44 
45     static SoundRenderer &instance();
46 
47     static SDL_mutex *getMutex();
48 
49     int start();
50 
51     virtual float getRendererFreq() const;
52 
53     // Returns frequency of the unique instance of this class.
54     //
55     static float freq();
56 
57     FrameSourceAdder &getTopFrameSourceAdder();
58 
59     // Can be used in a derived class to ask a thread to terminate.
60     //
61     virtual void requestStop();
62 
63     // Can be used in a derived class to reclaim a thread's resources
64     // (e.g., pthread_join()).
65     //
66     virtual void waitForEnd();
67 
68     void startRecording();
69 
70     bool isRecording() const;
71 
72     // Returns true if renderer was recording.
73     // Returns false if it was not.
74     // It is harmless to call this when not recording, although it
75     // involves locking a mutex.
76     //
77     bool stopRecording();
78 
79     bool isBusy() const;
80 
81 protected:
82 
83     // Must return error code <= -100 on error, on 0 for success.
84     //
85     virtual int initAudio() = 0;
86 
87     void getFramesFromTopSource(Frame *frameBuffer, size_t numFramesToFill);
88 
89     // Called with every framebuffer obtained from the top frame source.
90     //
91     virtual void record(Frame *frameBuffer, size_t numFramesToFill);
92 
93 private:
94 
95     SoundRenderer(const SoundRenderer &);
96     SoundRenderer & operator = (const SoundRenderer &);
97 
98 protected:
99 
100     SDL_mutex *mutex;  // owned by this object
101     float rendererFreqInHz;
102     FrameSourceAdder topFrameSource;
103     bool recording;
104 
105 };
106 
107 
108 }  // namespace roundbeetle
109