root/branches/libffado-2.0/src/libcontrol/ClockSelect.cpp

Revision 1385, 6.7 kB (checked in by ppalmers, 12 years ago)

Implement a mechanism to disable the samplerate and clock source controls while the device is streaming in order to avoid changes that could mess up jack. The saffire pro controls that cause a device reset to
happen are also disabled while streaming is active.

Line 
1 /*
2  * Copyright (C) 2005-2008 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) version 3 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "ClockSelect.h"
25 #include "ffadodevice.h"
26
27 namespace Control {
28
29 //// --- ClockSelect --- ////
30 ClockSelect::ClockSelect(FFADODevice &d)
31 : AttributeEnum(&d)
32 , m_Device( d )
33 {
34     setName("ClockSelect");
35     setLabel("Clock Source");
36     setDescription("Select the device clock source");
37 }
38
39 bool
40 ClockSelect::select(int idx)
41 {
42     debugOutput(DEBUG_LEVEL_VERBOSE, "Selecting clock idx: %d\n", idx);
43     FFADODevice::ClockSourceVector v = m_Device.getSupportedClockSources();
44     if(idx >= (int)v.size()) {
45         debugError("index out of range\n");
46         return false;
47     }
48     if(idx < 0) {
49         debugError("index < 0\n");
50         return false;
51     }
52     if(!m_Device.setActiveClockSource(v.at(idx))) {
53         debugWarning("could not set active clocksource\n");
54         return false;
55     }
56     debugOutput(DEBUG_LEVEL_VERBOSE, " clock id: %d\n", v.at(idx).id);
57     return true;
58 }
59
60 int
61 ClockSelect::selected()
62 {
63     debugOutput(DEBUG_LEVEL_VERBOSE, "Finding active clock\n");
64     FFADODevice::ClockSourceVector v = m_Device.getSupportedClockSources();
65     FFADODevice::ClockSource active = m_Device.getActiveClockSource();
66     int i=0;
67     for (i=0; i < (int)v.size(); i++) {
68         FFADODevice::ClockSource c = v.at(i);
69         if(c.id == active.id) {
70             debugOutput(DEBUG_LEVEL_VERBOSE, " Active clock at %d, id %d\n", i, c.id);
71             return i;
72         }
73     }
74     debugError("No active clock source found!\n");
75     return -1;
76 }
77
78 int
79 ClockSelect::count()
80 {
81     return m_Device.getSupportedClockSources().size();
82 }
83
84 std::string
85 ClockSelect::getEnumLabel(int idx)
86 {
87     FFADODevice::ClockSourceVector v = m_Device.getSupportedClockSources();
88     if(idx >= (int)v.size()) {
89         debugError("index out of range\n");
90         return "Error";
91     }
92     if(idx < 0) {
93         debugError("index < 0\n");
94         return "Error";
95     }
96     return v.at(idx).description;
97 }
98
99 int
100 ClockSelect::attributeCount()
101 {
102 /*
103         /// indicates the type of the clock source (e.g. eCT_ADAT)
104         enum eClockSourceType type;
105         /// indicated the id of the clock source (e.g. id=1 => clocksource is ADAT_1)
106         unsigned int id;
107         /// is the clock source valid (i.e. can be selected) at this moment?
108         bool valid;
109         /// is the clock source active at this moment?
110         bool active;
111         /// is the clock source locked?
112         bool locked;
113         /// is the clock source slipping?
114         bool slipping;
115         /// description of the clock struct (optional)
116         std::string description;
117 */
118     return 7;
119 }
120
121 std::string
122 ClockSelect::getAttributeValue(int attridx)
123 {
124     char tmp[16];
125     std::string retval = "bad attr index";
126     FFADODevice::ClockSource active = m_Device.getActiveClockSource();
127    
128     switch(attridx) {
129         case 0:
130             retval = FFADODevice::ClockSourceTypeToString(active.type);
131             break;
132         case 1:
133             snprintf(tmp, 16, "%u", active.id);
134             retval = tmp;
135             break;
136         case 2:
137             snprintf(tmp, 16, "%u", active.valid);
138             retval = tmp;
139             break;
140         case 3:
141             snprintf(tmp, 16, "%u", active.active);
142             retval = tmp;
143             break;
144         case 4:
145             snprintf(tmp, 16, "%u", active.locked);
146             retval = tmp;
147             break;
148         case 5:
149             snprintf(tmp, 16, "%u", active.slipping);
150             retval = tmp;
151             break;
152         case 6:
153             retval = active.description;
154     }
155     return retval;
156 }
157
158 std::string
159 ClockSelect::getAttributeName(int attridx)
160 {
161     switch(attridx) {
162         case 0: return "type";
163         case 1: return "id";
164         case 2: return "valid";
165         case 3: return "active";
166         case 4: return "locked";
167         case 5: return "slipping";
168         case 6: return "description";
169         default: return "bad attr index";
170     }
171 }
172
173 bool
174 ClockSelect::canChangeValue()
175 {
176     return m_Device.getStreamingState() == FFADODevice::eSS_Idle;
177 }
178
179 void
180 ClockSelect::show()
181 {
182     debugOutput( DEBUG_LEVEL_NORMAL, "ClockSelect Element %s, active: %s\n",
183         getName().c_str(), m_Device.getActiveClockSource().description.c_str());
184 }
185
186 // --- samplerate selection ---
187
188 SamplerateSelect::SamplerateSelect(FFADODevice &d)
189 : Enum(&d)
190 , m_Device( d )
191 {
192     setName("SamplerateSelect");
193     setLabel("Samplerate Select");
194     setDescription("Select the device sample rate");
195 }
196
197 bool
198 SamplerateSelect::select(int idx)
199 {
200     std::vector<int> freqs = m_Device.getSupportedSamplingFrequencies();
201     if (idx >= 0 && idx < (int)freqs.size()) {
202         if(!m_Device.setSamplingFrequency(freqs.at(idx))) {
203             debugWarning("Could not select samplerate\n");
204             return false;
205         }
206         return true;
207     } else {
208         debugWarning("bad index specified\n");
209         return false;
210     }
211 }
212
213 int
214 SamplerateSelect::selected()
215 {
216     std::vector<int> freqs = m_Device.getSupportedSamplingFrequencies();
217     int samplerate = m_Device.getSamplingFrequency();
218     for (int i = 0; i < (int)freqs.size(); i++) {
219         if (samplerate == freqs.at(i)) {
220             return i;
221         }
222     }
223     debugError("could not find the selected samplerate\n");
224     return -1;
225 }
226
227 int
228 SamplerateSelect::count()
229 {
230     return  m_Device.getSupportedSamplingFrequencies().size();
231 }
232
233 std::string
234 SamplerateSelect::getEnumLabel(int idx)
235 {
236     char tmp[16];
237     std::string retval = "Error";
238     std::vector<int> freqs = m_Device.getSupportedSamplingFrequencies();
239     if (idx >= 0 && idx < (int)freqs.size()) {
240         snprintf(tmp, 16, "%u", freqs.at(idx));
241         retval = tmp;
242     } else {
243         debugWarning("bad index specified\n");
244     }
245     return retval;
246 }
247
248 bool
249 SamplerateSelect::canChangeValue()
250 {
251     return m_Device.getStreamingState() == FFADODevice::eSS_Idle;
252 }
253
254 void
255 SamplerateSelect::show()
256 {
257     debugOutput( DEBUG_LEVEL_NORMAL, "SamplerateSelect Element %s, current: %d\n",
258         getName().c_str(), m_Device.getSamplingFrequency());
259
260 }
261
262
263 } // namespace Control
Note: See TracBrowser for help on using the browser.