3 #define VERSION "0.3.1.0"
11 #include <linux/soundcard.h>
12 #include <sys/ioctl.h>
16 typedef enum { false = 0, true = 1 } bool;
22 signed short int left
;
23 signed short int right
;
27 /* output device stuff */
30 bool output_stop
= true;
32 #define wavrate ((double)44100)
33 #define pi ((double)3.141596)
35 #define bufsize (8000)
36 longtrick buffer
[bufsize
];
37 static double barkbounds
[barksize
+1] =
39 0,100,200,300,400,510,630,770,920,1080,1270,1480,1720,2000,2380,2700,
40 3150,3700,4400,5300,6400,7700,9500,12000,15500
43 static double perfectspectrum
[barksize
] =
69 static double perfect_amp(double freq
)
72 for(i
= 0 ; i
< barksize
; i
++)
73 if (freq
>=barkbounds
[i
] && freq
< barkbounds
[i
+1])
75 double left
= barkbounds
[i
];
76 double right
= barkbounds
[i
+1];
77 double center
= (left
+right
)/2.0;
82 if (i
>0) y1
= perfectspectrum
[i
-1];
83 y2
= perfectspectrum
[i
];
87 y1
= perfectspectrum
[i
];
88 if (i
<barksize
) y1
= perfectspectrum
[i
+1];
90 double answer
= y1
+(freq
-left
)*(y2
-y1
)/(center
-left
);
91 answer
= exp10(answer
/10.0);
92 printf("freq %g will be played at volume %g\n",freq
,answer
);
98 static double *lfo_frequencies
[barksize
];
99 static double *lfo_phases
[barksize
];
100 static double *lfo_amps
[barksize
];
101 static double *std_frequencies
[barksize
];
102 static double *std_phases
[barksize
];
103 static double std_amps
[barksize
];
104 static double scaling_factor
= 2.0*pi
/wavrate
;
106 const char* n_comments
= "";
107 const char* n_playrate
= "44100";
108 const char* n_lfo_shaping
= "";
109 const char* n_freq_shaping
= "";
110 const char* n_brainwave_freq
= "100";
111 const char* n_output_filename
= "cryosleep-generation.raw";
112 const char* n_dsp_filename
= "/dev/dsp";
113 static bool n_independent_stereo_channels
= false;
114 static double n_freq_lo
;
115 static double n_freq_hi
;
116 static int n_traces
= 1;
117 static bool n_use_dsp
= false;
118 static bool n_stereo
= false;
119 static bool n_perfect
= false;
121 bool __atob(const char* v
)
125 if (strcmp(v
,"true")==0)
127 if (strcmp(v
,"yes")==0)
133 void handle_value(const char* name
, const char* value
)
135 if ((!name
)||(!strlen(name
)))
138 if (strcmp(name
,"lfo-shaping")==0)
139 n_lfo_shaping
= strdup(value
);
140 else if (strcmp(name
, "traces")==0)
141 n_traces
= atoi(value
);
142 else if (strcmp(name
,"frequency-shaping")==0)
143 n_freq_shaping
= strdup(value
);
144 else if (strcmp(name
,"frequency-low")==0)
145 n_freq_lo
= atof(value
);
146 else if (strcmp(name
,"frequency-high")==0)
147 n_freq_hi
= atof(value
);
148 else if (strcmp(name
,"dsp-filename")==0)
149 n_dsp_filename
= strdup(value
);
150 else if (strcmp(name
,"output-filename")==0)
151 n_output_filename
= strdup(value
);
152 else if (strcmp(name
,"comments")==0)
153 n_comments
= strdup(value
);
154 else if (strcmp(name
,"brainwave-frequency")==0)
155 n_brainwave_freq
= strdup(value
);
156 else if (strcmp(name
,"playrate")==0)
157 n_playrate
= strdup(value
);
158 else if (strcmp(name
,"use-dsp")==0)
159 n_use_dsp
= __atob(value
);
160 else if (strcmp(name
,"stereo")==0)
161 n_stereo
= __atob(value
);
162 else if (strcmp(name
,"independent-stereo")==0)
163 n_independent_stereo_channels
= __atob(value
);
165 printf("%s=\"%s\"\n", name
, value
);
168 void read_line(char* buffer
)
175 for(c
=buffer
; *c
; c
++)
179 // now skip leading spaces
180 while ((*buffer
==' ')||(*buffer
=='\n')||(*buffer
=='\r')||(*buffer
=='\t'))
184 char* colon
= strchr(buffer
,':');
189 fprintf(stderr
,"Maybe broken line: \"%s\"\n", buffer
);
194 /* skip off follwing spaces in variable name */
197 while ((backptr
>buffer
)&&((*backptr
==' ')||(*backptr
=='\t')||(*backptr
=='\n')||(*backptr
=='\r')))
201 /* now get the field name */
204 /* jump to value and skip spaces there */
206 while ((*buffer
==' ')||(*buffer
=='\n')||(*buffer
=='\r')||(*buffer
=='\t'))
211 backptr
=buffer
+strlen(buffer
)-1;
212 while ((backptr
>buffer
)&&((*backptr
==' ')||(*backptr
=='\t')||(*backptr
=='\n')||(*backptr
=='\r')))
221 handle_value(fieldname
,value
);
224 void read_cf(const char* fn
)
228 FILE* fp
= fopen(fn
,"r");
231 if (fgets(buffer
, sizeof(buffer
)-1, fp
))
236 int dsp_open(const char* device
)
239 output_fd
=open(device
,O_WRONLY
);
242 fprintf(stderr
,"Could not open dsp device \"%s\"\n",device
);
246 // set dsp parameters
248 if (ioctl(output_fd
,SNDCTL_DSP_SETFMT
,&p
)==-1)
250 fprintf(stderr
,"dsp: setting dsp to standard 16 bit failed\n");
254 if (ioctl(output_fd
,SNDCTL_DSP_CHANNELS
,&p
)==-1)
256 fprintf(stderr
,"dsp: setting dsp to 2 channels failed\n");
260 if (ioctl(output_fd
,SNDCTL_DSP_SPEED
,&p
)==-1)
262 printf("dsp: setting dsp speed (%d) failed\n",p
++);
263 if (ioctl(output_fd
,SNDCTL_DSP_SPEED
,&p
)==-1)
265 printf("dsp: setting dsp speed (%d) failed\n",p
);
267 if (ioctl(output_fd
,SNDCTL_DSP_SPEED
,&p
)==-1)
269 printf("dsp: setting dsp speed (%d) failed\n",p
);
277 #define choose(lo,hi) ((double)(random()%10000)*(hi-lo)/10000.0+lo)
278 #define mean(lo,hi) ((lo+hi)/2)
280 void file_open(const char * name
)
282 output_fd
= open(name
,O_CREAT
|O_WRONLY
|O_TRUNC
,S_IRUSR
|S_IWUSR
);
283 assert(output_fd
!=-1);
288 assert(output_fd
== -1);
290 dsp_open(n_dsp_filename
);
292 file_open(n_output_filename
);
297 double pos
= 0.0, maximum
= 0.0, phase
, freq
;
298 int i
, j
, k
= 0, track
;
306 double brainwave
= 0;
308 brainwave
= atof(n_brainwave_freq
);
310 double playrate
= playrate
= atof(n_playrate
);
313 double lfo_shaping
= atof(n_lfo_shaping
);
314 double freq_shaping
= atof(n_freq_shaping
);
316 for ( track
= 0 ; track
< barksize
; track
++)
318 lfo_frequencies
[track
] = (double*)malloc(sizeof(double)*n_traces
);
319 lfo_phases
[track
] = (double*)malloc(sizeof(double)*n_traces
);
320 lfo_amps
[track
] = (double*)malloc(sizeof(double)*n_traces
);
321 std_frequencies
[track
] = (double*)malloc(sizeof(double)*n_traces
);
322 std_phases
[track
] = (double*)malloc(sizeof(double)*n_traces
);
325 for ( track
= 0 ; track
< barksize
; track
++ )
326 for ( i
= 0 ; i
< n_traces
; i
++ )
328 double down
= barkbounds
[ track
] ;
329 double up
= barkbounds
[ track
+1 ] ;
330 freq
= choose ( n_freq_lo
,n_freq_hi
) ;
331 lfo_phases
[ track
] [ i
] = choose ( 0,2.0*pi
) ;
332 lfo_frequencies
[ track
] [ i
] = freq
*scaling_factor
;
333 lfo_amps
[ track
] [ i
] = sqrt ( pow ( freq
, - lfo_shaping
) ) ;
334 std_phases
[ track
] [ i
] = choose ( 0,2.0*pi
) ;
336 std_frequencies
[ track
] [ i
] = choose ( down
,up
) *scaling_factor
;
338 std_amps
[ track
] = perfect_amp(mean ( down
,up
));
340 std_amps
[ track
] = sqrt ( pow ( mean ( down
,up
) , - freq_shaping
) ) ;
344 while ( ! output_stop
)
346 for ( j
= 0 ; j
< bufsize
; j
++ )
348 register double left
;
349 register double right
;
350 register double * phases
;
351 register double * freqs
;
352 register double * amps
;
353 if (n_independent_stereo_channels
)
355 register double volume_left
;
356 register double volume_right
;
358 for ( track
= left
= right
= 0 ; track
< barksize
; track
++ )
360 phases
= lfo_phases
[ track
] ;
361 freqs
= lfo_frequencies
[ track
] ;
362 amps
= lfo_amps
[ track
] ;
365 for( i
= 0; i
< n_traces
/2 ; i
++ )
367 volume_left
+= sin ( phases
[ i
] + freqs
[ i
] * pos
) * amps
[ i
] ;
369 for( i
= n_traces
/2; i
< n_traces
; i
++ )
371 volume_right
+= sin ( phases
[ i
] + freqs
[ i
] * pos
) * amps
[ i
] ;
374 volume_left
= 1.0 - abs(volume_left
);
375 volume_right
= 1.0 - abs(volume_right
);
377 phases
= std_phases
[ track
] ;
378 freqs
= std_frequencies
[ track
] ;
379 volume_left
*= std_amps
[ track
] ;
380 volume_right
*= std_amps
[ track
] ;
381 for(i
= 0 ; i
< n_traces
; i
++)
383 phase
= phases
[ i
] ;
385 left
+= volume_left
* sin ( phase
+ pos
* freq
) ;
386 freq
+= brainwave
* scaling_factor
;
387 right
+= volume_right
* sin ( phase
+ pos
* freq
) ;
394 register double volume
;
395 for ( track
= left
= right
= 0 ; track
< barksize
; track
++ )
397 phases
= lfo_phases
[ track
] ;
398 freqs
= lfo_frequencies
[ track
] ;
399 amps
= lfo_amps
[ track
] ;
400 for( i
= volume
= 0 ; i
< n_traces
; i
++ )
402 volume
+= sin ( phases
[ i
] + freqs
[ i
] * pos
) * amps
[ i
] ;
404 phases
= std_phases
[ track
] ;
405 freqs
= std_frequencies
[ track
] ;
406 volume
*= std_amps
[ track
] ;
407 for(i
= 0 ; i
< n_traces
; i
++)
409 phase
= phases
[ i
] ;
411 left
+= volume
* sin ( phase
+ pos
* freq
) ;
412 freq
+= brainwave
* scaling_factor
;
413 right
+= volume
* sin ( phase
+ pos
* freq
) ;
418 if ( fabs ( left
) > maximum
)
419 maximum
= fabs ( left
) ;
420 if ( fabs ( right
) > maximum
)
421 maximum
= fabs ( right
) ;
422 left
*= 32767.0 / maximum
;
423 right
*= 32767.0 / maximum
;
424 buffer
[ j
] . leftright
. left
= (signed short)left
;
425 buffer
[ j
] . leftright
. right
= (signed short)right
;
427 double total
= (++k
)*bufsize
;
428 total
/=(double)playrate
;
429 sprintf(yeah
,"%g seconds processed (maximum = %g)" ,total
, maximum
);
430 printf("%s\n", yeah
);
431 long written
= write(output_fd
,buffer
,4*bufsize
);
432 assert(written
== 4*bufsize
);
440 int main(int argc
, char* argv
[])
446 fprintf(stderr
,"cryosleep <config>\n");
450 printf("Cryosleep v" VERSION
" (c) Werner Van Belle, Enrico Weigelt <weigelt@metux.de> 2003, 2007, 2008\n");