File: main.cc

package info (click to toggle)
synaesthesia 2.4-5.1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,484 kB
  • ctags: 538
  • sloc: ansic: 7,090; sh: 2,915; cpp: 1,963; perl: 95; makefile: 49
file content (438 lines) | stat: -rw-r--r-- 11,264 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* Synaesthesia - program to display sound graphically
   Copyright (C) 1997  Paul Francis Harrison

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  675 Mass Ave, Cambridge, MA 02139, USA.

  The author may be contacted at:
    pfh@yoyo.cc.monash.edu.au
  or
    27 Bond St., Mt. Waverley, 3149, Melbourne, Australia
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <pwd.h>
#include <string.h>
#include "syna.h"

//void setupIcons();

/* Config and globals =============================================== */

BaseScreen *screen;

sampleType *data;

int outWidth, outHeight;
Bitmap<unsigned short> outputBmp, lastOutputBmp, lastLastOutputBmp;

PolygonEngine<unsigned short,Combiner,2> polygonEngine;

void allocOutput(int w,int h) {
  /*delete[] output;
  delete[] lastOutput;
  delete[] lastLastOutput;
  output = new unsigned char[w*h*2];
  lastOutput = new unsigned char[w*h*2];
  lastLastOutput = new unsigned char[w*h*2];
  memset(output,32,w*h*2);
  memset(lastOutput,32,w*h*2);
  outWidth = w;
  outHeight = h;*/

  outputBmp.size(w,h);
  lastOutputBmp.size(w,h);
  lastLastOutputBmp.size(w,h);
  polygonEngine.size(w,h);
  outWidth = w;
  outHeight = h;
}

SymbolID state = NoCD;
int track = 1, frames = 0;
double trackProgress = 0.0;

char **playList;
int playListLength, playListPosition;

SymbolID fadeMode;
bool pointsAreDiamonds;

double brightnessTwiddler; 
double starSize; 
double volume = 0.5;

double fgRedSlider, fgGreenSlider, bgRedSlider, bgGreenSlider;

static SoundSource soundSource;

static int windX, windY, windWidth, windHeight;
static char dspName[80];
static char mixerName[80];
static char cdromName[80];

void setStateToDefaults() {
  fadeMode = Stars;
  pointsAreDiamonds = true;

  brightnessTwiddler = 0.3;
  starSize = 0.5; 

  fgRedSlider=0.0;
  fgGreenSlider=0.5;
  bgRedSlider=0.75;
  bgGreenSlider=0.4;
}

bool loadConfig() {
  setStateToDefaults();
  windX=10;
  windY=30;
  windWidth = DefaultWidth;
  windHeight = DefaultHeight;
  if (!access("/dev/sound/dsp", F_OK))
	  strcpy(dspName, "/dev/sound/dsp");
  else
	  strcpy(dspName, "/dev/dsp");
  if (!access("/dev/sound/mixer", F_OK))
	  strcpy(mixerName, "/dev/sound/mixer");
  else
	  strcpy(mixerName, "/dev/mixer");
  strcpy(cdromName, "/dev/cdrom");

  //Should i free this? Manual is unclear
  struct passwd *passWord = getpwuid(getuid());
  if (passWord == 0) return false;

  char *fileName = new char[strlen(passWord->pw_dir) + 20];
  strcpy(fileName,passWord->pw_dir);
  strcat(fileName,"/.synaesthesia");
  FILE *f = fopen(fileName,"rt");
  delete fileName;

  if (f) {
    char line[80];
    while(0 != fgets(line,80,f)) {
      sscanf(line,"x %d",&windX);
      sscanf(line,"y %d",&windY);
      sscanf(line,"width %d",&windWidth);
      sscanf(line,"height %d",&windHeight);
      sscanf(line,"brightness %lf",&brightnessTwiddler);
      sscanf(line,"pointsize %lf",&starSize);
      sscanf(line,"fgred %lf",&fgRedSlider);
      sscanf(line,"fggreen %lf",&fgGreenSlider);
      sscanf(line,"bgred %lf",&bgRedSlider);
      sscanf(line,"bggreen %lf",&bgGreenSlider);
      sscanf(line,"dsp %s",dspName);
      sscanf(line,"mixer %s",mixerName);
      sscanf(line,"cdrom %s",cdromName);
      if (strncmp(line,"fade",4) == 0) fadeMode = Stars;
      if (strncmp(line,"wave",4) == 0) fadeMode = Wave;
      if (strncmp(line,"heat",4) == 0) fadeMode = Flame;
      if (strncmp(line,"stars",5) == 0) pointsAreDiamonds = false;
      if (strncmp(line,"diamonds",8) == 0) pointsAreDiamonds = true;
    }
    fclose(f);
  
    if (windWidth < 1)
      windWidth = DefaultWidth;
    if (windHeight < 1)
      windHeight = DefaultHeight;
    windWidth  &= ~3;

#   define bound(v) \
      if (v < 0.0) v = 0.0; \
      if (v > 1.0) v = 1.0;

    bound(brightnessTwiddler)
    bound(starSize)
    bound(fgRedSlider)
    bound(fgGreenSlider)
    bound(bgRedSlider)
    bound(bgGreenSlider)

#   undef bound
  
    return true;
  } else
    return false;

}

