From 73b5fd788b76fc7d08ae7dd4fe48ca6ab42c8439 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Fri, 23 Mar 2018 11:34:32 +0100 Subject: intermediary commit --- sv.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 16 deletions(-) diff --git a/sv.c b/sv.c index 7cdfa20..ba27f92 100644 --- a/sv.c +++ b/sv.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -16,8 +17,8 @@ #define MIDICHK(stmt, msg) if((stmt) < 0) {puts("ERROR: "#msg); exit(1);} -int width = 1918; -int height = 1078; +int width = 1920; +int height = 1080; GLFWwindow* window; GLuint vertex; GLuint fragment; @@ -55,12 +56,20 @@ 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 *screenshot(void * f) { unsigned char pixels[width*height*4]; - time_t current_time = time(NULL); - char output_file[25]; - strftime(output_file, 25, "%Y-%m-%d_%H%M%S.png", localtime(¤t_time)); + //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(¤t_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 @@ -76,14 +85,35 @@ void screenshot() { } } } + 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(); + //else if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) screenshot(); } const char * readShader(char * path) { @@ -148,7 +178,6 @@ void createShader() { void imageUniforms(int i) { glUniform1i(glGetUniformLocation(shader.id, "images")+i,i); - //glUniform1f(glGetUniformLocation(shader.id, "ratios")+i,images[i].ratio); } void resize_callback(GLFWwindow* window, int w, int h) { @@ -164,8 +193,6 @@ void readImage(int i) { 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); - - //images[i].ratio = (float) w/h; imageUniforms(i); glActiveTexture(GL_TEXTURE0+i); @@ -206,12 +233,15 @@ void *readMidi() { 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.mp4", localtime(¤t_time)); + strftime(output_file, 25, "%Y-%m-%d_%H%M%S.mkv", localtime(¤t_time)); char cmd[250]; - sprintf(cmd, "ffmpeg -r 60 -f rawvideo -pix_fmt rgba -s %ix%i -i - -threads 0 -preset veryfast -y -pix_fmt yuv420p -c:v libx264 -crf 18 -vf vflip %s",width,height,output_file); + 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; } } @@ -219,7 +249,7 @@ void *readMidi() { running = 0; if (recording) { recording = 0; - pclose(ffmpeg); + //pclose(ffmpeg); } } else if(ev->type == SND_SEQ_EVENT_CONTINUE) @@ -292,6 +322,8 @@ int main(int argc, char **argv) { 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(); @@ -308,15 +340,37 @@ int main(int argc, char **argv) { glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glfwSwapBuffers(window); + if (recording) { - unsigned char buffer[width*height*4]; + /* + 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); - fwrite(buffer, sizeof(int)*width*height, 1, ffmpeg); + 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); + //if (recording) pclose(ffmpeg); pthread_cancel(midiin_t); pthread_cancel(frag_t); glfwDestroyWindow(window); -- cgit v1.2.3