1 /* Scicos
2 *
3 * Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 * See the file ./license.txt
20 */
21 /*--------------------------------------------------------------------------*/
22 #include <stdio.h>
23 #include <string.h>
24 #include <math.h>
25 #include "scicos_print.h"
26 #include "charEncoding.h"
27 #include "mput.h"
28 #include "localization.h"
29 #include "sci_malloc.h"
30 #include "dynlib_scicos_blocks.h"
31 /*--------------------------------------------------------------------------*/
writeau(int * flag,int * nevprt,double * t,double xd[],double x[],int * nx,double z[],int * nz,double tvec[],int * ntvec,double rpar[],int * nrpar,int ipar[],int * nipar,double * inptr[],int insz[],int * nin,double * outptr[],int outsz[],int * nout)32 SCICOS_BLOCKS_IMPEXP void writeau(int *flag, int *nevprt,
33 double *t, double xd[],
34 double x[], int *nx,
35 double z[], int *nz,
36 double tvec[], int *ntvec,
37 double rpar[], int *nrpar,
38 int ipar[], int *nipar,
39 double *inptr[], int insz[],
40 int *nin, double *outptr[],
41 int outsz[], int *nout)
42 /*
43 ipar[1] = lfil : file name length
44 ipar[2:4] = fmt : numbers type ascii code
45 ipar[5] = n : buffer length in number of records
46 ipar[6] = swap
47 ipar[7:6+lfil] = character codes for file name
48 */
49 {
50 FILE *fd = NULL;
51 int n = 0, k = 0, i = 0, ierr = 0;
52 double *buffer = NULL, *record = NULL;
53 /* long offset;*/
54 int SCALE = 32768;
55 int BIAS = 132;
56 int CLIP = 32635;
57 int OFFSET = 335;
58 double y = 0.;
59 int sig = 0;
60 int e = 0;
61 double f = 0.;
62
63
64 --ipar;
65 --z;
66 fd = (FILE *)(long)z[2];
67 buffer = (z + 3);
68 ierr = 0;
69 /*
70 k : record counter within the buffer
71 */
72
73 if (*flag == 2 && *nevprt > 0)
74 {
75 /* add a new record to the buffer */
76 n = ipar[5];
77 k = (int)z[1];
78 /* copy current record to output
79 printf("%i\n",k);*/
80 record = buffer + (k - 1) * (*nin);
81
82 for (i = 0; i < *nin; i++)
83 {
84 y = *inptr[i];
85 y = SCALE * y;
86 if (y < 0.0)
87 {
88 y = -y;
89 sig = -1;
90 }
91 else
92 {
93 sig = 1;
94 }
95 if (y > CLIP)
96 {
97 y = CLIP;
98 }
99 y = y + BIAS;
100 f = frexp(y, &e);
101 y = 64 * sig - 16 * e - (int) (32 * f) + OFFSET;
102 record[i] = y;
103 }
104 if (k < n)
105 {
106 z[1] = z[1] + 1.0;
107 }
108 else
109 {
110 mput2(fd, ipar[6], buffer, ipar[5] * (*nin), "uc", &ierr);
111 if (ierr != 0)
112 {
113 *flag = -3;
114 return;
115 }
116 z[1] = 1.0;
117
118 }
119
120 }
121 else if (*flag == 4)
122 {
123 wcfopen(fd, "/dev/audio", "wb");
124 if (!fd )
125 {
126 scicos_print(_("Could not open /dev/audio!\n"));
127 *flag = -3;
128 return;
129 }
130 z[2] = (double)(long)fd;
131 z[1] = 1.0;
132 }
133 else if (*flag == 5)
134 {
135 if (z[2] == 0)
136 {
137 return;
138 }
139 k = (int) z[1];
140 if (k > 1) /* flush rest of buffer */
141 {
142 mput2(fd, ipar[6], buffer, (k - 1) * (*nin), "uc", &ierr);
143 if (ierr != 0)
144 {
145 *flag = -3;
146 return;
147 }
148 }
149 fclose(fd);
150 z[2] = 0.0;
151 }
152 return;
153 }
154 /*--------------------------------------------------------------------------*/
155