Skip to content

Commit

Permalink
added a color chooser for scales and lots of cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsheeler committed Oct 6, 2017
1 parent 256c4dd commit e4e6603
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 135 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ LFLAGS = $(shell pkg-config --libs wayland-client) \
-lccv -lm -lpng -ljpeg -lswscale -lavutil -lswresample \
-lavformat -lavcodec -lpthread -ljack
LIBS =
#CFLAGS =-g -Wall
CFLAGS = -O3 -ffast-math -Wall \
#CFLAGS = -O3 -ffast-math -Wall
CFLAGS =-g -Wall \
$(shell pkg-config --cflags pangocairo) \
$(shell pkg-config --cflags gtk+-3.0) \
$(shell pkg-config --cflags fftw3)
Expand Down
21 changes: 11 additions & 10 deletions dingle_dots.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
#include "dingle_dots.h"
#include "midi.h"

int dingle_dots_init(dingle_dots_t *dd, int width, int height) {
int dingle_dots_init(dingle_dots_t *dd, char *dev_name, int width, int height,
char *video_file_name, int video_bitrate) {
memset(dd, 0, sizeof(dingle_dots_t));
int ret;
dd->camera_rect.width = width;
dd->camera_rect.height = height;
dd->recording_started = 0;
dd->recording_stopped = 0;
dd->nports = 2;
dd->make_new_tld = 0;
dd->dev_name = dev_name;
strncpy(dd->video_file_name, video_file_name, STR_LEN);
dd->video_bitrate = video_bitrate;
dd->analysis_rect.width = 260;
dd->analysis_rect.height = 148;
dd->ascale_factor_x = dd->camera_rect.width / (double)dd->analysis_rect.width;
Expand Down Expand Up @@ -76,17 +84,10 @@ int dingle_dots_free(dingle_dots_t *dd) {
return 0;
}

void dingle_dots_add_scale(dingle_dots_t *dd, midi_key_t *key) {
void dingle_dots_add_scale(dingle_dots_t *dd, midi_key_t *key, color *c) {
int i;
double x_delta;
color c;
srand(time(NULL));
struct hsva h;
h.h = (double) rand() / RAND_MAX;
h.v = 0.45;
h.s = 1.0;
h.a = 0.5;
c = hsv2rgb(&h);
x_delta = 1. / (key->num_steps + 1);
for (i = 0; i < key->num_steps; i++) {
char key_name[NCHAR];
Expand All @@ -98,7 +99,7 @@ void dingle_dots_add_scale(dingle_dots_t *dd, midi_key_t *key) {
dingle_dots_add_note(dd, key_name, i + 1,
key->base_note + key->steps[i],
x_delta * (i + 1) * dd->camera_rect.width, dd->camera_rect.height / 2.,
dd->camera_rect.width/32, &c);
dd->camera_rect.width/32, c);
}
}

Expand Down
31 changes: 29 additions & 2 deletions dingle_dots.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,33 @@
#include "kmeter.h"
#include "midi.h"

#define STR_LEN 80

typedef struct sound_shape sound_shape;
typedef struct dingle_dots_t dingle_dots_t;
typedef struct midi_key_t midi_key_t;

struct buffer {
void *start;
size_t length;
};

struct dingle_dots_t {
GApplication *app;
char *dev_name;
gboolean fullscreen;
char video_file_name[STR_LEN];
int recording_started;
int recording_stopped;
int can_process;
int can_capture;
int audio_done;
int video_done;
int trailer_written;
int make_new_tld;
int use_rand_color_for_scale;
uint32_t video_bitrate;
AVFormatContext *video_output_context;
struct timespec out_frame_ts;
disk_thread_info_t audio_thread_info;
disk_thread_info_t video_thread_info;
Expand Down Expand Up @@ -44,17 +64,24 @@ struct dingle_dots_t {
GdkPoint mdown_pos;
GtkWidget *scale_combo;
GtkWidget *note_combo;
GtkWidget *rand_color_button;
GtkWidget *scale_color_button;
cairo_surface_t *csurface;
cairo_t *cr;
long jack_overruns;
int nports;
jack_port_t **in_ports;
jack_port_t **out_ports;
jack_client_t *client;
jack_port_t *midi_port;
jack_ringbuffer_t *midi_ring_buf;
};

int dingle_dots_init(dingle_dots_t *dd, int width, int height);
int dingle_dots_init(dingle_dots_t *dd, char *dev_name, int width, int height,
char *video_file_name, int video_bitrate);
int dingle_dots_free(dingle_dots_t *dd);
int dingle_dots_deactivate_sound_shapes(dingle_dots_t *dd);
int dingle_dots_add_note(dingle_dots_t *dd, char *scale_name,
int scale_num, int midi_note, double x, double y, double r, color *c);
void dingle_dots_add_scale(dingle_dots_t *dd, midi_key_t *key);
void dingle_dots_add_scale(dingle_dots_t *dd, midi_key_t *key, color *c);
#endif
51 changes: 26 additions & 25 deletions muxing.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static int write_frame(AVFormatContext *fmt_ctx,
/* Add an output stream. */
static void add_stream(OutputStream *ost, AVFormatContext *oc,
AVCodec **codec, enum AVCodecID codec_id, int width,
int height) {
int height, int video_bitrate) {
AVCodecContext *c;
int i;
*codec = avcodec_find_encoder(codec_id);
Expand Down Expand Up @@ -81,7 +81,7 @@ static void add_stream(OutputStream *ost, AVFormatContext *oc,
case AVMEDIA_TYPE_VIDEO:
printf("case VIDEO\n");
c->codec_id = codec_id;
c->bit_rate = stream_bitrate;
c->bit_rate = video_bitrate;
c->width = width;
c->height = height;
ost->st->time_base = (AVRational){ 1, STREAM_FRAME_RATE };
Expand Down Expand Up @@ -173,12 +173,12 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost,
}
}

int get_audio_frame(OutputStream *ost, AVFrame **ret_frame) {
int get_audio_frame(dingle_dots_t *dd, OutputStream *ost, AVFrame **ret_frame) {
AVFrame *frame = ost->tmp_frame;
int j, i;
float *q = (float*)frame->data[0];
*ret_frame = frame;
if (recording_stopped) {
if (dd->recording_stopped) {
printf("audio done\n");
ret_frame = NULL;
return 1;
Expand Down Expand Up @@ -210,7 +210,7 @@ int write_audio_frame(dingle_dots_t *dd, AVFormatContext *oc,
int ret;
int dst_nb_samples;
c = ost->enc;
ret = get_audio_frame(ost, &frame);
ret = get_audio_frame(dd, ost, &frame);
if (ret < 0) {
return -1;
} else if (ret == 1) {
Expand Down Expand Up @@ -308,13 +308,13 @@ static void open_video(int width, int height, AVFormatContext *oc, AVCodec *code
}
}

int get_video_frame(OutputStream *ost, AVFrame **ret_frame) {
int get_video_frame(dingle_dots_t *dd, OutputStream *ost, AVFrame **ret_frame) {
AVCodecContext *c = ost->enc;
*ret_frame = NULL;
int size = ost->out_frame.size + sizeof(struct timespec);
int space = jack_ringbuffer_read_space(video_ring_buf);
if (space < size) {
if (recording_stopped) {
if (dd->recording_stopped) {
return 1;
} else {
return -1;
Expand Down Expand Up @@ -353,16 +353,16 @@ int get_video_frame(OutputStream *ost, AVFrame **ret_frame) {
* encode one video frame and send it to the muxer
* return 1 when encoding is finished, 0 otherwise
*/
int write_video_frame(AVFormatContext *oc, OutputStream *ost)
int write_video_frame(dingle_dots_t *dd, AVFormatContext *oc, OutputStream *ost)
{
int ret;
AVCodecContext *c;
AVFrame *tframe;
AVPacket pkt;
c = ost->enc;
ret = get_video_frame(ost, &tframe);
ret = get_video_frame(dd, ost, &tframe);
if (ret < 0) {
if(recording_stopped) {
if(dd->recording_stopped) {
return 1;
}
return -1;
Expand Down Expand Up @@ -419,29 +419,30 @@ int init_output(dingle_dots_t *dd) {
int ret;
AVDictionary *opt = NULL;
av_register_all();
filename = out_file_name;
avformat_alloc_output_context2(&oc, NULL, NULL, filename);
if (!oc) {
filename = dd->video_file_name;
avformat_alloc_output_context2(&dd->video_output_context, NULL, NULL, filename);
if (!dd->video_output_context) {
printf("Could not deduce output format from file extension: using MPEG.\n");
avformat_alloc_output_context2(&oc, NULL, "mpeg", filename);
avformat_alloc_output_context2(&dd->video_output_context, NULL, "mpeg", filename);
}
if (!oc)
if (!dd->video_output_context)
return 1;
fmt = oc->oformat;
add_stream(&dd->video_thread_info.stream, oc,
&video_codec, fmt->video_codec, dd->camera_rect.width, dd->camera_rect.height);
fmt = dd->video_output_context->oformat;
add_stream(&dd->video_thread_info.stream, dd->video_output_context,
&video_codec, fmt->video_codec, dd->camera_rect.width,
dd->camera_rect.height, dd->video_bitrate);
if (fmt->audio_codec != AV_CODEC_ID_NONE) {
add_stream(&dd->audio_thread_info.stream, oc,
&audio_codec, fmt->audio_codec, 0, 0);
add_stream(&dd->audio_thread_info.stream, dd->video_output_context,
&audio_codec, fmt->audio_codec, 0, 0, 0);
}
av_dict_set(&opt, "cpu-used", "-8", 0);
av_dict_set(&opt, "deadline", "realtime", 0);
open_video(dd->camera_rect.width, dd->camera_rect.height,
oc, video_codec, &dd->video_thread_info.stream, opt);
open_audio(oc, audio_codec, &dd->audio_thread_info.stream, opt);
av_dump_format(oc, 0, filename, 1);
dd->video_output_context, video_codec, &dd->video_thread_info.stream, opt);
open_audio(dd->video_output_context, audio_codec, &dd->audio_thread_info.stream, opt);
av_dump_format(dd->video_output_context, 0, filename, 1);
if (!(fmt->flags & AVFMT_NOFILE)) {
ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
ret = avio_open(&dd->video_output_context->pb, filename, AVIO_FLAG_WRITE);
if (ret < 0) {
fprintf(stderr, "Could not open '%s': %s\n", filename,
av_err2str(ret));
Expand All @@ -450,7 +451,7 @@ int init_output(dingle_dots_t *dd) {
}

/* Write the stream header, if any. */
ret = avformat_write_header(oc, &opt);
ret = avformat_write_header(dd->video_output_context, &opt);
if (ret < 0) {
fprintf(stderr, "Error occurred when opening output file: %s\n",
av_err2str(ret));
Expand Down
15 changes: 2 additions & 13 deletions muxing.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,13 @@

#define SCALE_FLAGS SWS_BICUBIC

int write_video_frame(AVFormatContext *oc, OutputStream *ost);
int write_video_frame(dingle_dots_t *dd, AVFormatContext *oc,
OutputStream *ost);
int write_audio_frame(dingle_dots_t *dd, AVFormatContext *oc,
OutputStream *ost);
int init_output();
void close_stream(AVFormatContext *oc, OutputStream *ost);
extern AVFormatContext *oc;
extern AVFrame *frame;
extern jack_ringbuffer_t *video_ring_buf, *audio_ring_buf;
extern char *out_file_name;
extern uint32_t stream_bitrate;
extern double ascale_factor_x;
extern double ascale_factor_y;
extern uint32_t width;
extern uint32_t height;
extern uint32_t awidth;
extern uint32_t aheight;
extern volatile int can_capture;
extern int recording_started;
extern int recording_stopped;
extern pthread_mutex_t av_thread_lock;
#endif
9 changes: 7 additions & 2 deletions sound_shape.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int sound_shape_init(sound_shape *ss, char *label,
strncpy(ss->label, label, NCHAR);
ss->midi_note = midi_note;
ss->normal = color_copy(c);
ss->playing = color_lighten(c, 0.75);
ss->playing = color_lighten(c, 0.95);
ss->on = 0;
ss->mdown = 0;
return 0;
Expand Down Expand Up @@ -47,7 +47,7 @@ static void sound_shape_render_label(sound_shape *ss, cairo_t *cr) {

int sound_shape_render(sound_shape *ss, cairo_t *cr) {
color *c;
c = ss->on ? &ss->playing : &ss->normal;
c = &ss->normal;
cairo_save(cr);
cairo_set_source_rgba(cr, c->r, c->g, c->b, c->a);
cairo_translate(cr, ss->x, ss->y);
Expand All @@ -57,6 +57,11 @@ int sound_shape_render(sound_shape *ss, cairo_t *cr) {
cairo_set_source_rgba(cr, 0.5*c->r, 0.5*c->g, 0.5*c->b, 0.75);
cairo_set_line_width(cr, 0.05 * ss->r);
cairo_stroke(cr);
if (ss->on) {
cairo_set_source_rgba(cr, 1, 1, 1, 0.25);
cairo_arc(cr, 0, 0, ss->r*1.025, 0, 2 * M_PI);
cairo_fill(cr);
}
if (ss->hovered) {
cairo_set_source_rgba(cr, 1, 1, 1, 0.25);
cairo_arc(cr, 0, 0, ss->r, 0, 2 * M_PI);
Expand Down
Loading

0 comments on commit e4e6603

Please sign in to comment.