xref: /haiku/src/add-ons/media/media-add-ons/mixer/MixerOutput.h (revision b6270d60b29ad49ae09811b7d0b4968989229908)
1 #ifndef _MIXER_OUTPUT_H
2 #define _MIXER_OUTPUT_H
3 
4 #include "debug.h"
5 #include "ByteSwap.h"
6 #include "MixerCore.h"
7 #include <Buffer.h>
8 
9 /* The data storage for channel sources is optimized
10  * for fast data retrieval by the MixerCore, which
11  * will call GetOutputChannelSourceInfoAt() and
12  * iterate through the first source_count array entries
13  * for source_gain[] and source_type[]
14  */
15 
16 class MixerOutput
17 {
18 public:
19 	MixerOutput(MixerCore *core, const media_output &output);
20 	~MixerOutput();
21 
22 	media_output & MediaOutput();
23 
24 	void ChangeFormat(const media_multi_audio_format &format);
25 
26 	// The physical output channels
27 	uint32	GetOutputChannelCount();
28 	uint32	GetOutputChannelType(int channel);
29 	void	SetOutputChannelGain(int channel, float gain);
30 	float	GetOutputChannelGain(int channel);
31 
32 	// The Sources for each channel
33 	void	AddOutputChannelSource(int channel, int source_type, float source_gain);
34 	void	RemoveOutputChannelSource(int channel, int source_type);
35 	bool	SetOutputChannelSourceGain(int channel, int source_type, float source_gain);
36 	float	GetOutputChannelSourceGain(int channel, int source_type);
37 	bool	HasOutputChannelSource(int channel, int source_type);
38 
39 	// The Output can be muted
40 	void	SetMuted(bool yesno);
41 	bool	IsMuted();
42 
43 	// Only for use by MixerCore:
44 
45 	// For iteration of a channel's sources
46 	uint32	GetOutputChannelSourceCount(int channel);
47 	void	GetOutputChannelSourceInfoAt(int channel, int source_index, int *source_type, float *source_gain);
48 
49 	// To swap byteorder in a buffer is that is needed
50 	void	AdjustByteOrder(BBuffer *buffer);
51 
52 private:
53 	void UpdateByteOrderSwap();
54 	void UpdateOutputChannels();
55 	void AssignDefaultSources();
56 
57 private:
58 
59 	/* An entry in the source array is not the same as the
60 	 * channel type, but the number should be the same
61 	 */
62 	 enum {
63 		MAX_SOURCE_ENTRIES = MAX_CHANNEL_TYPES
64 	};
65 
66 	struct output_chan_info {
67 		int		channel_type;
68 		float	channel_gain;
69 		int		source_count;
70 		float	source_gain[MAX_SOURCE_ENTRIES];
71 		int		source_type[MAX_SOURCE_ENTRIES];
72 	};
73 
74 	MixerCore 			*fCore;
75 	media_output 		fOutput;
76 
77 	uint32				fOutputChannelCount;
78 	output_chan_info 	*fOutputChannelInfo; //array
79 	ByteSwap			*fOutputByteSwap;
80 
81 	bool				fMuted;
82 };
83 
84 inline uint32 MixerOutput::GetOutputChannelCount()
85 {
86 	return fOutputChannelCount;
87 }
88 
89 inline float MixerOutput::GetOutputChannelGain(int channel)
90 {
91 	if (channel < 0 || channel >= fOutputChannelCount)
92 		return 1.0;
93 	return fOutputChannelInfo[channel].channel_gain;
94 }
95 
96 inline uint32 MixerOutput::GetOutputChannelSourceCount(int channel)
97 {
98 	if (channel < 0 || channel >= fOutputChannelCount)
99 		return 0;
100 	return fOutputChannelInfo[channel].source_count;
101 }
102 
103 inline void MixerOutput::GetOutputChannelSourceInfoAt(int channel, int source_index, int *source_type, float *source_gain)
104 {
105 	ASSERT(channel >= 0 && channel < fOutputChannelCount);
106 	ASSERT(index >= 0 && source_index < fOutputChannelInfo[channel].source_count);
107 	*source_type = fOutputChannelInfo[channel].source_type[source_index];
108 	*source_gain = fOutputChannelInfo[channel].source_gain[source_index];
109 }
110 
111 inline void MixerOutput::AdjustByteOrder(BBuffer *buffer)
112 {
113 	if (fOutputByteSwap)
114 		fOutputByteSwap->Swap(buffer->Data(), buffer->SizeUsed());
115 }
116 
117 inline bool MixerOutput::IsMuted()
118 {
119 	return fMuted;
120 }
121 
122 #endif
123