Commit 4b87ee595f7df274d7d56f4d2bdf42a56bc93422

  • avatar
  • Eladith <jasavilh @j…u.fi> (Committer)
  • Sun Apr 25 15:49:25 EEST 2010
  • avatar
  • Eladith <jasavilh @j…u.fi> (Author)
  • Sun Apr 25 15:49:25 EEST 2010
Simple wrapper for SDL pixel handling
sdl_window.cpp
(62 / 0)
  
1#include "sdl_window.h"
2#include <iostream>
3using namespace std;
4
5sdl_window::sdl_window(int w, int h) {
6 screen = SDL_SetVideoMode(w, h, 32, SDL_SWSURFACE);
7 if (!screen) {
8 cerr << "SDL_SetVideoMode() failed" << endl;
9 throw std::exception();
10 }
11}
12
13void sdl_window::drawpixel(int x, int y, float r, float g, float b) {
14 Uint32 pixel = clamp(r, g, b);
15
16 /* Function copied from SDL docs */
17 int bpp = screen->format->BytesPerPixel;
18 Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + x * bpp;
19
20 switch(bpp) {
21 case 1:
22 *p = pixel;
23 break;
24
25 case 2:
26 *(Uint16 *)p = pixel;
27 break;
28
29 case 3:
30 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
31 p[0] = (pixel >> 16) & 0xff;
32 p[1] = (pixel >> 8) & 0xff;
33 p[2] = pixel & 0xff;
34 } else {
35 p[0] = pixel & 0xff;
36 p[1] = (pixel >> 8) & 0xff;
37 p[2] = (pixel >> 16) & 0xff;
38 }
39 break;
40
41 case 4:
42 *(Uint32 *)p = pixel;
43 break;
44 }
45
46}
47
48Uint32 sdl_window::clamp(float r, float g, float b) {
49 unsigned int red = max(0.0f, min(255.0f, r * 255.0f));
50 unsigned int green = max(0.0f, min(255.0f, r * 255.0f));
51 unsigned int blue = max(0.0f, min(255.0f, r * 255.0f));
52
53 return red << 16 | green << 8 | blue;
54}
55
56void sdl_window::update(int x1, int y1, int x2, int y2) {
57 SDL_UpdateRect(screen, x1, y1, x2, y2);
58}
59
60void sdl_window::update(void) {
61 SDL_UpdateRect(screen, 0, 0, 0, 0);
62}
sdl_window.h
(10 / 0)
  
1#include "SDL.h"
2class sdl_window {
3 SDL_Surface *screen;
4 Uint32 clamp(float r, float g, float b);
5 public:
6 sdl_window(int w, int h);
7 void drawpixel(int x, int y, float r, float g, float b);
8 void update(void);
9 void update(int x1, int y1, int x2, int y2);
10};
wav.cpp
(6 / 13)
  
3333}
3434
3535int window_view::num_windows(void) {
36 int num_samples = (header.data_len / header.channels) / (header.sbits / 8);
37 return (num_samples / window_size) * 2;
36 return (num_samples() / window_size) * 2;
3837}
3938
40int main(int argc, char** argv) {
41 int ws = 1000;
42 window_view view (argv[1], ws);
43 double l[ws];
44 double r[ws];
39int window_view::num_samples(void) {
40 return (header.data_len / header.channels) / (header.sbits / 8);
41}
4542
46 for (int i = 0; i < view.num_windows(); ++i) {
47 view.next_window(l, r);
48 cout << l[0] << " " << r[0] << "\n";
49 }
50
51 return 0;
43int window_view::rate(void) {
44 return header.rate;
5245}
wav.h
(12 / 10)
  
1#include <string>
1#include <stdint.h>
22#include <fstream>
33using namespace std;
44
55struct wav_header {
66 //riff chunk
77 char riff[4];
8 int len;
8 int32_t len;
99 char wave[4];
1010
1111 //format chunk
1212 char fmt[4];
13 int fmtlen;
14 short int ftag;
15 short int channels;
16 int rate;
17 int bbs;
18 short int stype;
19 short int sbits;
13 int32_t fmtlen;
14 int16_t ftag;
15 int16_t channels;
16 int32_t rate;
17 int32_t bbs;
18 int16_t stype;
19 int16_t sbits;
2020
2121 //data chunk
2222 char text_data[4];
23 int data_len;
23 int32_t data_len;
2424};
2525
2626class window_view {
3333 ~window_view();
3434 void next_window(double *left, double *right);
3535 int num_windows(void);
36 int num_samples(void);
37 int rate(void);
3638};