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 |
---|
28 | int driver_sysclock_open_vio(struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh, struct roar_stream_server * sstream) { |
---|
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 | self->last_wanted = 0; |
---|
75 | |
---|
76 | return 0; |
---|
77 | } |
---|
78 | |
---|
79 | int driver_sysclock_close (struct roar_vio_calls * vio) { |
---|
80 | struct driver_sysclock * self = vio->inst; |
---|
81 | |
---|
82 | if ( self == NULL ) |
---|
83 | return -1; |
---|
84 | |
---|
85 | free(self); |
---|
86 | |
---|
87 | return 0; |
---|
88 | } |
---|
89 | |
---|
90 | ssize_t driver_sysclock_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
91 | struct driver_sysclock * self = vio->inst; |
---|
92 | struct timeval now; |
---|
93 | long long diff = (1000000 * count / self->bps); |
---|
94 | long long ago; |
---|
95 | |
---|
96 | gettimeofday(&now, NULL); |
---|
97 | |
---|
98 | ago = now.tv_usec - self->lasttime.tv_usec; |
---|
99 | ago += 1000000*(now.tv_sec - self->lasttime.tv_sec); |
---|
100 | |
---|
101 | ago -= self->last_wanted; |
---|
102 | |
---|
103 | memcpy(&(self->lasttime), &now, sizeof(now)); |
---|
104 | |
---|
105 | ROAR_DBG("driver_sysclock_write(*): count=%u, bps=%u, diff=%llu, ago=%llu", count, self->bps, diff, ago); |
---|
106 | |
---|
107 | diff -= ago; |
---|
108 | |
---|
109 | self->last_wanted = diff; |
---|
110 | |
---|
111 | if ( diff > 0 ) |
---|
112 | usleep(diff); |
---|
113 | |
---|
114 | return count; |
---|
115 | } |
---|
116 | |
---|
117 | #endif |
---|
118 | |
---|
119 | //ll |
---|