-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrameRecorder.pde
134 lines (116 loc) · 3.09 KB
/
FrameRecorder.pde
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
// if you want to use <a href="https://www.funprogramming.org/VideoExport-for-Processing/">Video Export</a>,
// delete comment after this comment.
//import com.hamoid.*;
interface FrameRecorder {
abstract void recordFrame();
abstract void finish();
}
final class SyncFrameRecorder implements FrameRecorder {
private final String frameFormat;
SyncFrameRecorder(String path, String ext) {
frameFormat = path + "/########." + ext;
}
void recordFrame() {
saveFrame(frameFormat);
}
void finish() {
}
}
final class AsyncFrameRecorder implements FrameRecorder {
private final ExecutorService executor = Executors.newCachedThreadPool();
private final List<Future> futures = new ArrayList<Future>();
private final String recordPath;
AsyncFrameRecorder(String path) {
recordPath = path;
}
void recordFrame() {
if (executor.isShutdown()) {
return;
}
loadPixels();
final int[] savePixels = Arrays.copyOf(pixels, pixels.length);
final long saveFrameCount = frameCount;
Runnable saveTask = new Runnable() {
public void run() {
final PImage frameImage = createImage(width, height, HSB);
frameImage.pixels = savePixels;
frameImage.save(String.format("%s/%08d.jpg", recordPath, saveFrameCount));
}
};
Iterator<Future> it = futures.iterator();
while (it.hasNext()) {
final Future f = it.next();
if (f.isDone()) {
it.remove();
}
}
futures.add(executor.submit(saveTask));
}
void finish() {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
for (Future f : futures) {
if (f.isDone() == false && f.isCancelled() == false) {
try {
f.get();
}
catch (InterruptedException e) {
}
catch (ExecutionException e) {
}
}
}
if (executor.isShutdown() == false) {
executor.shutdown();
try {
if (executor.awaitTermination(5, TimeUnit.SECONDS) == false) {
executor.shutdownNow();
executor.awaitTermination(5, TimeUnit.SECONDS);
}
}
catch (InterruptedException e) {
executor.shutdownNow();
}
}
}
}
/*
final class VideoExportRecorder extends FrameRecorder {
private final VideoExport videoExport;
VideoExportRecorder(PApplet applet) {
videoExport = new VideoExport(applet, "movie.mp4");
videoExport.startMovie();
}
void recordFrame() {
videoExport.saveFrame();
}
void finish() {
videoExport.endMovie();
}
}
*/
enum FrameRecorderType {
//VideoExportRecorder,
SyncTgaRecorder,
SyncPngRecorder,
AsyncRecorder
}
FrameRecorder createFrameRecorderInstanceOf(FrameRecorderType type, String path) {
switch (type) {
/*
case VideoExportRecorder:
return new VideoExportRecorder(this);
*/
case SyncTgaRecorder:
return new SyncFrameRecorder(path, "tga");
case SyncPngRecorder:
return new SyncFrameRecorder(path, "png");
case AsyncRecorder:
return new AsyncFrameRecorder(path);
default:
throw new RuntimeException();
}
}