source: roaraudio/roard/beep.c @ 3581:2f5833dfb334

Last change on this file since 3581:2f5833dfb334 was 3581:2f5833dfb334, checked in by phi, 14 years ago

support for X possition of events

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