summaryrefslogtreecommitdiff
path: root/sv.c
blob: ba27f921870618b49c45788101089c8f827382f1 (plain)
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
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <alsa/asoundlib.h>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#define MIDICHK(stmt, msg) if((stmt) < 0) {puts("ERROR: "#msg); exit(1);}

int width = 1920;
int height = 1080;
GLFWwindow* window;
GLuint vertex;
GLuint fragment;
GLuint shaderProgram;
GLuint backbuffer;

typedef struct Shad {
  GLuint id;
  char fragment[40];
  time_t mtime;
  int new;
} Shader;

Shader shader;

typedef struct Img {
  GLuint id;
  char path[40];
  int new;
} Image;

Image images[8];

float params[4];

int record = 0;
int recording = 0;
int fullscreen = 0;

FILE* ffmpeg;

static snd_seq_t *midi_seq;
static int midiin;

int ticks =0;
int running = 0;
int program=0;
int frame = 0;
unsigned char buffer[1920*1080*4];
struct arg_struct {
  unsigned char buffer[1920*1080*4];
    int frame;
};

void *screenshot(void * f) {
  unsigned char pixels[width*height*4];
  //time_t current_time = time(NULL);
  //char output_file[25];
  char output_file[20];
  //strftime(output_file, 25, "%Y-%m-%d_%H%M%S.png", localtime(&current_time));
  sprintf(output_file, "record/%i.png",*(int*)f);
  glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

  // Flip the image on Y
  int depth = 4;
  int row,col,z;
  stbi_uc temp;
  for (row = 0; row < (height>>1); row++) {
   for (col = 0; col < width; col++) {
      for (z = 0; z < depth; z++) {
         temp = pixels[(row * width + col) * depth + z];
         pixels[(row * width + col) * depth + z] = pixels[((height - row - 1) * width + col) * depth + z];
         pixels[((height - row - 1) * width + col) * depth + z] = temp;
      }
   }
  }
  printf("%s\n",output_file);
  if (0 == stbi_write_png(output_file, width, height, 4, pixels, width * 4)) { printf("can't create file %s",output_file); }
}

void *save(void * b) {
      //struct arg_struct args;
      //glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  char output_file[20];
  sprintf(output_file, "record/%i.png",frame);
  printf("%ix%i\n",width,height);
  printf("%s\n",output_file);
  int r = stbi_write_png(output_file, width, height, 4, buffer, width * 4);
  printf("%s\n",output_file);
  printf("%i\n",r);
  //struct arg_struct *args = arguments;
  //char output_file[20];
  //sprintf(output_file, "record/%i.png",frame);
  //printf("%s\n",output_file);
  //int r = stbi_write_png(output_file, width, height, 4, buffer, width * 4);
  //printf("%i\n",r);
  //if (0 == stbi_write_png(output_file, width, height, 4, buffer, width * 4)) { printf("can't create file %s",output_file); }
  return(NULL);
}

static void error_callback(int error, const char* description) { fputs(description, stderr); }

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
    if (key == GLFW_KEY_Q && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE);
    //else if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) screenshot();
}

const char * readShader(char * path) {
  FILE *file = fopen(path, "r");
  if (!file) { fprintf(stderr,"Cannot read %s\n",path); }
  char source[100000];
  int res = fread(source,1,100000-1,file);
  source[res] = 0;
  fclose(file);
  const char *c_str = source;
  return c_str;
}

GLuint compileShader(const char * source, GLenum type) {
  GLuint sh = glCreateShader(type);
  glShaderSource(sh, 1, &source, NULL);
  glCompileShader(sh);
  int success;
  glGetShaderiv(sh, GL_COMPILE_STATUS, &success);
  if (!success) {
    char infoLog[512];
    glGetShaderInfoLog(sh, 512, NULL, infoLog);
    fprintf(stderr,"Shader compilation failed: %s\n",infoLog);
  }
  else { return sh; }
}

