source: roaraudio/roard/beep.c @ 4521:4277b6a0c8a1

Last change on this file since 4521:4277b6a0c8a1 was 4521:4277b6a0c8a1, checked in by phi, 14 years ago

fixed a lot compiler warnings

File size: 5.1 KB
Line 
1//beep.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28// TODO: FIXME: move them out of here into libroar:
29static ssize_t beep_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
30 struct roar_buffer * rbuf;
31
32 ROAR_DBG("beep_read(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned int)count);
33
34 if ( vio->inst == NULL )
35  return 0;
36
37 rbuf = vio->inst;
38
39 if ( roar_buffer_shift_out(&rbuf, buf, &count) == -1 )
40  return -1;
41
42 vio->inst = rbuf;
43
44 return count;
45}
46
47static int     beep_close   (struct roar_vio_calls * vio) {
48 ROAR_DBG("beep_close(vio=%p) = ?", vio);
49
50 if ( vio->inst != NULL )
51  roar_buffer_free(vio->inst);
52
53 ROAR_DBG("beep_close(vio=%p) = 0", vio);
54 return 0;
55}
56
57static void beep_init_vio (struct roar_vio_calls * vio, void * inst) {
58 ROAR_DBG("beep_init_vio(vio=%p, inst=%p) = ?", vio, inst);
59
60 memset(vio, 0, sizeof(struct roar_vio_calls));
61
62 vio->inst  = inst;
63 vio->read  = beep_read;
64 vio->close = beep_close;
65
66 ROAR_DBG("beep_init_vio(vio=%p, inst=%p) = (void)", vio, inst);
67}
68
69struct roar_buffer * beep_fill_buffer (struct roar_beep * beep, struct roar_audio_info * info) {
70 struct roar_buffer          * buf;
71 size_t frames  = beep->time * info->rate     / 1000;
72 size_t samples = frames     * info->channels;
73 size_t mod     = info->rate / beep->freq;
74 char                        * data;
75 void                        * bufdata;
76 char   val;
77 size_t pos;
78 size_t chan;
79
80 ROAR_DBG("beep_fill_buffer(beep=%p, info=%p) = ?", beep, info);
81
82 if ( roar_buffer_new_data(&buf, samples, &bufdata) == -1 ) {
83  ROAR_DBG("beep_fill_buffer(beep=%p, info=%p) = NULL", beep, info);
84  return NULL;
85 }
86
87 data = (char*)bufdata;
88
89 for (pos = 0; pos < frames; pos++) {
90  val = (pos % mod) < mod/2 ? -128 : 127;
91  for (chan = 0; chan < info->channels; chan++)
92   data[pos*info->channels + chan] = val;
93 }
94
95 ROAR_DBG("beep_fill_buffer(beep=%p, info=%p) = %p", beep, info, buf);
96 return buf;
97}
98
99int beep_start (int client, struct roar_beep * beep) {
100 struct roar_stream_server *  ss;
101 struct roar_stream        *   s;
102 struct roar_buffer        * buf;
103 int stream;
104
105 ROAR_DBG("beep_start(client=%i, beep=%p) = ?", client, beep);
106
107 if ( beep->vol  == 0 )
108  beep->vol  = ROAR_BEEP_DEFAULT_VOL;
109
110 if ( beep->time == 0 )
111  beep->time = ROAR_BEEP_DEFAULT_TIME;
112
113 if ( beep->freq == 0 )
114  beep->freq = ROAR_BEEP_DEFAULT_FREQ;
115
116 if ( beep->type == 0 )
117  beep->type = ROAR_BEEP_DEFAULT_TYPE;
118
119 // x, y, z location '0' is allready centered.
120
121 // TODO: remove the following lions as soon as we support non zero values
122 if ( beep->z != 0 )
123  return -1;
124
125 if ( beep->y != 0 )
126  return -1;
127
128 ROAR_DBG("beep_start(client=%i, beep=%p) = ?", client, beep);
129
130 if ((stream = streams_new()) == -1 )
131  return -1;
132
133 ROAR_DBG("beep_start(client=%i, beep=%p): stream=%i", client, beep, stream);
134
135 if ( client_stream_add(client, stream) == -1 ) {
136  streams_delete(stream);
137  return -1;
138 }
139
140 if ( streams_set_name(stream, "Beep Source") == -1 ) {
141  streams_delete(stream);
142  return -1;
143 }
144
145 if ( streams_get(stream, &ss) == -1 ) {
146  streams_delete(stream);
147  return -1;
148 }
149
150 s = ROAR_STREAM(ss);
151
152 memcpy(&(s->info), g_sa, sizeof(s->info));
153
154 s->info.channels = 2;
155 s->info.bits     = 8;
156
157 ss->mixer.mixer[0] = beep->x > 0 ?
158                        ((long)beep->vol * ((long)ROAR_BEEP_MAX_POS - (long)beep->x)/(long)ROAR_BEEP_MAX_POS) :
159                        beep->vol;
160 ss->mixer.mixer[1] = beep->x < 0 ?
161                        ((long)beep->vol * ((long)ROAR_BEEP_MAX_POS + (long)beep->x)/(long)ROAR_BEEP_MAX_POS) :
162                        beep->vol;
163 ss->mixer.scale    = ROAR_BEEP_MAX_VOL;
164
165 ROAR_DBG("beep_start(client=%i, beep=%p): beep->x=%i, ss->mixer.mixer[] = {%u, %u}", client, beep, beep->x, ss->mixer.mixer[0], ss->mixer.mixer[1]);
166
167 if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) {
168  streams_delete(stream);
169  return -1;
170 }
171
172 if ( streams_set_role(stream, ROAR_ROLE_BEEP) == -1 ) {
173  streams_delete(stream);
174  return -1;
175 }
176
177 if ( (buf = beep_fill_buffer(beep, &(s->info))) == NULL ) {
178  streams_delete(stream);
179  return -1;
180 }
181
182 ROAR_DBG("beep_start(client=%i, beep=%p): buf=%p", client, beep, buf);
183
184 beep_init_vio(&(ss->vio), buf);
185
186 if ( streams_set_fh(stream, -2) == -1 ) {
187  streams_delete(stream);
188  return -1;
189 }
190
191 ROAR_DBG("beep_start(client=%i, beep=%p) = %i", client, beep, stream);
192 return stream;
193}
194
195//ll
Note: See TracBrowser for help on using the repository browser.