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
|
import React = require("react");
import { FieldViewProps, FieldView } from './FieldView';
import { observer } from "mobx-react";
import "./AudioBox.scss";
import { Cast, DateCast } from "../../../new_fields/Types";
import { AudioField, nullAudio } from "../../../new_fields/URLField";
import { DocExtendableComponent } from "../DocComponent";
import { makeInterface, createSchema } from "../../../new_fields/Schema";
import { documentSchema } from "../../../new_fields/documentSchemas";
import { Utils } from "../../../Utils";
import { RouteStore } from "../../../server/RouteStore";
import { runInAction, observable, reaction, IReactionDisposer, computed, action } from "mobx";
import { DateField } from "../../../new_fields/DateField";
import { SelectionManager } from "../../util/SelectionManager";
import { Doc, DocListCast } from "../../../new_fields/Doc";
import { ContextMenuProps } from "../ContextMenuItem";
import { ContextMenu } from "../ContextMenu";
import { Id } from "../../../new_fields/FieldSymbols";
interface Window {
MediaRecorder: MediaRecorder;
}
declare class MediaRecorder {
// whatever MediaRecorder has
constructor(e: any);
}
export const audioSchema = createSchema({
playOnSelect: "boolean"
});
type AudioDocument = makeInterface<[typeof documentSchema, typeof audioSchema]>;
const AudioDocument = makeInterface(documentSchema, audioSchema);
@observer
export class AudioBox extends DocExtendableComponent<FieldViewProps, AudioDocument>(AudioDocument) {
public static LayoutString(fieldKey: string) { return FieldView.LayoutString(AudioBox, fieldKey); }
_linkPlayDisposer: IReactionDisposer | undefined;
_reactionDisposer: IReactionDisposer | undefined;
_scrubbingDisposer: IReactionDisposer | undefined;
_ele: HTMLAudioElement | null = null;
_recorder: any;
_lastUpdate = 0;
@observable private _audioState = 0;
@observable public static ScrubTime = 0;
public static ActiveRecordings: Doc[] = [];
componentDidMount() {
runInAction(() => this._audioState = this.path ? 2 : 0);
this._linkPlayDisposer = reaction(() => this.layoutDoc.scrollToLinkID,
scrollLinkId => {
scrollLinkId && DocListCast(this.dataDoc.links).map(l => {
const la1 = l.anchor1 as Doc;
const la2 = l.anchor2 as Doc;
if (l[Id] === scrollLinkId && la1 && la2) {
let doc = Doc.AreProtosEqual(la1, this.dataDoc) ? la2 : la1;
let seek = DateCast(la1.creationTime);
setTimeout(() => this.playFrom(seek.date.getTime()), 250);
}
});
scrollLinkId && (this.layoutDoc.scrollLinkID = undefined);
}, { fireImmediately: true });
this._reactionDisposer = reaction(() => SelectionManager.SelectedDocuments(),
selected => {
let sel = selected.length ? selected[0].props.Document : undefined;
this.Document.playOnSelect && sel && !Doc.AreProtosEqual(sel, this.props.Document) && this.playFrom(DateCast(sel.creationTime).date.getTime());
});
this._scrubbingDisposer = reaction(() => AudioBox.ScrubTime, time => this.Document.playOnSelect && this.playFrom(time));
}
updateHighlights = () => {
const extensionDoc = this.extensionDoc;
const htmlEle = this._ele;
const start = extensionDoc && DateCast(extensionDoc.recordingStart);
if (htmlEle && !htmlEle.paused && start) {
setTimeout(this.updateHighlights, 30);
DocListCast(this.dataDoc.links).map(l => {
let la1 = l.anchor1 as Doc;
if (Doc.AreProtosEqual(la1, this.dataDoc)) {
la1 = l.anchor2 as Doc;
}
let date = DateCast(la1.creationDate);
let offset = (date!.date.getTime() - start.date.getTime()) / 1000;
if (offset > this._lastUpdate && offset < htmlEle.currentTime) {
Doc.linkFollowHighlight(la1);
}
});
this._lastUpdate = htmlEle.currentTime;
}
}
playFrom = (seek: number) => {
const extensionDoc = this.extensionDoc;
let start = extensionDoc && DateCast(extensionDoc.recordingStart);
if (this._ele && start) {
if (seek) {
let delta = (seek - start.date.getTime()) / 1000;
if (start && delta > 0 && delta < this._ele.duration) {
this._ele.currentTime = delta;
this._ele.play();
this._lastUpdate = delta;
setTimeout(this.updateHighlights, 0);
} else {
this._ele.pause();
}
} else {
this._ele.pause();
}
}
}
componentWillUnmount() {
this._reactionDisposer && this._reactionDisposer();
this._linkPlayDisposer && this._linkPlayDisposer();
this._scrubbingDisposer && this._scrubbingDisposer();
}
recordAudioAnnotation = () => {
let gumStream: any;
let self = this;
const extensionDoc = this.extensionDoc;
extensionDoc && navigator.mediaDevices.getUserMedia({
audio: true
}).then(function (stream) {
gumStream = stream;
self._recorder = new MediaRecorder(stream);
extensionDoc.recordingStart = new DateField(new Date());
AudioBox.ActiveRecordings.push(self.props.Document);
self._recorder.ondataavailable = async function (e: any) {
const formData = new FormData();
formData.append("file", e.data);
const res = await fetch(Utils.prepend(RouteStore.upload), {
method: 'POST',
body: formData
});
const files = await res.json();
const url = Utils.prepend(files[0].path);
// upload to server with known URL
self.props.Document[self.props.fieldKey] = new AudioField(url);
};
runInAction(() => self._audioState = 1);
self._recorder.start();
setTimeout(() => {
self.stopRecording();
gumStream.getAudioTracks()[0].stop();
}, 60 * 60 * 1000); // stop after an hour?
});
}
specificContextMenu = (e: React.MouseEvent): void => {
let funcs: ContextMenuProps[] = [];
funcs.push({ description: (this.Document.playOnSelect ? "Don't play" : "Play") + " when document selected", event: () => this.Document.playOnSelect = !this.Document.playOnSelect, icon: "expand-arrows-alt" });
ContextMenu.Instance.addItem({ description: "Audio Funcs...", subitems: funcs, icon: "asterisk" });
}
stopRecording = action(() => {
this._recorder.stop();
this._audioState = 2;
let ind = AudioBox.ActiveRecordings.indexOf(this.props.Document);
ind !== -1 && (AudioBox.ActiveRecordings.splice(ind, 1));
});
recordClick = (e: React.MouseEvent) => {
if (e.button === 0 && !e.ctrlKey) {
this._recorder ? this.stopRecording() : this.recordAudioAnnotation();
e.stopPropagation();
}
}
playClick = (e: any) => setTimeout(this.updateHighlights, 30);
setRef = (e: HTMLAudioElement | null) => {
e && e.addEventListener("play", this.playClick as any);
this._ele = e;
}
@computed get path() {
let field = Cast(this.props.Document[this.props.fieldKey], AudioField);
let path = (field instanceof AudioField) ? field.url.href : "";
return path === nullAudio ? "" : path;
}
@computed get audio() {
let interactive = this.active() ? "-interactive" : "";
return <audio controls ref={this.setRef} className={`audiobox-control${interactive}`}>
<source src={this.path} type="audio/mpeg" />
Not supported.
</audio>;
}
render() {
let interactive = this.active() ? "-interactive" : "";
return (!this.extensionDoc ? (null) :
<div className={`audiobox-container`} onContextMenu={this.specificContextMenu} onClick={!this.path ? this.recordClick : undefined}>
<div className="audiobox-handle"></div>
{!this.path ?
<button className={`audiobox-record${interactive}`} style={{ backgroundColor: ["black", "red", "blue"][this._audioState] }}>
{this._audioState === 1 ? "STOP" : "RECORD"}
</button> :
this.audio
}
</div>
);
}
}
|