GLuint linkShader(GLuint vertex, GLuint fragment) {
  glUseProgram(0);
  glDeleteProgram(shader.id);
  shader.id = glCreateProgram();
  glAttachShader(shader.id, vertex);
  glAttachShader(shader.id, fragment);
  glLinkProgram(shader.id);
  int success;
  glGetProgramiv(shader.id, GL_LINK_STATUS, &success);
  if (!success) {
    char infoLog[512];
    glGetProgramInfoLog(shader.id, 512, NULL, infoLog);
    fprintf(stderr,"Shader linking failed: %s\n",infoLog);
  }
  glDetachShader(shader.id, vertex); 
  glDetachShader(shader.id, fragment); 
  glDeleteShader(vertex);
  glDeleteShader(fragment);
}

void createShader() {
  static const char vertex_src[] = {
    "#version 450 core\n"
    "const vec2 quadVertices[4] = { vec2(-1.0, -1.0), vec2(1.0, -1.0), vec2(-1.0, 1.0), vec2(1.0, 1.0) };\n"
    "void main() { gl_Position = vec4(quadVertices[gl_VertexID], 0.0, 1.0); }\n"
  };
  vertex = compileShader(vertex_src, GL_VERTEX_SHADER);
  fragment = compileShader(readShader(shader.fragment),GL_FRAGMENT_SHADER);
  linkShader(vertex,fragment);
  struct stat file_stat;
  stat(shader.fragment, &file_stat);
  shader.mtime = file_stat.st_mtime;
  glUseProgram(shader.id);
};

void imageUniforms(int i) {
  glUniform1i(glGetUniformLocation(shader.id, "images")+i,i);
}

void resize_callback(GLFWwindow* window, int w, int h) {
  width = w;
  height = h;
  glUniform2f(glGetUniformLocation(shader.id, "resolution"),width,height); 
}

void readImage(int i) {

  images[i].new = 1;

  int w,h,comp;
  stbi_set_flip_vertically_on_load(1);
  unsigned char* pixels = stbi_load(images[i].path, &w, &h, &comp, STBI_rgb_alpha);
  imageUniforms(i);

  glActiveTexture(GL_TEXTURE0+i);
  glGenTextures(1, &images[i].id);
  glBindTexture(GL_TEXTURE_2D,images[i].id);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  stbi_image_free(pixels);
}

void *readMidi() {

  MIDICHK(snd_seq_open(&midi_seq, "default", SND_SEQ_OPEN_INPUT, 0), "Could not open sequencer");
  MIDICHK(snd_seq_set_client_name(midi_seq, "SV"), "Could not set client name");
  MIDICHK(midiin = snd_seq_create_simple_port(midi_seq, "sv:in", SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, SND_SEQ_PORT_TYPE_APPLICATION), "Could not open port");
  
  while(1) {
    snd_seq_event_t *ev = NULL;
    snd_seq_event_input(midi_seq, &ev);
    if (ev->type == SND_SEQ_EVENT_NOTEON) {
      int note = ev->data.note.note;
      if (note >= 36) program = note-36;
    }
    else if(ev->type == SND_SEQ_EVENT_CONTROLLER) {
      int p = ev->data.control.param;
      int v = ev->data.control.value;
      if (p < 4) params[p] = (float)v/127.0;
    }
    else if(ev->type == SND_SEQ_EVENT_SONGPOS)
      ticks = ev->data.control.value*24;
    else if(ev->type == SND_SEQ_EVENT_START) {
      running = 1;
      if (record) {
      /*
        time_t current_time = time(NULL);
        char output_file[25];
        strftime(output_file, 25, "%Y-%m-%d_%H%M%S.mkv", localtime(&current_time));
        char cmd[250];
        sprintf(cmd, "ffmpeg -framerate 60 -f rawvideo -pix_fmt rgba -s %ix%i -i - -preset ultrafast -y -c:v libx264 -crf 0 -vf vflip %s",width,height,output_file);
        ffmpeg = popen(cmd, "w");
        */
        frame = 0;
        recording = 1;
      }
    }
    else if(ev->type == SND_SEQ_EVENT_STOP) {
      running = 0;
      if (recording) {
        recording = 0;
        //pclose(ffmpeg);
      }
    }
    else if(ev->type == SND_SEQ_EVENT_CONTINUE)
      running = 1;
    else if(ev->type == SND_SEQ_EVENT_CLOCK)
      if (running) ticks++;
  }
}

