File: mp3player.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 (188 lines) | stat: -rw-r--r-- 5,224 bytes parent folder | download
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
/*
  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>
#include <assert.h>

double fT; // Traegerfrequenz des Radiosenders [1/s]
u_int32_t resx; // anzahl sichtbarer pixel in X richtung
u_int32_t resy; // anzahl sichtbarer pixel in Y richtung
u_int32_t horizontalspan; // anzahl virtueller pixel in X richtung
u_int32_t verticalspan; // anzahl virtueller pixel in Y richtung
double fP; // Pixelfrequenz der Grafikkarte [1/s]
int playbackmode;
double amplify;

SDL_Surface *screen;

void error (const char *const msg)
{
  fprintf(stderr, "ERROR: %s\n\n", msg);
  exit(1);
}

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

int main(int argc, char *argv[])
{
  char *filename;

  atexit(SDL_Quit);

  printf(
	 "\n"
	 "Tempest for MP3 - 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!=10) usage();
  fP=atof(argv[1]);
  resx=atol(argv[2]);
  resy=atol(argv[3]);
  horizontalspan=atol(argv[4]);
  verticalspan=atol(argv[5]);
  fT=atof(argv[6]);
  playbackmode=atoi(argv[7]);
  amplify=atof(argv[8]);
  filename=argv[9];

  double npp=horizontalspan * verticalspan; // number of pixels pro screen update periode
  double gZ=npp/fP; // gesamtZeit pro screen update periode
  double cT=gZ*fT; // anzahl Traegerperioden pro update periode
  cT=double(int(cT));
  fT=cT/gZ;

  printf("\n"
	 "Pixel Clock %.5f Hz\n"
	 "X Resolution %d Pixels\n"
	 "Y Resolution %d Pixels\n"
	 "Horizontal Total %d Pixels\n"
	 "Vertical Total %d Pixels\n"
	 "AM Carrier Frequency %.5f Hz\n"
	 "Playbackmode %d\n"
	 "Amplify %.5f\n"
	 "\n\n",
	 fP,resx,resy,horizontalspan,verticalspan,fT,playbackmode,amplify);

  if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    exit(1);
  }
  
  screen = SDL_SetVideoMode(resx, resy, 8, SDL_HWPALETTE | SDL_ANYFORMAT | SDL_FULLSCREEN);
  if ( screen == NULL ) {
    fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
    exit(1);
  }
  if ((screen->flags & SDL_HWPALETTE) == 0) error("i can't get hardware palette support.");
  if ((screen->flags & SDL_FULLSCREEN) == 0) error("cannot set fullscreen mode.");
  if (screen->format->BitsPerPixel != 8) error("cannot set 8bpp mode.");

  SDL_LockSurface (screen);
  u_int8_t *p = (u_int8_t*) screen->pixels;
  u_int32_t pitch=screen->pitch;
  double npcb=npp/double(128); // number of pixels pro screen update periode pro audio sample
  for (u_int32_t y=0;y<resy;++y)
    for (u_int32_t x=0;x<resx;++x) {
      double iP=y*horizontalspan+x; // pixel index
      double tP=iP/fP; // pixel zeit [s]
      if (sin(tP*fT*double(2)*M_PI)>0) 
	p[pitch*y+x]=int(iP/npcb)*2;
      else
	p[pitch*y+x]=int(iP/npcb)*2+1;
    }
  SDL_UnlockSurface(screen);
  SDL_Flip(screen);

  double aF=fP/npcb; // audio frequenz [1/s]
  printf("audio file frequency should be %f Hz\n",aF);

  u_int32_t audiolength;
  u_int8_t *audiobuf;
  {
    FILE *input;
    input = fopen(filename,"rb");
    assert (input != NULL);
    assert (fseek(input,0,SEEK_END) != -1);
    audiolength=ftell(input);
    assert (audiolength>=0);
    assert (fseek(input,0,SEEK_SET) != -1);
    audiobuf = (u_int8_t*) malloc (audiolength);
    assert (audiobuf != 0);
    assert (fread(audiobuf,audiolength,1,input)==1);
    assert (fclose(input)==0);
  }
  printf("audiolength is %d\n",audiolength);

  SDL_Event event;

  SDL_Color colors[256];
  u_int32_t curpos=0;
  for (;;) {
    if(SDL_PollEvent(&event)) if (event.type==SDL_KEYDOWN) exit(0);

    for(int i=0;i<128;i++){
      double value=audiobuf[curpos];
      value-=double(128);
      value*=amplify;
      value+=double(128);
      int ivalue=int(value);
      if (ivalue<0) ivalue=0;
      if (ivalue>255) ivalue=255;
      
      if (playbackmode==0) {
	colors[i*2].r=ivalue;
	colors[i*2].g=ivalue;
	colors[i*2].b=ivalue;
	
	colors[i*2+1].r=0;
	colors[i*2+1].g=0;
	colors[i*2+1].b=0;
      }else if (playbackmode==1) {
	int col = (ivalue * 20) / 45;
	
	colors[i*2].r=col;
	colors[i*2].g=col;
	colors[i*2].b=col;
	
	colors[i*2+1].r=255-col;
	colors[i*2+1].g=255-col;
	colors[i*2+1].b=255-col;
      }
      curpos ++;
      if (curpos>=audiolength) exit(0);
    }
    SDL_SetPalette(screen, SDL_PHYSPAL, colors, 0, 256);
  }

  return 0;
}