source: roaraudio/roard/beep.c @ 6052:d48765b2475e

Last change on this file since 6052:d48765b2475e was 6052:d48765b2475e, checked in by phi, 9 years ago

updated copyright headers

File size: 5.2 KB
Line 
1//beep.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2015
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 vio->flags = ROAR_VIO_FLAGS_NONE;
64 vio->refc  = 1;
65 vio->inst  = inst;
66 vio->read  = beep_read;
67 vio->close = beep_close;
68
69 ROAR_DBG("beep_init_vio(vio=%p, inst=%p) = (void)", vio, inst);
70}
71
72struct roar_buffer * beep_fill_buffer (struct roar_beep * beep, struct roar_audio_info * info) {
73 struct roar_buffer          * buf;
74 size_t frames  = beep->time * info->rate     / 1000;
75 size_t samples = frames     * info->channels;
76 size_t mod     = info->rate / beep->freq;
77 char                        * data;
78 void                        * bufdata;
79 char   val;
80 size_t pos;
81 size_t chan;
82
83 ROAR_DBG("beep_fill_buffer(beep=%p, info=%p) = ?", beep, info);
84
85 if ( roar_buffer_new_data(&buf, samples, &bufdata) == -1 ) {
86  ROAR_DBG("beep_fill_buffer(beep=%p, info=%p) = NULL", beep, info);
87  return NULL;
88 }
89
90 data = (char*)bufdata;
91
92 for (pos = 0; pos < frames; pos++) {
93  val = (pos % mod) < mod/2 ? -128 : 127;
94  for (chan = 0; chan < info->channels; chan++)
95   data[pos*info->channels + chan] = val;
96 }
97
98 ROAR_DBG("beep_fill_buffer(beep=%p, info=%p) = %p", beep, info, buf);
99 return buf;
100}
101
102int beep_start (int client, struct roar_beep * beep) {
103 struct roar_stream_server *  ss;
104 struct roar_stream        *   s;
105 struct roar_buffer        * buf;
106 int stream;
107
108 ROAR_DBG("beep_start(client=%i, beep=%p) = ?", client, beep);
109
110 if ( beep->vol  == 0 )
111  beep->vol  = ROAR_BEEP_DEFAULT_VOL;
112
113 if ( beep->time == 0 )
114  beep->time = ROAR_BEEP_DEFAULT_TIME;
115
116 if ( beep->freq == 0 )
117  beep->freq = ROAR_BEEP_DEFAULT_FREQ;
118
119 if ( beep->type == 0 )
120  beep->type = ROAR_BEEP_DEFAULT_TYPE;
121
122 // x, y, z location '0' is allready centered.
123
124 // TODO: remove the following lions as soon as we support non zero values
125 if ( beep->z != 0 )
126  return -1;
127
128 if ( beep->y != 0 )
129  return -1;
130
131 ROAR_DBG("beep_start(client=%i, beep=%p) = ?", client, beep);
132
133 if ((stream = streams_new()) == -1 )
134  return -1;
135
136 ROAR_DBG("beep_start(client=%i, beep=%p): stream=%i", client, beep, stream);
137
138 if ( client_stream_add(client, stream) == -1 ) {
139  streams_delete(stream);
140  return -1;
141 }
142
143 if ( streams_set_name(stream, "Beep Source") == -1 ) {
144  streams_delete(stream);
145  return -1;
146 }
147
148 if ( streams_get(stream, &ss) == -1 ) {
149  streams_delete(stream);
150  return -1;
151 }
152
153 s = ROAR_STREAM(ss);
154
155 memcpy(&(s->info), g_sa, sizeof(s->info));
156
157 s->info.channels = 2;
158 s->info.bits     = 8;
159
160 ss->mixer.mixer[0] = 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.mixer[1] = beep->x < 0 ?
164                        ((long)beep->vol * ((long)ROAR_BEEP_MAX_POS + (long)beep->x)/(long)ROAR_BEEP_MAX_POS) :
165                        beep->vol;
166 ss->mixer.scale    = ROAR_BEEP_MAX_VOL;
167
168 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]);
169
170 if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) {
171  streams_delete(stream);
172  return -1;
173 }
174
175 if ( streams_set_role(stream, ROAR_ROLE_BEEP) == -1 ) {
176  streams_delete(stream);
177  return -1;
178 }
179
180 if ( (buf = beep_fill_buffer(beep, &(s->info))) == NULL ) {
181  streams_delete(stream);
182  return -1;
183 }
184
185 ROAR_DBG("beep_start(client=%i, beep=%p): buf=%p", client, beep, buf);
186
187 beep_init_vio(&(ss->vio), buf);
188
189 if ( streams_set_fh(stream, -2) == -1 ) {
190  streams_delete(stream);
191  return -1;
192 }
193
194 ROAR_DBG("beep_start(client=%i, beep=%p) = %i", client, beep, stream);
195 return stream;
196}
197
198//ll
Note: See TracBrowser for help on using the repository browser.