-
Notifications
You must be signed in to change notification settings - Fork 0
/
rec2.c
108 lines (96 loc) · 3.27 KB
/
rec2.c
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
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <time.h>
#include "output.h"
#include "gfx.h"
#include "text.h"
#include "render.h"
#include "arecord.h"
#ifdef WIN32
#include <conio.h>
#else
#include <fcntl.h>
#endif
#define SECONDS 40
// Goals:
// 1. Use keypress to start/stop recording and play
// I made this video because I just wanted to upload something onto my youtube channel.
// This video uses the PortAudio library to record sound from a microphone.
// Then, it uses FFmpeg to put that sound into this video.
// For the last couple of days, I have been working on the Codim project.
// There may be another update soon, but there also may not be, if I stop working on Codim for whatever reason.
int main() {
ARecordContext* arc = arecord_create(44100);
arecord_open(arc);
arecord_start(arc);
puts("Recording...");
size_t i = 0;
clock_t time = clock();
while (arecord_isactive(arc)) {
clock_t c = clock();
if ((c - time) >= CLOCKS_PER_SEC) {
time = c;
i++;
printf("%llu seconds passed\n", i);
}
if (kbhit()) {
arecord_stop(arc);
}
}
arecord_close(arc);
size_t sampleno = 0, frameno = 0;
OutputContext* oc = output_create("myfile.mp4", &(OutputAudioOpts){
.sample_rate = 44100,
}, &(OutputVideoOpts) {
.width = 1920,
.height = 1080,
.fps = 24,
});
GfxContext* gc = gfx_create(1920, 1080);
RenderContext rc;
render_init(&rc);
TextContext tc;
text_init(&tc, "sample.ttf", 45);
RenderDrawable rd;
text_render(&tc,
"I made this video because I just wanted to upload something onto my youtube channel.\n"
"This video uses the PortAudio library to record sound from a microphone.\n"
"Then, it uses FFmpeg to put that sound into this video.\n"
"For the last couple of days, I have been working on the Codim project.\n"
"There may be another update soon, but there also may not be,\n"
"if I stop working on Codim for whatever reason."
"\n\n\n\n-- AncientStraits 2024"
, 60.0, 1000.0, &rd);
render_add(&rc, rd);
output_open(oc);
while (output_is_open(oc)) {
// if (frameno >= 24*SECONDS) {
// output_close(oc);
// }
if (output_get_encode_type(oc) == OUTPUT_TYPE_VIDEO) {
glClearColor(0.3, 0.5, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
render(&rc, 1920, 1080);
gfx_render(gc, oc->vf);
output_encode_video(oc);
frameno++;
} else if (output_get_encode_type(oc) == OUTPUT_TYPE_AUDIO) {
assert(oc->acc->ch_layout.nb_channels == 2);
int16_t* data = (int16_t*)oc->afd->data[0];
for (int i = 0; i < oc->afd->nb_samples * 2; i++) {
*data++ = arc->udata.samples[sampleno];
sampleno++;
}
if (sampleno >= arc->udata.idx*2)
output_close(oc);
output_encode_audio(oc);
}
}
text_deinit(&tc);
render_deinit(&rc);
gfx_destroy(gc);
output_destroy(oc);
arecord_destroy(arc);
return 0;
}