xref: /haiku/headers/os/media/SoundPlayer.h (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1 /*	SoundPlayer.h	*/
2 /*	Copyright 1998 Be Incorporated. All rights reserved.	*/
3 
4 #if !defined(_SOUND_PLAYER_H)
5 #define _SOUND_PLAYER_H
6 
7 
8 #include <MediaDefs.h>
9 #include <BufferProducer.h>
10 #include <Locker.h>
11 #include <exception>
12 
13 
14 class BSound;
15 class sound_error : public exception {
16 	const char * m_str_const;
17 public:
18 	sound_error(const char * str);
19 	const char * what() const;
20 };
21 
22 class BSoundPlayer {
23 	friend class _SoundPlayNode;
24 public:
25 		enum sound_player_notification {
26 			B_STARTED = 1,
27 			B_STOPPED,
28 			B_SOUND_DONE
29 		};
30 
31 		BSoundPlayer(
32 				const char * name = NULL,
33 				void (*PlayBuffer)(void *, void * buffer, size_t size, const media_raw_audio_format & format) = NULL,
34 				void (*Notifier)(void *, sound_player_notification what, ...) = NULL,
35 				void * cookie = NULL);
36 		BSoundPlayer(
37 				const media_raw_audio_format * format,
38 				const char * name = NULL,
39 				void (*PlayBuffer)(void *, void * buffer, size_t size, const media_raw_audio_format & format) = NULL,
40 				void (*Notifier)(void *, sound_player_notification what, ...) = NULL,
41 				void * cookie = NULL);
42 		BSoundPlayer(
43 				const media_node & toNode,
44 				const media_multi_audio_format * format = NULL,
45 				const char * name = NULL,
46 				const media_input * input = NULL,
47 				void (*PlayBuffer)(void *, void * buffer, size_t size, const media_raw_audio_format & format) = NULL,
48 				void (*Notifier)(void *, sound_player_notification what, ...) = NULL,
49 				void * cookie = NULL);
50 virtual	~BSoundPlayer();
51 
52 		status_t InitCheck();	//	new in R4.1
53 		media_raw_audio_format Format() const;	//	new in R4.1
54 
55 		status_t Start();
56 		void Stop(
57 				bool block = true,
58 				bool flush = true);
59 
60 		typedef void (*BufferPlayerFunc)(void *, void *, size_t, const media_raw_audio_format &);
61 		BufferPlayerFunc BufferPlayer() const;
62 		void SetBufferPlayer(
63 			void (*PlayBuffer)(void *, void * buffer, size_t size, const media_raw_audio_format & format));
64 		typedef void (*EventNotifierFunc)(void *, sound_player_notification what, ...);
65 		EventNotifierFunc EventNotifier() const;
66 		void SetNotifier(
67 			void (*Notifier)(void *, sound_player_notification what, ...));
68 		void * Cookie() const;
69 		void SetCookie(
70 			void * cookie);
71 		void SetCallbacks(		//	atomic change
72 				void (*PlayBuffer)(void *, void * buffer, size_t size, const media_raw_audio_format & format) = NULL,
73 				void (*Notifier)(void *, sound_player_notification what, ...) = NULL,
74 				void * cookie = NULL);
75 
76 		typedef int32 play_id;
77 
78 		bigtime_t	CurrentTime();
79 		bigtime_t	PerformanceTime();
80 		status_t	Preroll();
81 		play_id		StartPlaying(
82 						BSound * sound,
83 						bigtime_t at_time = 0);
84 		play_id 	StartPlaying(
85 						BSound * sound,
86 						bigtime_t at_time,
87 						float with_volume);
88 		status_t 	SetSoundVolume(
89 						play_id sound,		//	update only non-NULL values
90 						float new_volume);
91 		bool 		IsPlaying(
92 						play_id id);
93 		status_t 	StopPlaying(
94 						play_id id);
95 		status_t 	WaitForSound(
96 						play_id id);
97 
98 		float 		Volume();			/* 0 - 1.0 */
99 		void 		SetVolume(			/* 0 - 1.0 */
100 						float new_volume);
101 		float		VolumeDB(			/* -xx - +xx (see GetVolumeInfo()) */
102 						bool forcePoll = false);	/* if false, cached value will be used if new enough */
103 		void		SetVolumeDB(
104 						float volume_dB);
105 		status_t	GetVolumeInfo(
106 						media_node * out_node,
107 						int32 * out_parameter,
108 						float * out_min_dB,
109 						float * out_max_dB);
110 		bigtime_t	Latency();
111 
112 virtual	bool HasData();
113 		void SetHasData(		//	this does more than just set a flag
114 				bool has_data);
115 
116 protected:
117 
118 		void SetInitError(		//	new in R4.1
119 				status_t in_error);
120 
121 private:
122 
123 virtual	status_t _Reserved_SoundPlayer_0(void *, ...);
124 virtual	status_t _Reserved_SoundPlayer_1(void *, ...);
125 virtual	status_t _Reserved_SoundPlayer_2(void *, ...);
126 virtual	status_t _Reserved_SoundPlayer_3(void *, ...);
127 virtual	status_t _Reserved_SoundPlayer_4(void *, ...);
128 virtual	status_t _Reserved_SoundPlayer_5(void *, ...);
129 virtual	status_t _Reserved_SoundPlayer_6(void *, ...);
130 virtual	status_t _Reserved_SoundPlayer_7(void *, ...);
131 
132 		_SoundPlayNode * _m_node;
133 		struct _playing_sound {
134 			_playing_sound * next;
135 			off_t cur_offset;
136 			BSound * sound;
137 			play_id id;
138 			int32 delta;
139 			int32 rate;
140 			float volume;
141 		};
142 		_playing_sound * _m_sounds;
143 		struct _waiting_sound {
144 			_waiting_sound * next;
145 			bigtime_t start_time;
146 			BSound * sound;
147 			play_id id;
148 			int32 rate;
149 			float volume;
150 		};
151 		_waiting_sound * _m_waiting;
152 		void (*_PlayBuffer)(void * cookie, void * buffer, size_t size, const media_raw_audio_format & format);
153 		void (*_Notifier)(void * cookie, sound_player_notification what, ...);
154 		BLocker _m_lock;
155 		float _m_volume;
156 		media_input m_input;
157 		media_output m_output;
158 		float * _m_mix_buffer;
159 		size_t _m_mix_buffer_size;
160 		void * _m_cookie;
161 		void * _m_buf;
162 		size_t _m_bufsize;
163 		int32 _m_has_data;
164 
165 		status_t _m_init_err;		//	new in R4.1
166 		bigtime_t _m_perfTime;
167 		BContinuousParameter * _m_volumeSlider;
168 		bigtime_t _m_gotVolume;
169 		uint32 _m_reserved[10];
170 
171 		void NotifySoundDone(
172 				play_id sound,
173 				bool got_to_play);
174 
175 		void get_volume_slider();
176 		void Init(
177 				const media_node * node,
178 				const media_multi_audio_format * format,
179 				const char * name,
180 				const media_input * input,
181 				void (*PlayBuffer)(void *, void * buffer, size_t size, const media_raw_audio_format & format),
182 				void (*Notifier)(void *, sound_player_notification what, ...),
183 				void * cookie);
184 		//	for B_STARTED and B_STOPPED, the argument is BSoundPlayer* (this)
185 		//	for B_SOUND_DONE, the arguments are play_id and true/false for whether it got to play data at all
186 virtual	void Notify(
187 				sound_player_notification what,
188 				...);
189 		//	get data into the buffer to play -- this version will use the queued BSound system
190 virtual	void PlayBuffer(
191 				void * buffer,
192 				size_t size,
193 				const media_raw_audio_format & format);
194 };
195 
196 
197 #endif /* _SOUND_PLAYER_H */
198