summaryrefslogtreecommitdiff
path: root/muse2/muse/driver/alsatimer.cpp
blob: cf211a2f4db4b88c28762f621337930de50f7ec0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//=========================================================
//  MusE
//  Linux Music Editor
//  $Id: alsatimer.cpp,v 1.1.2.9 2009/03/28 01:46:10 terminator356 Exp $
//
//  Plenty of code borrowed from timer.c example in 
//  alsalib 1.0.7
//
//  (C) Copyright 2004 Robert Jonsson (rj@spamatica.se)
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; version 2 of
//  the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//=========================================================
        
#include "alsatimer.h"
#include <climits>

#define TIMER_DEBUG 0

namespace MusECore {
  
  AlsaTimer::AlsaTimer()
     {
     if(TIMER_DEBUG)
       fprintf(stderr,"AlsaTimer::AlsaTimer(this=%p) called\n",this);
     handle = NULL;
     id = NULL;
     info = NULL;
     params = NULL;
     findBest = true;
     }
     
  AlsaTimer::~AlsaTimer()
    {
    if(TIMER_DEBUG)
       fprintf(stderr,"AlsaTimer::~AlsaTimer(this=%p) called\n",this);
    if (handle)
      snd_timer_close(handle);
    if (id) snd_timer_id_free(id);
    if (info) snd_timer_info_free(info);
    if (params) snd_timer_params_free(params);
    }
  
  signed int AlsaTimer::initTimer()
    {
    if(TIMER_DEBUG)
      printf("AlsaTimer::initTimer(this=%p)\n",this);
  
    int err;
    int devclass = SND_TIMER_CLASS_GLOBAL;
    int sclass = SND_TIMER_CLASS_NONE;
    int card = 0;
    int device = SND_TIMER_GLOBAL_SYSTEM;
    int subdevice = 0;
    int test_ids[] = { SND_TIMER_GLOBAL_SYSTEM
                     , SND_TIMER_GLOBAL_RTC
#ifdef SND_TIMER_GLOBAL_HPET
                     , SND_TIMER_GLOBAL_HPET
#endif
                     };
    int max_ids = sizeof(test_ids) / sizeof(int);
    long best_res = LONG_MAX;
    //int best_dev = -1; // SND_TIMER_GLOBAL_SYSTEM;
    int best_dev = SND_TIMER_GLOBAL_SYSTEM;          
    int i;

    if (id || info || params) {
      fprintf(stderr,"AlsaTimer::initTimer(): called on initialised timer!\n");
      return fds->fd;
    }  
    snd_timer_id_malloc(&id);
    snd_timer_info_malloc(&info);
    snd_timer_params_malloc(&params);

    if (findBest) {
      for (i = 0; i < max_ids; ++i) {
        device = test_ids[i];
        sprintf(timername, "hw:CLASS=%i,SCLASS=%i,CARD=%i,DEV=%i,SUBDEV=%i", devclass, sclass, card, device, subdevice);
        if ((err = snd_timer_open(&handle, timername, SND_TIMER_OPEN_NONBLOCK)) < 0) {
          continue;
          }
        if ((err = snd_timer_info(handle, info)) < 0) {
          snd_timer_close(handle);
          continue;
          }
        // select a non slave timer with the lowest resolution value
        int is_slave = snd_timer_info_is_slave(info);
        long res = snd_timer_info_get_resolution(info);
        if ((is_slave == 0) && (best_res > res)) {
          best_res = res;
          best_dev = device;
          }
        snd_timer_close(handle);
        }
      device = best_dev;
      }

    //if(best_dev==-1)
    //  return -1; // no working timer found

    sprintf(timername, "hw:CLASS=%i,SCLASS=%i,CARD=%i,DEV=%i,SUBDEV=%i", devclass, sclass, card, device, subdevice);
    if ((err = snd_timer_open(&handle, timername, SND_TIMER_OPEN_NONBLOCK))<0) {
      fprintf(stderr, "AlsaTimer::initTimer(): timer open %i (%s)\n", err, snd_strerror(err));
      return -1;  
      }
    
    if ((err = snd_timer_info(handle, info)) < 0) {
      fprintf(stderr, "AlsaTimer::initTimer(): timer info %i (%s)\n", err, snd_strerror(err));
      return -1;
      }

    //if(debugMsg)
      fprintf(stderr, "AlsaTimer::initTimer(): best available ALSA timer: %s\n", snd_timer_info_get_name(info));

    snd_timer_params_set_auto_start(params, 1);
    snd_timer_params_set_ticks(params, 1);
      
    if ((err = snd_timer_params(handle, params)) < 0) {
      fprintf(stderr, "AlsaTimer::initTimer(): timer params %i (%s)\n", err, snd_strerror(err));
      return -1;
      }
    
    count = snd_timer_poll_descriptors_count(handle);
    fds = (pollfd *)calloc(count, sizeof(pollfd));
    if (fds == NULL) {
      fprintf(stderr, "AlsaTimer::initTimer(): malloc error\n");
      return -1;
      }
    if ((err = snd_timer_poll_descriptors(handle, fds, count)) < 0) {
      fprintf(stderr, "AlsaTimer::initTimer(): snd_timer_poll_descriptors error: %s\n", snd_strerror(err));
      return -1;
      }
    return fds->fd;
    }
  
