@siteed/expo-audio-studio 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/CHANGELOG.md +210 -0
  2. package/LICENSE +21 -0
  3. package/README.md +269 -0
  4. package/android/build.gradle +105 -0
  5. package/android/src/main/AndroidManifest.xml +27 -0
  6. package/android/src/main/java/net/siteed/audiostream/AudioAnalysisData.kt +166 -0
  7. package/android/src/main/java/net/siteed/audiostream/AudioDataEncoder.kt +9 -0
  8. package/android/src/main/java/net/siteed/audiostream/AudioFileHandler.kt +131 -0
  9. package/android/src/main/java/net/siteed/audiostream/AudioFormatUtils.kt +103 -0
  10. package/android/src/main/java/net/siteed/audiostream/AudioNotificationsManager.kt +435 -0
  11. package/android/src/main/java/net/siteed/audiostream/AudioProcessor.kt +2235 -0
  12. package/android/src/main/java/net/siteed/audiostream/AudioRecorderManager.kt +1437 -0
  13. package/android/src/main/java/net/siteed/audiostream/AudioRecordingService.kt +166 -0
  14. package/android/src/main/java/net/siteed/audiostream/AudioTrimmer.kt +1099 -0
  15. package/android/src/main/java/net/siteed/audiostream/Constants.kt +21 -0
  16. package/android/src/main/java/net/siteed/audiostream/EventSender.kt +7 -0
  17. package/android/src/main/java/net/siteed/audiostream/ExpoAudioStreamModule.kt +739 -0
  18. package/android/src/main/java/net/siteed/audiostream/FFT.kt +99 -0
  19. package/android/src/main/java/net/siteed/audiostream/Features.kt +98 -0
  20. package/android/src/main/java/net/siteed/audiostream/NotificationConfig.kt +70 -0
  21. package/android/src/main/java/net/siteed/audiostream/PermissionUtils.kt +59 -0
  22. package/android/src/main/java/net/siteed/audiostream/RecordingActionReceiver.kt +59 -0
  23. package/android/src/main/java/net/siteed/audiostream/RecordingConfig.kt +205 -0
  24. package/android/src/main/java/net/siteed/audiostream/WaveformConfig.kt +19 -0
  25. package/android/src/main/java/net/siteed/audiostream/WaveformRenderer.kt +159 -0
  26. package/android/src/main/res/drawable/ic_default_action_icon.xml +16 -0
  27. package/android/src/main/res/drawable/ic_microphone.xml +13 -0
  28. package/android/src/main/res/drawable/ic_pause.xml +10 -0
  29. package/android/src/main/res/drawable/ic_play.xml +10 -0
  30. package/android/src/main/res/drawable/ic_stop.xml +10 -0
  31. package/android/src/main/res/layout/notification_recording.xml +37 -0
  32. package/android/src/main/test/java/net/siteed/audiostream/AudioProcessorTest.kt +56 -0
  33. package/app.plugin.js +1 -0
  34. package/build/AudioAnalysis/AudioAnalysis.types.d.ts +179 -0
  35. package/build/AudioAnalysis/AudioAnalysis.types.d.ts.map +1 -0
  36. package/build/AudioAnalysis/AudioAnalysis.types.js +3 -0
  37. package/build/AudioAnalysis/AudioAnalysis.types.js.map +1 -0
  38. package/build/AudioAnalysis/extractAudioAnalysis.d.ts +68 -0
  39. package/build/AudioAnalysis/extractAudioAnalysis.d.ts.map +1 -0
  40. package/build/AudioAnalysis/extractAudioAnalysis.js +203 -0
  41. package/build/AudioAnalysis/extractAudioAnalysis.js.map +1 -0
  42. package/build/AudioAnalysis/extractAudioData.d.ts +3 -0
  43. package/build/AudioAnalysis/extractAudioData.d.ts.map +1 -0
  44. package/build/AudioAnalysis/extractAudioData.js +5 -0
  45. package/build/AudioAnalysis/extractAudioData.js.map +1 -0
  46. package/build/AudioAnalysis/extractMelSpectrogram.d.ts +14 -0
  47. package/build/AudioAnalysis/extractMelSpectrogram.d.ts.map +1 -0
  48. package/build/AudioAnalysis/extractMelSpectrogram.js +85 -0
  49. package/build/AudioAnalysis/extractMelSpectrogram.js.map +1 -0
  50. package/build/AudioAnalysis/extractPreview.d.ts +11 -0
  51. package/build/AudioAnalysis/extractPreview.d.ts.map +1 -0
  52. package/build/AudioAnalysis/extractPreview.js +25 -0
  53. package/build/AudioAnalysis/extractPreview.js.map +1 -0
  54. package/build/AudioAnalysis/extractWaveform.d.ts +8 -0
  55. package/build/AudioAnalysis/extractWaveform.d.ts.map +1 -0
  56. package/build/AudioAnalysis/extractWaveform.js +11 -0
  57. package/build/AudioAnalysis/extractWaveform.js.map +1 -0
  58. package/build/AudioRecorder.provider.d.ts +11 -0
  59. package/build/AudioRecorder.provider.d.ts.map +1 -0
  60. package/build/AudioRecorder.provider.js +37 -0
  61. package/build/AudioRecorder.provider.js.map +1 -0
  62. package/build/ExpoAudioStream.native.d.ts +3 -0
  63. package/build/ExpoAudioStream.native.d.ts.map +1 -0
  64. package/build/ExpoAudioStream.native.js +6 -0
  65. package/build/ExpoAudioStream.native.js.map +1 -0
  66. package/build/ExpoAudioStream.types.d.ts +532 -0
  67. package/build/ExpoAudioStream.types.d.ts.map +1 -0
  68. package/build/ExpoAudioStream.types.js +2 -0
  69. package/build/ExpoAudioStream.types.js.map +1 -0
  70. package/build/ExpoAudioStream.web.d.ts +59 -0
  71. package/build/ExpoAudioStream.web.d.ts.map +1 -0
  72. package/build/ExpoAudioStream.web.js +285 -0
  73. package/build/ExpoAudioStream.web.js.map +1 -0
  74. package/build/ExpoAudioStreamModule.d.ts +3 -0
  75. package/build/ExpoAudioStreamModule.d.ts.map +1 -0
  76. package/build/ExpoAudioStreamModule.js +693 -0
  77. package/build/ExpoAudioStreamModule.js.map +1 -0
  78. package/build/WebRecorder.web.d.ts +119 -0
  79. package/build/WebRecorder.web.d.ts.map +1 -0
  80. package/build/WebRecorder.web.js +436 -0
  81. package/build/WebRecorder.web.js.map +1 -0
  82. package/build/constants.d.ts +11 -0
  83. package/build/constants.d.ts.map +1 -0
  84. package/build/constants.js +14 -0
  85. package/build/constants.js.map +1 -0
  86. package/build/events.d.ts +26 -0
  87. package/build/events.d.ts.map +1 -0
  88. package/build/events.js +21 -0
  89. package/build/events.js.map +1 -0
  90. package/build/index.d.ts +15 -0
  91. package/build/index.d.ts.map +1 -0
  92. package/build/index.js +14 -0
  93. package/build/index.js.map +1 -0
  94. package/build/trimAudio.d.ts +25 -0
  95. package/build/trimAudio.d.ts.map +1 -0
  96. package/build/trimAudio.js +67 -0
  97. package/build/trimAudio.js.map +1 -0
  98. package/build/useAudioRecorder.d.ts +21 -0
  99. package/build/useAudioRecorder.d.ts.map +1 -0
  100. package/build/useAudioRecorder.js +427 -0
  101. package/build/useAudioRecorder.js.map +1 -0
  102. package/build/utils/BlobFix.d.ts +9 -0
  103. package/build/utils/BlobFix.d.ts.map +1 -0
  104. package/build/utils/BlobFix.js +498 -0
  105. package/build/utils/BlobFix.js.map +1 -0
  106. package/build/utils/audioProcessing.d.ts +24 -0
  107. package/build/utils/audioProcessing.d.ts.map +1 -0
  108. package/build/utils/audioProcessing.js +133 -0
  109. package/build/utils/audioProcessing.js.map +1 -0
  110. package/build/utils/concatenateBuffers.d.ts +8 -0
  111. package/build/utils/concatenateBuffers.d.ts.map +1 -0
  112. package/build/utils/concatenateBuffers.js +21 -0
  113. package/build/utils/concatenateBuffers.js.map +1 -0
  114. package/build/utils/convertPCMToFloat32.d.ts +13 -0
  115. package/build/utils/convertPCMToFloat32.d.ts.map +1 -0
  116. package/build/utils/convertPCMToFloat32.js +120 -0
  117. package/build/utils/convertPCMToFloat32.js.map +1 -0
  118. package/build/utils/encodingToBitDepth.d.ts +5 -0
  119. package/build/utils/encodingToBitDepth.d.ts.map +1 -0
  120. package/build/utils/encodingToBitDepth.js +13 -0
  121. package/build/utils/encodingToBitDepth.js.map +1 -0
  122. package/build/utils/getWavFileInfo.d.ts +26 -0
  123. package/build/utils/getWavFileInfo.d.ts.map +1 -0
  124. package/build/utils/getWavFileInfo.js +92 -0
  125. package/build/utils/getWavFileInfo.js.map +1 -0
  126. package/build/utils/writeWavHeader.d.ts +49 -0
  127. package/build/utils/writeWavHeader.d.ts.map +1 -0
  128. package/build/utils/writeWavHeader.js +91 -0
  129. package/build/utils/writeWavHeader.js.map +1 -0
  130. package/build/workers/InlineFeaturesExtractor.web.d.ts +2 -0
  131. package/build/workers/InlineFeaturesExtractor.web.d.ts.map +1 -0
  132. package/build/workers/InlineFeaturesExtractor.web.js +828 -0
  133. package/build/workers/InlineFeaturesExtractor.web.js.map +1 -0
  134. package/build/workers/inlineAudioWebWorker.web.d.ts +2 -0
  135. package/build/workers/inlineAudioWebWorker.web.d.ts.map +1 -0
  136. package/build/workers/inlineAudioWebWorker.web.js +157 -0
  137. package/build/workers/inlineAudioWebWorker.web.js.map +1 -0
  138. package/expo-module.config.json +9 -0
  139. package/ios/AudioAnalysisData.swift +74 -0
  140. package/ios/AudioNotificationManager.swift +135 -0
  141. package/ios/AudioProcessingHelpers.swift +743 -0
  142. package/ios/AudioProcessor.swift +1313 -0
  143. package/ios/AudioStreamError.swift +7 -0
  144. package/ios/AudioStreamManager.swift +1708 -0
  145. package/ios/AudioStreamManagerDelegate.swift +16 -0
  146. package/ios/DataPoint.swift +54 -0
  147. package/ios/DecodingConfig.swift +47 -0
  148. package/ios/ExpoAudioStream.podspec +27 -0
  149. package/ios/ExpoAudioStreamModule.swift +805 -0
  150. package/ios/FFT.swift +62 -0
  151. package/ios/Features.swift +95 -0
  152. package/ios/Logger.swift +7 -0
  153. package/ios/NotificationExtension.swift +15 -0
  154. package/ios/RecordingResult.swift +22 -0
  155. package/ios/RecordingSettings.swift +265 -0
  156. package/ios/WaveformExtractor.swift +105 -0
  157. package/package.json +128 -0
  158. package/plugin/build/index.d.ts +21 -0
  159. package/plugin/build/index.js +192 -0
  160. package/plugin/src/index.ts +279 -0
  161. package/plugin/tsconfig.json +10 -0
  162. package/plugin/tsconfig.tsbuildinfo +1 -0
  163. package/src/AudioAnalysis/AudioAnalysis.types.ts +202 -0
  164. package/src/AudioAnalysis/extractAudioAnalysis.ts +333 -0
  165. package/src/AudioAnalysis/extractAudioData.ts +6 -0
  166. package/src/AudioAnalysis/extractMelSpectrogram.ts +144 -0
  167. package/src/AudioAnalysis/extractPreview.ts +34 -0
  168. package/src/AudioAnalysis/extractWaveform.ts +22 -0
  169. package/src/AudioRecorder.provider.tsx +54 -0
  170. package/src/ExpoAudioStream.native.ts +6 -0
  171. package/src/ExpoAudioStream.types.ts +641 -0
  172. package/src/ExpoAudioStream.web.ts +359 -0
  173. package/src/ExpoAudioStreamModule.ts +967 -0
  174. package/src/WebRecorder.web.ts +580 -0
  175. package/src/constants.ts +18 -0
  176. package/src/events.ts +60 -0
  177. package/src/index.ts +36 -0
  178. package/src/trimAudio.ts +90 -0
  179. package/src/useAudioRecorder.tsx +620 -0
  180. package/src/utils/BlobFix.ts +559 -0
  181. package/src/utils/audioProcessing.ts +205 -0
  182. package/src/utils/concatenateBuffers.ts +24 -0
  183. package/src/utils/convertPCMToFloat32.ts +170 -0
  184. package/src/utils/encodingToBitDepth.ts +18 -0
  185. package/src/utils/getWavFileInfo.ts +132 -0
  186. package/src/utils/writeWavHeader.ts +114 -0
  187. package/src/workers/InlineFeaturesExtractor.web.tsx +827 -0
  188. package/src/workers/inlineAudioWebWorker.web.tsx +156 -0
