source: roaraudio/roard/beep.c @ 5381:430b1d26e12d

Last change on this file since 5381:430b1d26e12d was 5381:430b1d26e12d, checked in by phi, 12 years ago

updated copyright years

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