source: roaraudio/roard/beep.c @ 3580:fcf489c8762c

Last change on this file since 3580:fcf489c8762c was 3580:fcf489c8762c, checked in by phi, 14 years ago

use volume, use MAX/4 as default volume for beeps

File size: 4.5 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 int i;
100
101 ROAR_DBG("beep_start(client=%i, beep=%p) = ?", client, beep);
102
103 if ( beep->vol  == 0 )
104  beep->vol  = ROAR_BEEP_DEFAULT_VOL;
105
106 if ( beep->time == 0 )
107  beep->time = 512; // 512ms
108
109 if ( beep->freq == 0 )
110  beep->freq = 440;
111
112 if ( beep->type == 0 )
113  beep->type = ROAR_BEEP_TYPE_DEFAULT;
114
115 // x, y, z location '0' is allready centered.
116
117 // TODO: remove the following lions as soon as we support non zero values
118 if ( beep->z != 0 )
119  return -1;
120
121 if ( beep->y != 0 )
122  return -1;
123
124 if ( beep->x != 0 )
125  return -1;
126
127 ROAR_DBG("beep_start(client=%i, beep=%p) = ?", client, beep);
128
129 if ((stream = streams_new()) == -1 )
130  return -1;
131
132 ROAR_DBG("beep_start(client=%i, beep=%p): stream=%i", client, beep, stream);
133
134 if ( client_stream_add(client, stream) == -1 ) {
135  streams_delete(stream);
136  return -1;
137 }
138
139 if ( streams_set_name(stream, "Beep Source") == -1 ) {
140  streams_delete(stream);
141  return -1;
142 }
143
144 if ( streams_get(stream, &ss) == -1 ) {
145  streams_delete(stream);
146  return -1;
147 }
148
149 s = ROAR_STREAM(ss);
150
151 memcpy(&(s->info), g_sa, sizeof(s->info));
152
153 s->info.channels = 1;
154 s->info.bits     = 8;
155
156 for (i = 0; i < s->info.channels; i++) {
157  ss->mixer.mixer[i] = beep->vol;
158  ss->mixer.scale    = ROAR_BEEP_MAX_VOL;
159 }
160
161 if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) {
162  streams_delete(stream);
163  return -1;
164 }
165
166 if ( (buf = beep_fill_buffer(beep, &(s->info))) == NULL ) {
167  streams_delete(stream);
168  return -1;
169 }
170
171 ROAR_DBG("beep_start(client=%i, beep=%p): buf=%p", client, beep, buf);
172
173 beep_init_vio(&(ss->vio), buf);
174
175 if ( streams_set_fh(stream, -2) == -1 ) {
176  streams_delete(stream);
177  return -1;
178 }
179
180 ROAR_DBG("beep_start(client=%i, beep=%p) = %i", client, beep, stream);
181 return stream;
182}
183
184//ll
Note: See TracBrowser for help on using the repository browser.