File: player.cpp

package info (click to toggle)
tempest-for-eliza 1.0.5-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 340 kB
  • sloc: cpp: 420; sh: 349; makefile: 20
file content (330 lines) | stat: -rw-r--r-- 7,572 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
/*
  tempest for eliza code
  Copyright (C) 2001  Erik Thiele

  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <math.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <SDL.h>
#include <string.h>

double carrier;
int resx;
int resy;
int horizontalspan;
double pixelclock;

SDL_Surface *screen;
Uint32 white, black;

class zeit : public timeval {
public:
  zeit() { setzero(); };
  void getcurrenttime() { gettimeofday (this,0); };
  void subtract (const zeit &subme)
    {
      tv_sec-=subme.tv_sec;
      tv_usec-=subme.tv_usec;
      normalize(); 
    };
  void add (const zeit &addme)
    {
      tv_sec+=addme.tv_sec;
      tv_usec+=addme.tv_usec;
      normalize(); 
    };
  void normalize()
    {
      while (tv_usec<0) {
	tv_usec+=1000000;
	tv_sec--;
      };
      while (tv_usec>=1000000) {
	tv_usec-=1000000;
	tv_sec++;
      };
    };
  bool zero()
    {
      return (tv_sec==0)&&(tv_usec==0);
    };
  void setzero()
    {
      tv_sec=tv_usec=0;
    };
  void print()
    {
      printf("%8d:%8d\n",int(tv_sec),int(tv_usec));
    };
  void setdouble(const double val)
    {
      long long i=(long long)(val*1000000);
      tv_usec=i%1000000;
      tv_sec=i/1000000;
      normalize();
    };
};

inline void pixelchen (const int x, const int y, const Uint32 pixel)
{
  Uint8 *bits=((Uint8*)screen->pixels)+y*screen->pitch+x*screen->format->BytesPerPixel;
  switch(screen->format->BytesPerPixel) {
  case 1:
    *((Uint8 *)(bits)) = (Uint8)pixel;
    break;
  case 2:
    *((Uint16 *)(bits)) = (Uint16)pixel;
    break;
  case 3:
    { /* Format/endian independent */
      Uint8 r, g, b;
      
      r = (pixel>>screen->format->Rshift)&0xFF;
      g = (pixel>>screen->format->Gshift)&0xFF;
      b = (pixel>>screen->format->Bshift)&0xFF;
      *((bits)+screen->format->Rshift/8) = r; 
      *((bits)+screen->format->Gshift/8) = g;
      *((bits)+screen->format->Bshift/8) = b;
    }
    break;
  case 4:
    *((Uint32 *)(bits)) = (Uint32)pixel;
    break;
  };
};

void waituntil (const zeit &desttime)
{
  zeit tm(desttime);
  zeit cur;
  cur.getcurrenttime();
  tm.subtract(cur);

  select(1,0,0,0,&tm);
};

void mkrealsound(double freq)
{
  SDL_Rect myrect;
  myrect.x=myrect.y=0;
  myrect.w=resx;
  myrect.h=resy;
  SDL_FillRect(screen,&myrect,black);
  
  int x, y;
  double t;
  
  double ftfp2 = freq / pixelclock * 2.0;
  double fcfp2 = carrier / pixelclock * 2.0;
  
  t = 0;
  for (y = 0; y < resy; y++) {
    if (int(t*ftfp2)%2) {
      // 1
      t+=resx;
    }else{
      // 0
      for (x = 0; x < resx; x++) {
	if (int(t*fcfp2)%2)
	  pixelchen(x,y,white);
	t++;
      };
    };
    
    t += horizontalspan - resx;
  }
}

double note (int nr)
{
  return pow(2.0,nr/12.0)*440.0*2;
};


int curoct, curnot;
double curlen;
bool loaded;
zeit timefornextsound;

void resetplay()
{
  curoct=0;
  curnot=0;
  curlen=128;
  loaded=false;
  timefornextsound.setzero();
};

void finishplay ()
{
  SDL_Rect myrect;
  myrect.x=myrect.y=0;
  myrect.w=resx;
  myrect.h=resy;
  SDL_FillRect(screen,&myrect,black);

  waituntil(timefornextsound);
  SDL_UpdateRect(screen,0,0,resx,resy);
};