@@ -0,0 +1,559 @@
1
+ /*
2
+ * There is a bug where `navigator.mediaDevices.getUserMedia` + `MediaRecorder`
3
+ * creates WEBM files without duration metadata. See:
4
+ * - https://bugs.chromium.org/p/chromium/issues/detail?id=642012
5
+ * - https://stackoverflow.com/a/39971175/13989043
6
+ *
7
+ * This file contains a function that fixes the duration metadata of a WEBM file.
8
+ * - Answer found: https://stackoverflow.com/a/75218309/13989043
9
+ * - Code adapted from: https://github.com/mat-sz/webm-fix-duration
10
+ * (forked from https://github.com/yusitnikov/fix-webm-duration)
11
+ */
12
+
13
+ /*
14
+ * This is the list of possible WEBM file sections by their IDs.
15
+ * Possible types: Container, Binary, Uint, Int, String, Float, Date
16
+ */
17
+ interface Section {
18
+ name: string
19
+ type: string
20
+ }
21
+
22
+ const sections: Record<number, Section> = {
23
+ 0xa45dfa3: { name: 'EBML', type: 'Container' },
24
+ 0x286: { name: 'EBMLVersion', type: 'Uint' },
25
+ 0x2f7: { name: 'EBMLReadVersion', type: 'Uint' },
26
+ 0x2f2: { name: 'EBMLMaxIDLength', type: 'Uint' },
27
+ 0x2f3: { name: 'EBMLMaxSizeLength', type: 'Uint' },
28
+ 0x282: { name: 'DocType', type: 'String' },
29
+ 0x287: { name: 'DocTypeVersion', type: 'Uint' },
30
+ 0x285: { name: 'DocTypeReadVersion', type: 'Uint' },
31
+ 0x6c: { name: 'Void', type: 'Binary' },
32
+ 0x3f: { name: 'CRC-32', type: 'Binary' },
33
+ 0xb538667: { name: 'SignatureSlot', type: 'Container' },
34
+ 0x3e8a: { name: 'SignatureAlgo', type: 'Uint' },
35
+ 0x3e9a: { name: 'SignatureHash', type: 'Uint' },
36
+ 0x3ea5: { name: 'SignaturePublicKey', type: 'Binary' },
37
+ 0x3eb5: { name: 'Signature', type: 'Binary' },
38
+ 0x3e5b: { name: 'SignatureElements', type: 'Container' },
39
+ 0x3e7b: { name: 'SignatureElementList', type: 'Container' },
40
+ 0x2532: { name: 'SignedElement', type: 'Binary' },
41
+ 0x8538067: { name: 'Segment', type: 'Container' },
42
+ 0x14d9b74: { name: 'SeekHead', type: 'Container' },
43
+ 0xdbb: { name: 'Seek', type: 'Container' },
44
+ 0x13ab: { name: 'SeekID', type: 'Binary' },
45
+ 0x13ac: { name: 'SeekPosition', type: 'Uint' },
46
+ 0x549a966: { name: 'Info', type: 'Container' },
47
+ 0x33a4: { name: 'SegmentUID', type: 'Binary' },
48
+ 0x3384: { name: 'SegmentFilename', type: 'String' },
49
+ 0x1cb923: { name: 'PrevUID', type: 'Binary' },
50
+ 0x1c83ab: { name: 'PrevFilename', type: 'String' },
51
+ 0x1eb923: { name: 'NextUID', type: 'Binary' },
52
+ 0x1e83bb: { name: 'NextFilename', type: 'String' },
53
+ 0x444: { name: 'SegmentFamily', type: 'Binary' },
54
+ 0x2924: { name: 'ChapterTranslate', type: 'Container' },
55
+ 0x29fc: { name: 'ChapterTranslateEditionUID', type: 'Uint' },
56
+ 0x29bf: { name: 'ChapterTranslateCodec', type: 'Uint' },
57
+ 0x29a5: { name: 'ChapterTranslateID', type: 'Binary' },
58
+ 0xad7b1: { name: 'TimecodeScale', type: 'Uint' },
59
+ 0x489: { name: 'Duration', type: 'Float' },
60
+ 0x461: { name: 'DateUTC', type: 'Date' },
61
+ 0x3ba9: { name: 'Title', type: 'String' },
62
+ 0xd80: { name: 'MuxingApp', type: 'String' },
63
+ 0x1741: { name: 'WritingApp', type: 'String' },
64
+ // 0xf43b675: { name: 'Cluster', type: 'Container' },
65
+ 0x67: { name: 'Timecode', type: 'Uint' },
66
+ 0x1854: { name: 'SilentTracks', type: 'Container' },
67
+ 0x18d7: { name: 'SilentTrackNumber', type: 'Uint' },
68
+ 0x27: { name: 'Position', type: 'Uint' },
69
+ 0x2b: { name: 'PrevSize', type: 'Uint' },
70
+ 0x23: { name: 'SimpleBlock', type: 'Binary' },
71
+ 0x20: { name: 'BlockGroup', type: 'Container' },
72
+ 0x21: { name: 'Block', type: 'Binary' },
73
+ 0x22: { name: 'BlockVirtual', type: 'Binary' },
74
+ 0x35a1: { name: 'BlockAdditions', type: 'Container' },
75
+ 0x26: { name: 'BlockMore', type: 'Container' },
76
+ 0x6e: { name: 'BlockAddID', type: 'Uint' },
77
+ 0x25: { name: 'BlockAdditional', type: 'Binary' },
78
+ 0x1b: { name: 'BlockDuration', type: 'Uint' },
79
+ 0x7a: { name: 'ReferencePriority', type: 'Uint' },
80
+ 0x7b: { name: 'ReferenceBlock', type: 'Int' },
81
+ 0x7d: { name: 'ReferenceVirtual', type: 'Int' },
82
+ 0x24: { name: 'CodecState', type: 'Binary' },
83
+ 0x35a2: { name: 'DiscardPadding', type: 'Int' },
84
+ 0xe: { name: 'Slices', type: 'Container' },
85
+ 0x68: { name: 'TimeSlice', type: 'Container' },
86
+ 0x4c: { name: 'LaceNumber', type: 'Uint' },
87
+ 0x4d: { name: 'FrameNumber', type: 'Uint' },
88
+ 0x4b: { name: 'BlockAdditionID', type: 'Uint' },
89
+ 0x4e: { name: 'Delay', type: 'Uint' },
90
+ 0x4f: { name: 'SliceDuration', type: 'Uint' },
91
+ 0x48: { name: 'ReferenceFrame', type: 'Container' },
92
+ 0x49: { name: 'ReferenceOffset', type: 'Uint' },
93
+ 0x4a: { name: 'ReferenceTimeCode', type: 'Uint' },
94
+ 0x2f: { name: 'EncryptedBlock', type: 'Binary' },
95
+ 0x654ae6b: { name: 'Tracks', type: 'Container' },
96
+ 0x2e: { name: 'TrackEntry', type: 'Container' },
97
+ 0x57: { name: 'TrackNumber', type: 'Uint' },
98
+ 0x33c5: { name: 'TrackUID', type: 'Uint' },
99
+ 0x3: { name: 'TrackType', type: 'Uint' },
100
+ 0x39: { name: 'FlagEnabled', type: 'Uint' },
101
+ 0x8: { name: 'FlagDefault', type: 'Uint' },
102
+ 0x15aa: { name: 'FlagForced', type: 'Uint' },
103
+ 0x1c: { name: 'FlagLacing', type: 'Uint' },
104
+ 0x2de7: { name: 'MinCache', type: 'Uint' },
105
+ 0x2df8: { name: 'MaxCache', type: 'Uint' },
106
+ 0x3e383: { name: 'DefaultDuration', type: 'Uint' },
107
+ 0x34e7a: { name: 'DefaultDecodedFieldDuration', type: 'Uint' },
108
+ 0x3314f: { name: 'TrackTimecodeScale', type: 'Float' },
109
+ 0x137f: { name: 'TrackOffset', type: 'Int' },
110
+ 0x15ee: { name: 'MaxBlockAdditionID', type: 'Uint' },
111
+ 0x136e: { name: 'Name', type: 'String' },
112
+ 0x2b59c: { name: 'Language', type: 'String' },
113
+ 0x6: { name: 'CodecID', type: 'String' },
114
+ 0x23a2: { name: 'CodecPrivate', type: 'Binary' },
115
+ 0x58688: { name: 'CodecName', type: 'String' },
116
+ 0x3446: { name: 'AttachmentLink', type: 'Uint' },
117
+ 0x1a9697: { name: 'CodecSettings', type: 'String' },
118
+ 0x1b4040: { name: 'CodecInfoURL', type: 'String' },
119
+ 0x6b240: { name: 'CodecDownloadURL', type: 'String' },
120
+ 0x2a: { name: 'CodecDecodeAll', type: 'Uint' },
121
+ 0x2fab: { name: 'TrackOverlay', type: 'Uint' },
122
+ 0x16aa: { name: 'CodecDelay', type: 'Uint' },
123
+ 0x16bb: { name: 'SeekPreRoll', type: 'Uint' },
124
+ 0x2624: { name: 'TrackTranslate', type: 'Container' },
125
+ 0x26fc: { name: 'TrackTranslateEditionUID', type: 'Uint' },
126
+ 0x26bf: { name: 'TrackTranslateCodec', type: 'Uint' },
127
+ 0x26a5: { name: 'TrackTranslateTrackID', type: 'Binary' },
128
+ 0x60: { name: 'Video', type: 'Container' },
129
+ 0x1a: { name: 'FlagInterlaced', type: 'Uint' },
130
+ 0x13b8: { name: 'StereoMode', type: 'Uint' },
131
+ 0x13c0: { name: 'AlphaMode', type: 'Uint' },
132
+ 0x13b9: { name: 'OldStereoMode', type: 'Uint' },
133
+ 0x30: { name: 'PixelWidth', type: 'Uint' },
134
+ 0x3a: { name: 'PixelHeight', type: 'Uint' },
135
+ 0x14aa: { name: 'PixelCropBottom', type: 'Uint' },
136
+ 0x14bb: { name: 'PixelCropTop', type: 'Uint' },
137
+ 0x14cc: { name: 'PixelCropLeft', type: 'Uint' },
138
+ 0x14dd: { name: 'PixelCropRight', type: 'Uint' },
139
+ 0x14b0: { name: 'DisplayWidth', type: 'Uint' },
140
+ 0x14ba: { name: 'DisplayHeight', type: 'Uint' },
141
+ 0x14b2: { name: 'DisplayUnit', type: 'Uint' },
142
+ 0x14b3: { name: 'AspectRatioType', type: 'Uint' },
143
+ 0xeb524: { name: 'ColourSpace', type: 'Binary' },
144
+ 0xfb523: { name: 'GammaValue', type: 'Float' },
145
+ 0x383e3: { name: 'FrameRate', type: 'Float' },
146
+ 0x61: { name: 'Audio', type: 'Container' },
147
+ 0x35: { name: 'SamplingFrequency', type: 'Float' },
148
+ 0x38b5: { name: 'OutputSamplingFrequency', type: 'Float' },
149
+ 0x1f: { name: 'Channels', type: 'Uint' },
150
+ 0x3d7b: { name: 'ChannelPositions', type: 'Binary' },
151
+ 0x2264: { name: 'BitDepth', type: 'Uint' },
152
+ 0x62: { name: 'TrackOperation', type: 'Container' },
153
+ 0x63: { name: 'TrackCombinePlanes', type: 'Container' },
154
+ 0x64: { name: 'TrackPlane', type: 'Container' },
155
+ 0x65: { name: 'TrackPlaneUID', type: 'Uint' },
156
+ 0x66: { name: 'TrackPlaneType', type: 'Uint' },
157
+ 0x69: { name: 'TrackJoinBlocks', type: 'Container' },
158
+ 0x6d: { name: 'TrackJoinUID', type: 'Uint' },
159
+ 0x40: { name: 'TrickTrackUID', type: 'Uint' },
160
+ 0x41: { name: 'TrickTrackSegmentUID', type: 'Binary' },
161
+ 0x46: { name: 'TrickTrackFlag', type: 'Uint' },
162
+ 0x47: { name: 'TrickMasterTrackUID', type: 'Uint' },
163
+ 0x44: { name: 'TrickMasterTrackSegmentUID', type: 'Binary' },
164
+ 0x2d80: { name: 'ContentEncodings', type: 'Container' },
165
+ 0x2240: { name: 'ContentEncoding', type: 'Container' },
166
+ 0x1031: { name: 'ContentEncodingOrder', type: 'Uint' },
167
+ 0x1032: { name: 'ContentEncodingScope', type: 'Uint' },
168
+ 0x1033: { name: 'ContentEncodingType', type: 'Uint' },
169
+ 0x1034: { name: 'ContentCompression', type: 'Container' },
170
+ 0x254: { name: 'ContentCompAlgo', type: 'Uint' },
171
+ 0x255: { name: 'ContentCompSettings', type: 'Binary' },
172
+ 0x1035: { name: 'ContentEncryption', type: 'Container' },
173
+ 0x7e1: { name: 'ContentEncAlgo', type: 'Uint' },
174
+ 0x7e2: { name: 'ContentEncKeyID', type: 'Binary' },
175
+ 0x7e3: { name: 'ContentSignature', type: 'Binary' },
176
+ 0x7e4: { name: 'ContentSigKeyID', type: 'Binary' },
177
+ 0x7e5: { name: 'ContentSigAlgo', type: 'Uint' },
178
+ 0x7e6: { name: 'ContentSigHashAlgo', type: 'Uint' },
179
+ 0xc53bb6b: { name: 'Cues', type: 'Container' },
180
+ 0x3b: { name: 'CuePoint', type: 'Container' },
181
+ 0x33: { name: 'CueTime', type: 'Uint' },
182
+ 0x37: { name: 'CueTrackPositions', type: 'Container' },
183
+ 0x77: { name: 'CueTrack', type: 'Uint' },
184
+ 0x71: { name: 'CueClusterPosition', type: 'Uint' },
185
+ 0x70: { name: 'CueRelativePosition', type: 'Uint' },
186
+ 0x32: { name: 'CueDuration', type: 'Uint' },
187
+ 0x1378: { name: 'CueBlockNumber', type: 'Uint' },
188
+ 0x6a: { name: 'CueCodecState', type: 'Uint' },
189
+ 0x5b: { name: 'CueReference', type: 'Container' },
190
+ 0x16: { name: 'CueRefTime', type: 'Uint' },
191
+ 0x17: { name: 'CueRefCluster', type: 'Uint' },
192
+ 0x135f: { name: 'CueRefNumber', type: 'Uint' },
193
+ 0x6b: { name: 'CueRefCodecState', type: 'Uint' },
194
+ 0x941a469: { name: 'Attachments', type: 'Container' },
195
+ 0x21a7: { name: 'AttachedFile', type: 'Container' },
196
+ 0x67e: { name: 'FileDescription', type: 'String' },
197
+ 0x66e: { name: 'FileName', type: 'String' },
198
+ 0x660: { name: 'FileMimeType', type: 'String' },
199
+ 0x65c: { name: 'FileData', type: 'Binary' },
200
+ 0x6ae: { name: 'FileUID', type: 'Uint' },
201
+ 0x675: { name: 'FileReferral', type: 'Binary' },
202
+ 0x661: { name: 'FileUsedStartTime', type: 'Uint' },
203
+ 0x662: { name: 'FileUsedEndTime', type: 'Uint' },
204
+ 0x43a770: { name: 'Chapters', type: 'Container' },
205
+ 0x5b9: { name: 'EditionEntry', type: 'Container' },
206
+ 0x5bc: { name: 'EditionUID', type: 'Uint' },
207
+ 0x5bd: { name: 'EditionFlagHidden', type: 'Uint' },
208
+ 0x5db: { name: 'EditionFlagDefault', type: 'Uint' },
209
+ 0x5dd: { name: 'EditionFlagOrdered', type: 'Uint' },
210
+ 0x36: { name: 'ChapterAtom', type: 'Container' },
211
+ 0x33c4: { name: 'ChapterUID', type: 'Uint' },
212
+ 0x1654: { name: 'ChapterStringUID', type: 'String' },
213
+ 0x11: { name: 'ChapterTimeStart', type: 'Uint' },
214
+ 0x12: { name: 'ChapterTimeEnd', type: 'Uint' },
215
+ 0x18: { name: 'ChapterFlagHidden', type: 'Uint' },
216
+ 0x598: { name: 'ChapterFlagEnabled', type: 'Uint' },
217
+ 0x2e67: { name: 'ChapterSegmentUID', type: 'Binary' },
218
+ 0x2ebc: { name: 'ChapterSegmentEditionUID', type: 'Uint' },
219
+ 0x23c3: { name: 'ChapterPhysicalEquiv', type: 'Uint' },
220
+ 0xf: { name: 'ChapterTrack', type: 'Container' },
221
+ 0x9: { name: 'ChapterTrackNumber', type: 'Uint' },
222
+ 0x0: { name: 'ChapterDisplay', type: 'Container' },
223
+ 0x5: { name: 'ChapString', type: 'String' },
224
+ 0x37c: { name: 'ChapLanguage', type: 'String' },
225
+ 0x37e: { name: 'ChapCountry', type: 'String' },
226
+ 0x2944: { name: 'ChapProcess', type: 'Container' },
227
+ 0x2955: { name: 'ChapProcessCodecID', type: 'Uint' },
228
+ 0x50d: { name: 'ChapProcessPrivate', type: 'Binary' },
229
+ 0x2911: { name: 'ChapProcessCommand', type: 'Container' },
230
+ 0x2922: { name: 'ChapProcessTime', type: 'Uint' },
231
+ 0x2933: { name: 'ChapProcessData', type: 'Binary' },
232
+ 0x254c367: { name: 'Tags', type: 'Container' },
233
+ 0x3373: { name: 'Tag', type: 'Container' },
234
+ 0x23c0: { name: 'Targets', type: 'Container' },
235
+ 0x28ca: { name: 'TargetTypeValue', type: 'Uint' },
236
+ 0x23ca: { name: 'TargetType', type: 'String' },
237
+ 0x23c5: { name: 'TagTrackUID', type: 'Uint' },
238
+ 0x23c9: { name: 'TagEditionUID', type: 'Uint' },
239
+ 0x23c4: { name: 'TagChapterUID', type: 'Uint' },
240
+ 0x23c6: { name: 'TagAttachmentUID', type: 'Uint' },
241
+ 0x27c8: { name: 'SimpleTag', type: 'Container' },
242
+ 0x5a3: { name: 'TagName', type: 'String' },
243
+ 0x47a: { name: 'TagLanguage', type: 'String' },
244
+ 0x484: { name: 'TagDefault', type: 'Uint' },
245
+ 0x487: { name: 'TagString', type: 'String' },
246
+ 0x485: { name: 'TagBinary', type: 'Binary' },
247
+ }
248
+
249
+ class WebmBase<T> {
250
+ source?: Uint8Array
251
+ data?: T
252
+
253
+ constructor(
254
+ private name = 'Unknown',
255
+ private type = 'Unknown'
256
+ ) {}
257
+
258
+ updateBySource() {}
259
+
260
+ setSource(source: Uint8Array) {
261
+ this.source = source
262
+ this.updateBySource()
263
+ }
264
+
265
+ updateByData() {}
266
+
267
+ setData(data: T) {
268
+ this.data = data
269
+ this.updateByData()
270
+ }
271
+ }
272
+
273
+ class WebmUint extends WebmBase<string> {
274
+ constructor(name: string, type: string) {
275
+ super(name, type || 'Uint')
276
+ }
277
+
278
+ updateBySource() {
279
+ // use hex representation of a number instead of number value
280
+ this.data = ''
281
+ for (let i = 0; i < this.source!.length; i++) {
282
+ const hex = this.source![i].toString(16)
283
+ this.data += padHex(hex)
284
+ }
285
+ }
286
+
287
+ updateByData() {
288
+ const length = this.data!.length / 2
289
+ this.source = new Uint8Array(length)
290
+ for (let i = 0; i < length; i++) {
291
+ const hex = this.data!.substr(i * 2, 2)
292
+ this.source[i] = parseInt(hex, 16)
293
+ }
294
+ }
295
+
296
+ getValue() {
297
+ return parseInt(this.data!, 16)
298
+ }
299
+
300
+ setValue(value: number) {
301
+ this.setData(padHex(value.toString(16)))
302
+ }
303
+ }
304
+
305
+ function padHex(hex: string) {
306
+ return hex.length % 2 === 1 ? '0' + hex : hex
307
+ }
308
+
309
+ class WebmFloat extends WebmBase<number> {
310
+ constructor(name: string, type: string) {
311
+ super(name, type || 'Float')
312
+ }
313
+
314
+ getFloatArrayType() {
315
+ return this.source && this.source.length === 4
316
+ ? Float32Array
317
+ : Float64Array
318
+ }
319
+ updateBySource() {
320
+ const byteArray = this.source!.reverse()
321
+ const floatArrayType = this.getFloatArrayType()
322
+ const buffer = new ArrayBuffer(byteArray.buffer.byteLength)
323
+ new Uint8Array(buffer).set(new Uint8Array(byteArray.buffer))
324
+ const floatArray = new floatArrayType(buffer)
325
+ this.data! = floatArray[0]
326
+ }
327
+ updateByData() {
328
+ const floatArrayType = this.getFloatArrayType()
329
+ const floatArray = new floatArrayType([this.data!])
330
+ const byteArray = new Uint8Array(floatArray.buffer)
331
+ this.source = byteArray.reverse()
332
+ }
333
+ getValue() {
334
+ return this.data
335
+ }
336
+ setValue(value: number) {
337
+ this.setData(value)
338
+ }
339
+ }
340
+
341
+ interface ContainerData {
342
+ id: number
343
+ idHex?: string
344
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
345
+ data: WebmBase<any>
346
+ }
347
+
348
+ class WebmContainer extends WebmBase<ContainerData[]> {
349
+ offset: number = 0
350
+ data: ContainerData[] = []
351
+
352
+ constructor(name: string, type: string) {
353
+ super(name, type || 'Container')
354
+ }
355
+
356
+ readByte() {
357
+ return this.source![this.offset++]
358
+ }
359
+ readUint() {
360
+ const firstByte = this.readByte()
361
+ const bytes = 8 - firstByte.toString(2).length
362
+ let value = firstByte - (1 << (7 - bytes))
363
+ for (let i = 0; i < bytes; i++) {
364
+ // don't use bit operators to support x86
365
+ value *= 256
366
+ value += this.readByte()
367
+ }
368
+ return value
369
+ }
370
+ updateBySource() {
371
+ let end: number | undefined = undefined
372
+ this.data = []
373
+ for (
374
+ this.offset = 0;
375
+ this.offset < this.source!.length;
376
+ this.offset = end
377
+ ) {
378
+ const id = this.readUint()
379
+ const len = this.readUint()
380
+ end = Math.min(this.offset + len, this.source!.length)
381
+ const data = this.source!.slice(this.offset, end)
382
+
383
+ const info = sections[id] || { name: 'Unknown', type: 'Unknown' }
384
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
385
+ let ctr: any = WebmBase
386
+ switch (info.type) {
387
+ case 'Container':
388
+ ctr = WebmContainer
389
+ break
390
+ case 'Uint':
391
+ ctr = WebmUint
392
+ break
393
+ case 'Float':
394
+ ctr = WebmFloat
395
+ break
396
+ }
397
+ const section = new ctr(info.name, info.type)
398
+ section.setSource(data)
399
+ this.data.push({
400
+ id,
401
+ idHex: id.toString(16),
402
+ data: section,
403
+ })
404
+ }
405
+ }
406
+ writeUint(x: number, draft = false) {
407
+ let flag = 0x80
408
+ let bytes = 1
409
+ // eslint-disable-next-line no-empty
410
+ for (; x >= flag && bytes < 8; bytes++, flag *= 0x80) {}
411
+
412
+ if (!draft) {
413
+ let value = flag + x
414
+ for (let i = bytes - 1; i >= 0; i--) {
415
+ // don't use bit operators to support x86
416
+ const c = value % 256
417
+ this.source![this.offset! + i] = c
418
+ value = (value - c) / 256
419
+ }
420
+ }
421
+
422
+ this.offset += bytes
423
+ }
424
+
425
+ writeSections(draft = false) {
426
+ this.offset = 0
427
+ for (let i = 0; i < this.data.length; i++) {
428
+ const section = this.data[i],
429
+ content = section.data.source,
430
+ contentLength = content!.length
431
+ this.writeUint(section.id, draft)
432
+ this.writeUint(contentLength, draft)
433
+ if (!draft) {
434
+ this.source!.set(content!, this.offset)
435
+ }
436
+ this.offset += contentLength
437
+ }
438
+ return this.offset
439
+ }
440
+
441
+ updateByData() {
442
+ // run without accessing this.source to determine total length - need to know it to create Uint8Array
443
+ const length = this.writeSections(true)
444
+ this.source = new Uint8Array(length)
445
+ // now really write data
446
+ this.writeSections()
447
+ }
448
+
449
+ getSectionById(id: number) {
450
+ for (let i = 0; i < this.data.length; i++) {
451
+ const section = this.data[i]
452
+ if (section.id === id) {
453
+ return section.data
454
+ }
455
+ }
456
+
457
+ return undefined
458
+ }
459
+ }
460
+
461
+ class WebmFile extends WebmContainer {
462
+ constructor(source: Uint8Array) {
463
+ super('File', 'File')
464
+ this.setSource(source)
465
+ }
466
+
467
+ fixDuration(duration: number) {
468
+ const segmentSection = this.getSectionById(0x8538067) as WebmContainer
469
+ if (!segmentSection) {
470
+ return false
471
+ }
472
+
473
+ const infoSection = segmentSection.getSectionById(
474
+ 0x549a966
475
+ ) as WebmContainer
476
+ if (!infoSection) {
477
+ return false
478
+ }
479
+
480
+ const timeScaleSection = infoSection.getSectionById(
481
+ 0xad7b1
482
+ ) as WebmFloat
483
+ if (!timeScaleSection) {
484
+ return false
485
+ }
486
+
487
+ let durationSection = infoSection.getSectionById(0x489) as WebmFloat
488
+ if (durationSection) {
489
+ if (durationSection.getValue()! <= 0) {
490
+ durationSection.setValue(duration)
491
+ } else {
492
+ return false
493
+ }
494
+ } else {
495
+ // append Duration section
496
+ durationSection = new WebmFloat('Duration', 'Float')
497
+ durationSection.setValue(duration)
498
+ infoSection.data.push({
499
+ id: 0x489,
500
+ data: durationSection,
501
+ })
502
+ }
503
+
504
+ // set default time scale to 1 millisecond (1000000 nanoseconds)
505
+ timeScaleSection.setValue(1000000)
506
+ infoSection.updateByData()
507
+ segmentSection.updateByData()
508
+ this.updateByData()
509
+
510
+ return true
511
+ }
512
+
513
+ toBlob(type = 'video/webm') {
514
+ const buffer = new ArrayBuffer(this.source!.buffer.byteLength)
515
+ new Uint8Array(buffer).set(new Uint8Array(this.source!.buffer))
516
+ return new Blob([buffer], { type })
517
+ }
518
+ }
519
+
520
+ /**
521
+ * Fixes duration on MediaRecorder output.
522
+ * @param blob Input Blob with incorrect duration.
523
+ * @param duration Correct duration (in milliseconds).
524
+ * @param type Output blob mimetype (default: video/webm).
525
+ * @returns
526
+ */
527
+ export const webmFixDuration = (
528
+ blob: Blob,
529
+ duration: number,
530
+ type = 'video/webm'
531
+ ): Promise<Blob> => {
532
+ return new Promise((resolve, reject) => {
533
+ try {
534
+ const reader = new FileReader()
535
+
536
+ reader.addEventListener('loadend', () => {
537
+ try {
538
+ const result = reader.result as ArrayBuffer
539
+ const file = new WebmFile(new Uint8Array(result))
540
+ if (file.fixDuration(duration)) {
541
+ resolve(file.toBlob(type))
542
+ } else {
543
+ resolve(blob)
544
+ }
545
+ } catch (ex) {
546
+ reject(ex)
547
+ }
548
+ })
549
+
550
+ reader.addEventListener('error', () =>
551
+ reject(new Error('FileReader error'))
552
+ )
553
+
554
+ reader.readAsArrayBuffer(blob)
555
+ } catch (ex) {
556
+ reject(ex)
557
+ }
558
+ })
559
+ }