  unsigned int AlsaTimer::setTimerResolution(unsigned int resolution)
    {
    if(TIMER_DEBUG)
      printf("AlsaTimer::setTimerResolution(%d)\n",resolution);
    /* Resolution of an AlsaTimer is fixed - it cannot be set */
    return 0;
    }
  
  unsigned int AlsaTimer::setTimerFreq(unsigned int freq)
    {
    signed int err;
    unsigned int setTick, actFreq;
    
    if(TIMER_DEBUG)
      printf("AlsaTimer::setTimerFreq(this=%p)\n",this);

    setTick = (1000000000 / snd_timer_info_get_resolution(info)) / freq;

    if (setTick == 0) {
      // return, print error if freq is below 500 (timing will suffer)
      if (((1000000000.0 / snd_timer_info_get_resolution(info)) / snd_timer_params_get_ticks(params)) < 500) {
        fprintf(stderr,"AlsaTimer::setTimerTicks(): requested freq %u Hz too high for timer (max is %g)\n",
          freq, 1000000000.0 / snd_timer_info_get_resolution(info));
        fprintf(stderr,"  freq stays at %ld Hz\n",
          (long int)((1000000000.0 / snd_timer_info_get_resolution(info)) / snd_timer_params_get_ticks(params)));
      }

      return (long int)((1000000000.0 / snd_timer_info_get_resolution(info)) / snd_timer_params_get_ticks(params));
    }
    actFreq = (1000000000 / snd_timer_info_get_resolution(info)) / setTick;
    if (actFreq != freq) {
      fprintf(stderr,"AlsaTimer::setTimerTicks(): warning: requested %u Hz, actual freq is %u Hz\n",
        freq, actFreq);
    }
    if(TIMER_DEBUG)
      printf("AlsaTimer::setTimerFreq(): Setting ticks (period) to %d ticks\n", setTick);
    snd_timer_params_set_auto_start(params, 1);
    snd_timer_params_set_ticks(params, setTick);
    if ((err = snd_timer_params(handle, params)) < 0) {
      fprintf(stderr, "AlsaTimer::setTimerFreq(): timer params %i (%s)\n", err, snd_strerror(err));
      return 0;
      }

    return actFreq;
    }
  
  unsigned int AlsaTimer::getTimerResolution()
    {
    return  snd_timer_info_get_resolution(info);
    }

  unsigned int AlsaTimer::getTimerFreq()
    {
    return (1000000000 / snd_timer_info_get_resolution(info)) / snd_timer_params_get_ticks(params);
    }
        
  bool AlsaTimer::startTimer()
    {
    if(TIMER_DEBUG)
      printf("AlsaTimer::startTimer(this=%p): handle=%p\n",this,handle);
    int err;
    if ((err = snd_timer_start(handle)) < 0) {
      fprintf(stderr, "AlsaTimer::startTimer(): timer start %i (%s)\n", err, snd_strerror(err));
      return false;
      }
    return true;
    }
  
  bool AlsaTimer::stopTimer()
    {
    int err;
    if(TIMER_DEBUG)
      printf("AlsaTimer::stopTimer(this=%p): handle=%p\n",this,handle);
    if ((err = snd_timer_stop(handle)) < 0) {
      fprintf(stderr, "AlsaTimer::stopTimer(): timer stop %i (%s)\n", err, snd_strerror(err));
      return false;
      }
    return true;
    }
        
  unsigned int  AlsaTimer::getTimerTicks(bool printTicks)
    {
    //if(TIMER_DEBUG)
    //  printf("AlsaTimer::getTimerTicks\n");
    snd_timer_read_t tr;
    tr.ticks = 0;
    while (snd_timer_read(handle, &tr, sizeof(tr)) == sizeof(tr)) {
              if (printTicks) {
                  printf("TIMER: resolution = %uns, ticks = %u\n",
                    tr.resolution, tr.ticks);
                  }
      }
    return tr.ticks;
    }

} // namespace MusECore