1 /*
2      File: MatrixMixerVolumes.cpp
3  Abstract: MatrixMixerVolumes.h
4   Version: 1.1
5 
6  Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
7  Inc. ("Apple") in consideration of your agreement to the following
8  terms, and your use, installation, modification or redistribution of
9  this Apple software constitutes acceptance of these terms.  If you do
10  not agree with these terms, please do not use, install, modify or
11  redistribute this Apple software.
12 
13  In consideration of your agreement to abide by the following terms, and
14  subject to these terms, Apple grants you a personal, non-exclusive
15  license, under Apple's copyrights in this original Apple software (the
16  "Apple Software"), to use, reproduce, modify and redistribute the Apple
17  Software, with or without modifications, in source and/or binary forms;
18  provided that if you redistribute the Apple Software in its entirety and
19  without modifications, you must retain this notice and the following
20  text and disclaimers in all such redistributions of the Apple Software.
21  Neither the name, trademarks, service marks or logos of Apple Inc. may
22  be used to endorse or promote products derived from the Apple Software
23  without specific prior written permission from Apple.  Except as
24  expressly stated in this notice, no other rights or licenses, express or
25  implied, are granted by Apple herein, including but not limited to any
26  patent rights that may be infringed by your derivative works or by other
27  works in which the Apple Software may be incorporated.
28 
29  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
30  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34 
35  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42  POSSIBILITY OF SUCH DAMAGE.
43 
44  Copyright (C) 2014 Apple Inc. All Rights Reserved.
45 
46 */
47 #include "MatrixMixerVolumes.h"
48 #include "CAXException.h"
49 
50 OSStatus	NumberChannels (AudioUnit 		 	au,
51 							AudioUnitScope		inScope,
52 							AudioUnitElement	inEl,
53 							UInt32				&outChans);
54 
55 
PrintBuses(FILE * file,const char * str,AudioUnit au,AudioUnitScope inScope)56 OSStatus PrintBuses (FILE* file, const char* str, AudioUnit au, AudioUnitScope inScope)
57 {
58 	OSStatus result;
59 	UInt32 busCount;
60 	UInt32 theSize = sizeof(busCount);
61 
62 	ca_require_noerr (result = AudioUnitGetProperty (au, kAudioUnitProperty_ElementCount,
63 							inScope, 0, &busCount, &theSize), home);
64 
65 	fprintf (file, "\t%s Elements:\n\t\t", str);
66 	for (UInt32 i = 0; i < busCount; ++i) {
67 		Float32 val;
68 		ca_require_noerr (result = AudioUnitGetParameter (au, kMatrixMixerParam_Enable, inScope, i, &val), home);
69 		UInt32 numChans;
70 		ca_require_noerr (result = NumberChannels (au, inScope, i, numChans), home);
71 		char frameCharStart = (val != 0 ? '[' : '{');
72 		char frameCharEnd = (val != 0 ? ']' : '}');
73 		fprintf (file, "%d:%c%d, %c%c  ", (int)i, frameCharStart, (int)numChans, (val != 0 ? 'T' : 'F'), frameCharEnd);
74 	}
75 	fprintf (file, "\n");
76 home:
77 	return result;
78 }
79 
PrintMatrixMixerVolumes(FILE * file,AudioUnit au)80 void	PrintMatrixMixerVolumes (FILE* file, AudioUnit au)
81 {
82 	UInt32 dims[2];
83 	UInt32 theSize =  sizeof(UInt32) * 2;
84 	Float32 *theVols = NULL;
85 	OSStatus result;
86 
87 // this call will fail if the unit is NOT initialized as it would present an incomplete state
88 	ca_require_noerr (result = AudioUnitGetProperty (au, kAudioUnitProperty_MatrixDimensions,
89 							kAudioUnitScope_Global, 0, dims, &theSize), home);
90 
91 	theSize = ((dims[0] + 1) * (dims[1] + 1)) * sizeof(Float32);
92 
93 	theVols	= static_cast<Float32*> (malloc (theSize));
94 
95 	ca_require_noerr (result = AudioUnitGetProperty (au, kAudioUnitProperty_MatrixLevels,
96 							kAudioUnitScope_Global, 0, theVols, &theSize), home);
97 
98 home:
99 	if (result) {
100 		if (theVols)
101 			free(theVols);
102 		return;
103 	}
104 
105 	theSize /= sizeof(Float32);
106 
107 	unsigned int inputs = dims[0];
108 	unsigned int outputs = dims[1];
109 
110 	fprintf (file, "\tInput Channels = %d, Output Channels = %d\n", (int)dims[0], (int)dims[1]);
111 	PrintBuses (file, "Input", au, kAudioUnitScope_Input);
112 	PrintBuses (file, "Output", au, kAudioUnitScope_Output);
113 	fprintf (file, "\tGlobal Volume: %.3f\n", theVols [theSize - 1]);
114 	for (unsigned int i = 0; i < (inputs + 1); ++i) {
115 		if (i < inputs) {
116 			fprintf (file, "\t%.3f   ", theVols[(i + 1) * (outputs + 1) - 1]);
117 
118 			for (unsigned int j = 0; j < outputs; ++j)
119 				fprintf (file, "(%.3f) ", theVols[(i * (outputs  + 1)) + j]);
120 		} else {
121 			fprintf (file, "\t        ");
122 			for (unsigned int j = 0; j < outputs; ++j)
123 				fprintf (file, " %.3f  ", theVols[(i * (outputs + 1)) + j]);
124 		}
125 		fprintf (file, "\n");
126 	}
127 
128 #if 0
129 	for (unsigned int i = 0; i < theSize; ++i)
130 		printf ("%f, ", theVols[i]);
131 #endif
132 	free(theVols);
133 }
134 
135 // Utility routine that gets the number of channels from an audio unit
NumberChannels(AudioUnit au,AudioUnitScope inScope,AudioUnitElement inEl,UInt32 & outChans)136 OSStatus	NumberChannels (AudioUnit 		 	au,
137 							AudioUnitScope		inScope,
138 							AudioUnitElement	inEl,
139 							UInt32				&outChans)
140 {
141 	AudioStreamBasicDescription desc;
142 	UInt32 dataSize = sizeof (AudioStreamBasicDescription);
143 	OSStatus result = AudioUnitGetProperty (au, kAudioUnitProperty_StreamFormat,
144 								inScope, inEl,
145 								&desc, &dataSize);
146 	if (!result)
147 		outChans = desc.mChannelsPerFrame;
148 	return result;
149 }
150