void saveConfig() {
  //Should i free this? Manual is unclear
  struct passwd *passWord = getpwuid(getuid());
  if (passWord == 0) {
    fprintf(stderr,"Couldn't work out where to put config file.\n");
    return;
  }
  
  char *fileName = new char[strlen(passWord->pw_dir) + 20];
  strcpy(fileName,passWord->pw_dir);
  strcat(fileName,"/.synaesthesia");
  FILE *f = fopen(fileName,"wt");
  delete fileName;

  if (f) {
    fprintf(f,"# Synaesthesia config file\n");
    fprintf(f,"x %d\n",windX);
    fprintf(f,"y %d\n",windY);
    fprintf(f,"width %d\n",outWidth);
    fprintf(f,"height %d\n",outHeight);

    fprintf(f,"# Point style: either diamonds or stars\n");
    fprintf(f,"%s\n",pointsAreDiamonds ? "diamonds" : "stars");
    
    fprintf(f,"# Fade style: either fade, wave or heat\n");
    fprintf(f,"%s\n",fadeMode==Stars ? "fade" : (fadeMode==Wave ? "wave" : "heat"));
    
    fprintf(f,"brightness %f\n",brightnessTwiddler);
    fprintf(f,"pointsize %f\n",starSize);
    fprintf(f,"fgred %f\n",fgRedSlider);
    fprintf(f,"fggreen %f\n",fgGreenSlider);
    fprintf(f,"bgred %f\n",bgRedSlider);
    fprintf(f,"bggreen %f\n",bgGreenSlider);
    fprintf(f,"dsp %s\n",dspName);
    fprintf(f,"mixer %s\n",mixerName);
    fprintf(f,"cdrom %s\n",cdromName);
    fclose(f);
  } else {
    fprintf(stderr,"Couldn't open config file to save settings.\n");
  }
}

void chomp(int &argc,char **argv,int argNum) {
  argc--;
  for(int i=argNum;i<argc;i++)
    argv[i] = argv[i+1];
}