void *watchShader() {
  while (1) {
    sleep(0.2);
    struct stat file_stat;
    stat(shader.fragment, &file_stat);
    if (file_stat.st_mtime > shader.mtime) shader.new = 1; 
  }
}

int main(int argc, char **argv) {

  // parse options
  int opt;
  while ((opt = getopt(argc, argv, "rf")) != -1) {
    switch (opt) {
      case 'r': record = 1; fullscreen = 1; break;
      case 'f': fullscreen = 1; break;
    }
  }

  for (int i = optind; i<argc; i++) {
    strncpy(images[i-optind].path, argv[i],40);
    images[i-optind].new = 1;
  }

  // createWindow
  glfwInit();
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  glfwWindowHint(GLFW_DECORATED, GL_FALSE);
  const GLFWvidmode * mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
  width = mode->width;
  height = mode->height;
  if (fullscreen) {
    window = glfwCreateWindow(width,height, "", glfwGetPrimaryMonitor(), NULL);
  } else {
    window = glfwCreateWindow(width,height, "sv", NULL, NULL);
    glfwSetWindowSizeCallback(window, resize_callback);
  }
  glfwMakeContextCurrent(window);
  glfwSetKeyCallback(window, key_callback);
  glfwSetErrorCallback(error_callback);
  glewInit();

  strncpy(shader.fragment,"shader.frag",40);
  shader.new = 1;
  unsigned int VAO;
  glGenVertexArrays(1, &VAO);
  glBindVertexArray(VAO);

  glfwGetWindowSize(window, &width, &height);
  glCreateTextures(GL_TEXTURE_2D,1,&backbuffer);
  glTextureStorage2D(backbuffer,1,GL_RGBA32F,width,height);
  glBindImageTexture(0,backbuffer, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
  for (int i = 0; i<8; i++) {
    readImage(i);
    images[i].new = 0;
  }
  
  pthread_t midiin_t;
  pthread_create(&midiin_t, NULL, readMidi, NULL);
  pthread_t frag_t;
  pthread_create(&frag_t, NULL, watchShader, NULL);

  int n = 0;
  printf("%ix%i\n",width,height);
  while (!glfwWindowShouldClose(window)) {
    if (shader.new) {
      createShader();
      glUniform2f(glGetUniformLocation(shader.id, "resolution"),width,height); // important!!
      for (int i = 0; i<8; i++) { imageUniforms(i); }
      shader.new = 0;
    }
    glUniform1f(glGetUniformLocation(shader.id, "time"),glfwGetTime());
    glUniform1f(glGetUniformLocation(shader.id, "ticks"),(float)ticks);
    glUniform1i(glGetUniformLocation(shader.id, "program"),program);
    for (int i = 0; i<4; i++) {
      glUniform1f(glGetUniformLocation(shader.id, "params")+i, params[i]);
    }

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glfwSwapBuffers(window);

    if (recording) {
    /*
      pid_t pid = fork();
      if (pid == 0) {
        unsigned char buffer[width*height*4];
      //struct arg_struct args;
        glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
        char output_file[20];
        sprintf(output_file, "record/%i.png",frame);
        printf("%s\n",output_file);
        int r = stbi_write_png(output_file, width, height, 4, buffer, width * 4);
        printf("%i\n",r);
        exit(EXIT_SUCCESS);
      }
      */
      //args.buffer = buffer;
      //args.frame = frame;
      glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
      pthread_t tid;
      pthread_create (&tid, NULL, save, NULL);
      pthread_detach (tid);
      //printf("%i\n",frame);
      frame++;
      //fwrite(buffer, sizeof(int)*width*height, 1, ffmpeg);
    }

    glfwPollEvents();
  }

  //if (recording) pclose(ffmpeg);
  pthread_cancel(midiin_t);
  pthread_cancel(frag_t);
  glfwDestroyWindow(window);
  glfwTerminate();
 
  exit(EXIT_SUCCESS);
}