source: roaraudio/roard/driver_sysclock.c @ 2213:7a2733bb4ae8

Last change on this file since 2213:7a2733bb4ae8 was 2213:7a2733bb4ae8, checked in by phi, 15 years ago

got it working!

File size: 2.7 KB
Line 
1//driver_raw.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio 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 software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "roard.h"
26
27#ifdef ROAR_HAVE_DRIVER_SYSCLOCK
28int driver_sysclock_open_vio(struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh) {
29 struct driver_sysclock * self = malloc(sizeof(struct driver_sysclock));
30
31 if ( self == NULL )
32  return -1;
33
34 if ( device != NULL ) {
35  return -1;
36 }
37
38 memset(self, 0, sizeof(struct driver_sysclock));
39 memset(inst, 0, sizeof(struct roar_vio_calls));
40
41 inst->inst  = self;
42 inst->close = driver_sysclock_close;
43 inst->write = driver_sysclock_write;
44
45 self->bps   = (info->bits / 8) * info->channels * info->rate;
46
47 if (!self->bps) {
48  free(self);
49  return -1;
50 }
51
52 switch (info->codec) {
53  case ROAR_CODEC_PCM_S_LE:
54  case ROAR_CODEC_PCM_S_BE:
55  case ROAR_CODEC_PCM_S_PDP:
56  case ROAR_CODEC_PCM_U_LE:
57  case ROAR_CODEC_PCM_U_BE:
58  case ROAR_CODEC_PCM_U_PDP:
59   break;
60  case ROAR_CODEC_ALAW:
61  case ROAR_CODEC_MULAW:
62    // one byte per sample
63    self->bps *= 8;
64    self->bps /= info->bits;
65   break;
66  default:
67    free(self);
68    return -1;
69   break;
70 }
71
72 gettimeofday(&(self->lasttime), NULL);
73
74 return 0;
75}
76
77int     driver_sysclock_close   (struct roar_vio_calls * vio) {
78 struct driver_sysclock * self = vio->inst;
79
80 if ( self == NULL )
81  return -1;
82
83 free(self);
84
85 return 0;
86}
87
88ssize_t driver_sysclock_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
89 struct driver_sysclock * self = vio->inst;
90 struct timeval now;
91 unsigned long long diff = (1000000 * count / self->bps);
92 unsigned long long ago;
93
94 gettimeofday(&now, NULL);
95
96 ago  = now.tv_usec - self->lasttime.tv_usec;
97 ago += 1000000*(now.tv_sec - self->lasttime.tv_sec);
98
99 memcpy(&(self->lasttime), &now, sizeof(now));
100
101 ROAR_DBG("driver_sysclock_write(*): count=%u, bps=%u, diff=%llu, ago=%llu", count, self->bps, diff, ago);
102
103 if ( diff <= ago )
104  return count;
105
106 diff -= ago;
107
108 usleep(diff);
109
110 return count;
111}
112
113#endif
114
115//ll
Note: See TracBrowser for help on using the repository browser.