void flushplay ()
{
  if (loaded) {
    if (curnot==-1000) {
      SDL_Rect myrect;
      myrect.x=myrect.y=0;
      myrect.w=resx;
      myrect.h=resy;
      SDL_FillRect(screen,&myrect,black);

      waituntil(timefornextsound);
    }else{
      mkrealsound(note(curnot+12*curoct));
    };
    if (timefornextsound.zero()) {
      timefornextsound.getcurrenttime();
    }else{
      waituntil(timefornextsound);
    };
    SDL_UpdateRect(screen,0,0,resx,resy);

    zeit length;
    length.setdouble(curlen/40.0);
    timefornextsound.add(length);
    loaded=false;
  };
};

void play (const char *const song)
{
  int pos=0;
  while (pos<int(strlen(song))) {
    SDL_Event event;
    while(SDL_PollEvent(&event)) if (event.type == SDL_MOUSEBUTTONDOWN) exit(0);
    switch(song[pos]) {
    case 'c': flushplay(); curnot=0; loaded=true; break;
    case 'd': flushplay(); curnot=2; loaded=true; break;
    case 'e': flushplay(); curnot=4; loaded=true; break;
    case 'f': flushplay(); curnot=5; loaded=true; break;
    case 'g': flushplay(); curnot=7; loaded=true; break;
    case 'a': flushplay(); curnot=9; loaded=true; break;
    case 'b': flushplay(); curnot=11; loaded=true; break;
    case 'p': flushplay(); curnot=-1000; loaded=true; break;
    case '#': curnot++; break;
    case '1': flushplay(); curlen=128; break;
    case '2': flushplay(); curlen=64; break;
    case '4': flushplay(); curlen=32; break;
    case '5': flushplay(); curlen=48; break;
    case '8': flushplay(); curlen=16; break;
    case '6': flushplay(); curlen=8; break;
    case 'u': flushplay(); curoct=-2; break;
    case 'l': flushplay(); curoct=-1; break;
    case 'h': flushplay(); curoct=0; break;
    case 'x': flushplay(); curoct=1; break;
    default:break;
    };
    pos++;
  };
  flushplay();
};

void usage()
{
  printf("\nwrong parameters ! read readme file!\n\n");
  exit(1);
};

int main(int argc, char *argv[])
{
  carrier=10000000.0;
  resx=1024;
  resy=768;
  horizontalspan=1400;
  pixelclock=105.0 * 1e6;
  char *filename;

  atexit(SDL_Quit);

  printf(
	 "\n"
	 "Tempest for Eliza - by erikyyy !\n"
	 "--------------------------------\n"
	 "\n"
	 "Read the README file to understand what's happening\n"
	 "if you do not read it, you will NOT know what to do\n"
	 );

  if (argc!=7) usage();
  pixelclock=atof(argv[1]);
  resx=atol(argv[2]);
  resy=atol(argv[3]);
  horizontalspan=atol(argv[4]);
  carrier=atof(argv[5]);
  filename=argv[6];

  printf("\n"
	 "Pixel Clock %.0f Hz\n"
	 "X Resolution %d Pixels\n"
	 "Y Resolution %d Pixels\n"
	 "Horizontal Total %d Pixels\n"
	 "AM Carrier Frequency %.0f Hz\n"
	 "\n\n",
	 pixelclock,resx,resy,horizontalspan,carrier);

  if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    exit(1);
  };
  
  /* Have a preference for 8-bit, but accept any depth */
  screen = SDL_SetVideoMode(resx, resy, 8, SDL_SWSURFACE|SDL_ANYFORMAT|SDL_FULLSCREEN);
  if ( screen == NULL ) {
    fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
    exit(1);
  }
  printf("Have Set %d bits-per-pixel mode\n",
	 screen->format->BitsPerPixel);

  SDL_Color col[2];
  col[0].r=col[0].g=col[0].b=0xff; // white
  col[1].r=col[1].g=col[1].b=0x00; // black
  
  SDL_SetColors(screen,col,0,2);
  white = SDL_MapRGB(screen->format,0xff,0xff,0xff);
  black = SDL_MapRGB(screen->format,0x00,0x00,0x00);
  
  char song[100000];
  FILE *input=fopen(filename,"r");
  if (!input) { printf("cannot open file\n"); return 1; };
  resetplay();
  char *ret;
  do {
    ret=fgets(song,100000,input);
    if (ret) {
      for (unsigned i=0;i<strlen(song);i++)
	if (song[i]=='*') song[i]=0;
      play (song);
    }
  } while(ret);
  finishplay();
  fclose(input);

  return 0;
};