source: roaraudio/roard/beep.c @ 3764:2a2b1eb426c4

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

added new buffer function roar_buffer_new_data() to make common alloc case more easy

File size: 5.0 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_data(&buf, samples, &data) == -1 ) {
76  ROAR_DBG("beep_fill_buffer(beep=%p, info=%p) = NULL", beep, info);
77  return NULL;
78 }
79
80 for (pos = 0; pos < frames; pos++) {
81  val = (pos % mod) < mod/2 ? -128 : 127;
82  for (chan = 0; chan < info->channels; chan++)
83   data[pos*info->channels + chan] = val;
84 }
85
86 ROAR_DBG("beep_fill_buffer(beep=%p, info=%p) = %p", beep, info, buf);
87 return buf;
88}
89
90int beep_start (int client, struct roar_beep * beep) {
91 struct roar_stream_server *  ss;
92 struct roar_stream        *   s;
93 struct roar_buffer        * buf;
94 int stream;
95
96 ROAR_DBG("beep_start(client=%i, beep=%p) = ?", client, beep);
97
98 if ( beep->vol  == 0 )
99  beep->vol  = ROAR_BEEP_DEFAULT_VOL;
100
101 if ( beep->time == 0 )
102  beep->time = ROAR_BEEP_DEFAULT_TIME;
103
104 if ( beep->freq == 0 )
105  beep->freq = ROAR_BEEP_DEFAULT_FREQ;
106
107 if ( beep->type == 0 )
108  beep->type = ROAR_BEEP_DEFAULT_TYPE;
109
110 // x, y, z location '0' is allready centered.
111
112 // TODO: remove the following lions as soon as we support non zero values
113 if ( beep->z != 0 )
114  return -1;
115
116 if ( beep->y != 0 )
117  return -1;
118
119 ROAR_DBG("beep_start(client=%i, beep=%p) = ?", client, beep);
120
121 if ((stream = streams_new()) == -1 )
122  return -1;
123
124 ROAR_DBG("beep_start(client=%i, beep=%p): stream=%i", client, beep, stream);
125
126 if ( client_stream_add(client, stream) == -1 ) {
127  streams_delete(stream);
128  return -1;
129 }
130
131 if ( streams_set_name(stream, "Beep Source") == -1 ) {
132  streams_delete(stream);
133  return -1;
134 }
135
136 if ( streams_get(stream, &ss) == -1 ) {
137  streams_delete(stream);
138  return -1;
139 }
140
141 s = ROAR_STREAM(ss);
142
143 memcpy(&(s->info), g_sa, sizeof(s->info));
144
145 s->info.channels = 2;
146 s->info.bits     = 8;
147
148 ss->mixer.mixer[0] = beep->x > 0 ?
149                        ((long)beep->vol * ((long)ROAR_BEEP_MAX_POS - (long)beep->x)/(long)ROAR_BEEP_MAX_POS) :
150                        beep->vol;
151 ss->mixer.mixer[1] = beep->x < 0 ?
152                        ((long)beep->vol * ((long)ROAR_BEEP_MAX_POS + (long)beep->x)/(long)ROAR_BEEP_MAX_POS) :
153                        beep->vol;
154 ss->mixer.scale    = ROAR_BEEP_MAX_VOL;
155
156 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]);
157
158 if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) {
159  streams_delete(stream);
160  return -1;
161 }
162
163 if ( streams_set_role(stream, ROAR_ROLE_BEEP) == -1 ) {
164  streams_delete(stream);
165  return -1;
166 }
167
168 if ( (buf = beep_fill_buffer(beep, &(s->info))) == NULL ) {
169  streams_delete(stream);
170  return -1;
171 }
172
173 ROAR_DBG("beep_start(client=%i, beep=%p): buf=%p", client, beep, buf);
174
175 beep_init_vio(&(ss->vio), buf);
176
177 if ( streams_set_fh(stream, -2) == -1 ) {
178  streams_delete(stream);
179  return -1;
180 }
181
182 ROAR_DBG("beep_start(client=%i, beep=%p) = %i", client, beep, stream);
183 return stream;
184}
185
186//ll
Note: See TracBrowser for help on using the repository browser.