BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / add-ons / media / media-add-ons / multi_audio / MultiAudioUtility.cpp
blob51b62587b8d2d73244ff9005d2a4573a0deeb436
1 /*
2 * Copyright 2008, Axel Dörfler, axeld@pinc-software.de.
3 * Copyright (c) 2002-2007, Jerome Duval (jerome.duval@free.fr)
5 * Distributed under the terms of the MIT License.
6 */
9 #include "MultiAudioUtility.h"
11 #include <errno.h>
13 #include <MediaDefs.h>
16 const float kSampleRates[] = {
17 8000.0, 11025.0, 12000.0, 16000.0, 22050.0, 24000.0, 32000.0, 44100.0,
18 48000.0, 64000.0, 88200.0, 96000.0, 176400.0, 192000.0, 384000.0, 1536000.0
20 const uint32 kSampleRateCount = sizeof(kSampleRates) / sizeof(kSampleRates[0]);
23 template<typename T> static status_t
24 call_driver(int device, int32 op, T* data)
26 if (ioctl(device, op, data, sizeof(T)) != 0)
27 return errno;
29 return B_OK;
33 namespace MultiAudio {
36 float
37 convert_to_sample_rate(uint32 rate)
39 for (uint32 i = 0; i < kSampleRateCount; i++) {
40 if (rate & 1)
41 return kSampleRates[i];
43 rate >>= 1;
46 return 0.0;
50 uint32
51 convert_from_sample_rate(float rate)
53 for (uint32 i = 0; i < kSampleRateCount; i++) {
54 if (rate <= kSampleRates[i])
55 return 1 << i;
58 return 0;
62 uint32
63 convert_to_media_format(uint32 format)
65 switch (format) {
66 case B_FMT_FLOAT:
67 return media_raw_audio_format::B_AUDIO_FLOAT;
68 case B_FMT_18BIT:
69 case B_FMT_20BIT:
70 case B_FMT_24BIT:
71 case B_FMT_32BIT:
72 return media_raw_audio_format::B_AUDIO_INT;
73 case B_FMT_16BIT:
74 return media_raw_audio_format::B_AUDIO_SHORT;
75 case B_FMT_8BIT_S:
76 return media_raw_audio_format::B_AUDIO_CHAR;
77 case B_FMT_8BIT_U:
78 return media_raw_audio_format::B_AUDIO_UCHAR;
80 default:
81 return 0;
86 int16
87 convert_to_valid_bits(uint32 format)
89 switch (format) {
90 case B_FMT_18BIT:
91 return 18;
92 case B_FMT_20BIT:
93 return 20;
94 case B_FMT_24BIT:
95 return 24;
96 case B_FMT_32BIT:
97 return 32;
99 default:
100 return 0;
105 uint32
106 convert_from_media_format(uint32 format)
108 switch (format) {
109 case media_raw_audio_format::B_AUDIO_FLOAT:
110 return B_FMT_FLOAT;
111 case media_raw_audio_format::B_AUDIO_INT:
112 return B_FMT_32BIT;
113 case media_raw_audio_format::B_AUDIO_SHORT:
114 return B_FMT_16BIT;
115 case media_raw_audio_format::B_AUDIO_CHAR:
116 return B_FMT_8BIT_S;
117 case media_raw_audio_format::B_AUDIO_UCHAR:
118 return B_FMT_8BIT_U;
120 default:
121 return 0;
126 uint32
127 select_sample_rate(uint32 rate)
129 // highest rate
130 uint32 crate = B_SR_1536000;
131 while (crate != 0) {
132 if (rate & crate)
133 return crate;
134 crate >>= 1;
136 return 0;
140 uint32
141 select_format(uint32 format)
143 // best format
144 // TODO ensure we support this format
145 /*if (format & B_FMT_FLOAT)
146 return B_FMT_FLOAT;*/
147 if (format & B_FMT_32BIT)
148 return B_FMT_32BIT;
149 if (format & B_FMT_24BIT)
150 return B_FMT_24BIT;
151 if (format & B_FMT_20BIT)
152 return B_FMT_20BIT;
153 if (format & B_FMT_18BIT)
154 return B_FMT_18BIT;
155 if (format & B_FMT_16BIT)
156 return B_FMT_16BIT;
157 if (format & B_FMT_8BIT_S)
158 return B_FMT_8BIT_S;
159 if (format & B_FMT_8BIT_U)
160 return B_FMT_8BIT_U;
162 return 0;
166 status_t
167 get_description(int device, multi_description* description)
169 return call_driver(device, B_MULTI_GET_DESCRIPTION, description);
173 status_t
174 get_enabled_channels(int device, multi_channel_enable* enable)
176 return call_driver(device, B_MULTI_GET_ENABLED_CHANNELS, enable);
180 status_t
181 set_enabled_channels(int device, multi_channel_enable* enable)
183 return call_driver(device, B_MULTI_SET_ENABLED_CHANNELS, enable);
187 status_t
188 get_global_format(int device, multi_format_info* info)
190 return call_driver(device, B_MULTI_GET_GLOBAL_FORMAT, info);
194 status_t
195 set_global_format(int device, multi_format_info* info)
197 return call_driver(device, B_MULTI_SET_GLOBAL_FORMAT, info);
201 status_t
202 get_buffers(int device, multi_buffer_list* list)
204 return call_driver(device, B_MULTI_GET_BUFFERS, list);
208 status_t
209 buffer_exchange(int device, multi_buffer_info* info)
211 return call_driver(device, B_MULTI_BUFFER_EXCHANGE, info);
215 status_t
216 list_mix_controls(int device, multi_mix_control_info* info)
218 return call_driver(device, B_MULTI_LIST_MIX_CONTROLS, info);
222 status_t
223 get_mix(int device, multi_mix_value_info* info)
225 return call_driver(device, B_MULTI_GET_MIX, info);
229 status_t
230 set_mix(int device, multi_mix_value_info* info)
232 return call_driver(device, B_MULTI_SET_MIX, info);
236 } // namespace MultiAudio