int main(int argc, char **argv) { 
  if (!loadConfig())
    saveConfig();

  if (argc == 1) {
    printf("SYNAESTHESIA " VERSION "\n\n"
           "Usage:\n"
           "  synaesthesia cd\n    - listen to a CD\n"
           "  synaesthesia line\n    - listen to line input\n"

#if HAVE_LIBESD
           "  synaesthesia esd\n    - listen to EsounD output (eg for use with XMMS)\n"
#endif
	   
           "  synaesthesia <track> <track> <track>...\n"
	   "    - play these CD tracks one after the other\n"
           "  <another program> |synaesthesia pipe <frequency>\n"
	   "    - send output of program to sound card as well as displaying it.\n"
	   "      (must be 16-bit stereo sound)\n"
	   "    example: mpg123 -s file.mp3 |synaesthesia pipe 44100\n\n"
	   "The following optional flags may be used\n"

#if HAVE_SDL
	   "     --use-sdl      force use of Simple DirectMedia Layer\n"
#endif
#ifndef X_DISPLAY_MISSING 
	   "     --use-x        force use of X-Windows\n"
#endif
#if HAVE_LIBVGA
	   "     --use-svga     force use of SVGALib\n"
#endif
	   "     --fullscreen   try to take over the whole screen\n"
	   "     --width nnn    make the window this wide\n"
	   "     --height nnn   make the window this high\n\n"
           "For more information, see http://logarithmic.net/pfh/Synaesthesia\n\n"
	   "Enjoy!\n"
	   );
    return 1;
  }

  //Do flags
  bool fullscreen = false, useSDL = true, useX = true, useSVGA = true;
  for(int i=0;i<argc;)
    if (strcmp(argv[i],"--use-sdl") == 0) {
      useX = false;
      useSVGA = false;
      chomp(argc,argv,i);
    } else if (strcmp(argv[i],"--use-x") == 0) {
      useSDL = false;
      useSVGA = false;
      chomp(argc,argv,i);
    } else if (strcmp(argv[i],"--use-svga") == 0) {
      useSDL = false;
      useX = false;
      chomp(argc,argv,i);
    } else if (strcmp(argv[i],"--fullscreen") == 0) {
      fullscreen = true;
      chomp(argc,argv,i);
    } else if (strcmp(argv[i],"--width") == 0) {
      chomp(argc,argv,i);
      windWidth = atoi(argv[i]);
      if (windWidth < 1)
        windWidth = DefaultWidth;
      windWidth  &= ~3;
      chomp(argc,argv,i);
    } else if (strcmp(argv[i],"--height") == 0) {
      chomp(argc,argv,i);
      windHeight = atoi(argv[i]);
      if (windHeight < 1)
        windHeight = DefaultHeight;
      chomp(argc,argv,i);
    } else
      i++;

  int configPlayTrack = -1;
  int inFrequency = Frequency;

  playListLength = 0;
  
  if (strcmp(argv[1],"line") == 0) soundSource = SourceLine;
  else if (strcmp(argv[1],"cd") == 0) soundSource = SourceCD;

#if HAVE_LIBESD
  else if (strcmp(argv[1],"esd") == 0) soundSource = SourceESD;
#endif
  
  else if (strcmp(argv[1],"pipe") == 0) {
    if (argc < 3 || sscanf(argv[2],"%d",&inFrequency) != 1)
      error("frequency not specified");
    soundSource = SourcePipe;
  } else
    if (sscanf(argv[1],"%d",&configPlayTrack) != 1)
      error("comprehending user's bizzare requests");
    else {
      soundSource = SourceCD;
      playList = argv+1;
      playListPosition = 0;
      playListLength = argc-1;
    }

  if (soundSource == SourceCD)
    cdOpen(cdromName);
  else
    state = Plug;

  if (configPlayTrack != -1) {
    cdStop();
  }

  openSound(soundSource, inFrequency, dspName, mixerName); 
  
  setupMixer(volume);

  //if (volume > 0.0) {
  //  brightnessTwiddler /= volume;
  //}
  //if (brightnessTwiddler > 1.0)
  //  brightnessTwiddler = 1.0;
  //else if (brightnessTwiddler < 0.0)
  //  brightnessTwiddler = 0.0;

  //if (volume > 1.0)
  //  volume = 1.0;
  //else if (volume < 0.0)
  //  volume = 0.0;

  screen = 0;

#if HAVE_SDL
  if (!screen && useSDL) {
    screen = new SdlScreen;
    if (!screen->init(windX,windY,windWidth,windHeight,fullscreen))
      screen = 0;
  }
#endif

#ifndef X_DISPLAY_MISSING
  if (!screen && useX) {
    screen = new XScreen;
    if (!screen->init(windX,windY,windWidth,windHeight,fullscreen))
      screen = 0;
  }
#endif

#if HAVE_LIBVGA
  if (!screen && useSVGA) {
    screen = new SvgaScreen;
    if (!screen->init(windX,windY,windWidth,windHeight,fullscreen))
      screen = 0;
  }
#endif

  if (!screen)
    error("opening any kind of display device");

  allocOutput(outWidth,outHeight);

  coreInit();

  setStarSize(starSize);

  interfaceInit();

  time_t timer = time(NULL);
  
  int frames = 0;
  for(;;) {
    fade();

    if (-1 == coreGo())
      break;

    if (interfaceGo()) break;
    
    screen->show(); 

    frames++;
  } 

  timer = time(NULL) - timer;

  interfaceEnd();
  
//  if (configPlayTrack != -1)
//    cdStop();    
  
  if (soundSource == SourceCD) 
    cdClose();

  closeSound();

  screen->end();
  delete screen;

  if (timer > 10)
    printf("Frames per second: %f\n", double(frames) / timer);
  
  return 0;
}

void error(char *str, bool syscall) { 
  fprintf(stderr, "synaesthesia: Error %s\n",str); 
  if (syscall)
    fprintf(stderr,"(reason for error: %s)\n",strerror(errno));
  exit(1);
}
void warning(char *str, bool syscall) { 
  fprintf(stderr, "synaesthesia: Possible error %s\n",str); 
  if (syscall)
    fprintf(stderr,"(reason for error: %s)\n",strerror(errno));
}