node-av 0.0.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.
- package/CHANGELOG.md +8 -0
- package/LICENSE.md +22 -0
- package/README.md +377 -0
- package/binding.gyp +78 -0
- package/dist/api/bitstream-filter.d.ts +246 -0
- package/dist/api/bitstream-filter.js +369 -0
- package/dist/api/bitstream-filter.js.map +1 -0
- package/dist/api/decoder.d.ts +257 -0
- package/dist/api/decoder.js +424 -0
- package/dist/api/decoder.js.map +1 -0
- package/dist/api/encoder.d.ts +298 -0
- package/dist/api/encoder.js +574 -0
- package/dist/api/encoder.js.map +1 -0
- package/dist/api/filter.d.ts +457 -0
- package/dist/api/filter.js +876 -0
- package/dist/api/filter.js.map +1 -0
- package/dist/api/hardware.d.ts +318 -0
- package/dist/api/hardware.js +558 -0
- package/dist/api/hardware.js.map +1 -0
- package/dist/api/index.d.ts +12 -0
- package/dist/api/index.js +20 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/io-stream.d.ts +109 -0
- package/dist/api/io-stream.js +124 -0
- package/dist/api/io-stream.js.map +1 -0
- package/dist/api/media-input.d.ts +295 -0
- package/dist/api/media-input.js +456 -0
- package/dist/api/media-input.js.map +1 -0
- package/dist/api/media-output.d.ts +274 -0
- package/dist/api/media-output.js +486 -0
- package/dist/api/media-output.js.map +1 -0
- package/dist/api/pipeline.d.ts +117 -0
- package/dist/api/pipeline.js +836 -0
- package/dist/api/pipeline.js.map +1 -0
- package/dist/api/types.d.ts +440 -0
- package/dist/api/types.js +2 -0
- package/dist/api/types.js.map +1 -0
- package/dist/api/utilities/audio-sample.d.ts +115 -0
- package/dist/api/utilities/audio-sample.js +110 -0
- package/dist/api/utilities/audio-sample.js.map +1 -0
- package/dist/api/utilities/channel-layout.d.ts +83 -0
- package/dist/api/utilities/channel-layout.js +87 -0
- package/dist/api/utilities/channel-layout.js.map +1 -0
- package/dist/api/utilities/image.d.ts +177 -0
- package/dist/api/utilities/image.js +183 -0
- package/dist/api/utilities/image.js.map +1 -0
- package/dist/api/utilities/index.d.ts +8 -0
- package/dist/api/utilities/index.js +17 -0
- package/dist/api/utilities/index.js.map +1 -0
- package/dist/api/utilities/media-type.d.ts +56 -0
- package/dist/api/utilities/media-type.js +60 -0
- package/dist/api/utilities/media-type.js.map +1 -0
- package/dist/api/utilities/pixel-format.d.ts +94 -0
- package/dist/api/utilities/pixel-format.js +102 -0
- package/dist/api/utilities/pixel-format.js.map +1 -0
- package/dist/api/utilities/sample-format.d.ts +132 -0
- package/dist/api/utilities/sample-format.js +144 -0
- package/dist/api/utilities/sample-format.js.map +1 -0
- package/dist/api/utilities/streaming.d.ts +104 -0
- package/dist/api/utilities/streaming.js +137 -0
- package/dist/api/utilities/streaming.js.map +1 -0
- package/dist/api/utilities/timestamp.d.ts +187 -0
- package/dist/api/utilities/timestamp.js +200 -0
- package/dist/api/utilities/timestamp.js.map +1 -0
- package/dist/api/utils.d.ts +61 -0
- package/dist/api/utils.js +330 -0
- package/dist/api/utils.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/audio-fifo.d.ts +339 -0
- package/dist/lib/audio-fifo.js +365 -0
- package/dist/lib/audio-fifo.js.map +1 -0
- package/dist/lib/binding.d.ts +192 -0
- package/dist/lib/binding.js +70 -0
- package/dist/lib/binding.js.map +1 -0
- package/dist/lib/bitstream-filter-context.d.ts +345 -0
- package/dist/lib/bitstream-filter-context.js +407 -0
- package/dist/lib/bitstream-filter-context.js.map +1 -0
- package/dist/lib/bitstream-filter.d.ts +124 -0
- package/dist/lib/bitstream-filter.js +138 -0
- package/dist/lib/bitstream-filter.js.map +1 -0
- package/dist/lib/channel-layouts.d.ts +51 -0
- package/dist/lib/channel-layouts.js +55 -0
- package/dist/lib/channel-layouts.js.map +1 -0
- package/dist/lib/codec-context.d.ts +763 -0
- package/dist/lib/codec-context.js +974 -0
- package/dist/lib/codec-context.js.map +1 -0
- package/dist/lib/codec-parameters.d.ts +362 -0
- package/dist/lib/codec-parameters.js +460 -0
- package/dist/lib/codec-parameters.js.map +1 -0
- package/dist/lib/codec-parser.d.ts +185 -0
- package/dist/lib/codec-parser.js +193 -0
- package/dist/lib/codec-parser.js.map +1 -0
- package/dist/lib/codec.d.ts +432 -0
- package/dist/lib/codec.js +492 -0
- package/dist/lib/codec.js.map +1 -0
- package/dist/lib/constants.d.ts +2037 -0
- package/dist/lib/constants.js +1659 -0
- package/dist/lib/constants.js.map +1 -0
- package/dist/lib/dictionary.d.ts +371 -0
- package/dist/lib/dictionary.js +406 -0
- package/dist/lib/dictionary.js.map +1 -0
- package/dist/lib/error.d.ts +216 -0
- package/dist/lib/error.js +254 -0
- package/dist/lib/error.js.map +1 -0
- package/dist/lib/filter-context.d.ts +445 -0
- package/dist/lib/filter-context.js +505 -0
- package/dist/lib/filter-context.js.map +1 -0
- package/dist/lib/filter-graph.d.ts +556 -0
- package/dist/lib/filter-graph.js +608 -0
- package/dist/lib/filter-graph.js.map +1 -0
- package/dist/lib/filter-inout.d.ts +205 -0
- package/dist/lib/filter-inout.js +264 -0
- package/dist/lib/filter-inout.js.map +1 -0
- package/dist/lib/filter.d.ts +231 -0
- package/dist/lib/filter.js +260 -0
- package/dist/lib/filter.js.map +1 -0
- package/dist/lib/format-context.d.ts +798 -0
- package/dist/lib/format-context.js +845 -0
- package/dist/lib/format-context.js.map +1 -0
- package/dist/lib/frame.d.ts +784 -0
- package/dist/lib/frame.js +933 -0
- package/dist/lib/frame.js.map +1 -0
- package/dist/lib/hardware-device-context.d.ts +407 -0
- package/dist/lib/hardware-device-context.js +429 -0
- package/dist/lib/hardware-device-context.js.map +1 -0
- package/dist/lib/hardware-frames-context.d.ts +374 -0
- package/dist/lib/hardware-frames-context.js +430 -0
- package/dist/lib/hardware-frames-context.js.map +1 -0
- package/dist/lib/index.d.ts +31 -0
- package/dist/lib/index.js +54 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/input-format.d.ts +216 -0
- package/dist/lib/input-format.js +246 -0
- package/dist/lib/input-format.js.map +1 -0
- package/dist/lib/io-context.d.ts +495 -0
- package/dist/lib/io-context.js +550 -0
- package/dist/lib/io-context.js.map +1 -0
- package/dist/lib/log.d.ts +201 -0
- package/dist/lib/log.js +219 -0
- package/dist/lib/log.js.map +1 -0
- package/dist/lib/native-types.d.ts +719 -0
- package/dist/lib/native-types.js +2 -0
- package/dist/lib/native-types.js.map +1 -0
- package/dist/lib/option.d.ts +589 -0
- package/dist/lib/option.js +853 -0
- package/dist/lib/option.js.map +1 -0
- package/dist/lib/output-format.d.ts +179 -0
- package/dist/lib/output-format.js +205 -0
- package/dist/lib/output-format.js.map +1 -0
- package/dist/lib/packet.d.ts +487 -0
- package/dist/lib/packet.js +558 -0
- package/dist/lib/packet.js.map +1 -0
- package/dist/lib/rational.d.ts +210 -0
- package/dist/lib/rational.js +233 -0
- package/dist/lib/rational.js.map +1 -0
- package/dist/lib/software-resample-context.d.ts +572 -0
- package/dist/lib/software-resample-context.js +610 -0
- package/dist/lib/software-resample-context.js.map +1 -0
- package/dist/lib/software-scale-context.d.ts +290 -0
- package/dist/lib/software-scale-context.js +308 -0
- package/dist/lib/software-scale-context.js.map +1 -0
- package/dist/lib/stream.d.ts +322 -0
- package/dist/lib/stream.js +408 -0
- package/dist/lib/stream.js.map +1 -0
- package/dist/lib/types.d.ts +59 -0
- package/dist/lib/types.js +8 -0
- package/dist/lib/types.js.map +1 -0
- package/dist/lib/utilities.d.ts +346 -0
- package/dist/lib/utilities.js +424 -0
- package/dist/lib/utilities.js.map +1 -0
- package/install/check.js +113 -0
- package/install/ffmpeg.js +163 -0
- package/package.json +107 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rational number type for precise fractional representations.
|
|
3
|
+
*
|
|
4
|
+
* Represents a rational number as a fraction (numerator/denominator).
|
|
5
|
+
* Used throughout FFmpeg for time bases, aspect ratios, frame rates,
|
|
6
|
+
* and other fractional values that require exact precision.
|
|
7
|
+
*
|
|
8
|
+
* Direct mapping to FFmpeg's AVRational structure.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { Rational } from 'node-av';
|
|
13
|
+
*
|
|
14
|
+
* // Common time bases
|
|
15
|
+
* const timebase = new Rational(1, 90000); // 90kHz for MPEG-TS
|
|
16
|
+
* const videoTimebase = new Rational(1, 25); // 25 fps
|
|
17
|
+
* const audioTimebase = new Rational(1, 48000); // 48kHz audio
|
|
18
|
+
*
|
|
19
|
+
* // Frame rates
|
|
20
|
+
* const framerate = new Rational(30, 1); // 30 fps
|
|
21
|
+
* const ntscFramerate = new Rational(30000, 1001); // 29.97 fps NTSC
|
|
22
|
+
* const palFramerate = new Rational(25, 1); // 25 fps PAL
|
|
23
|
+
*
|
|
24
|
+
* // Aspect ratios
|
|
25
|
+
* const aspectRatio = new Rational(16, 9); // 16:9
|
|
26
|
+
* const pixelAspect = new Rational(1, 1); // Square pixels
|
|
27
|
+
*
|
|
28
|
+
* // Arithmetic operations
|
|
29
|
+
* const doubled = timebase.mul(new Rational(2, 1));
|
|
30
|
+
* const inverted = framerate.inv(); // Get frame duration
|
|
31
|
+
* const sum = framerate.add(new Rational(5, 1)); // Add 5 fps
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare class Rational {
|
|
35
|
+
readonly num: number;
|
|
36
|
+
readonly den: number;
|
|
37
|
+
/**
|
|
38
|
+
* Create a new rational number.
|
|
39
|
+
*
|
|
40
|
+
* Represents the fraction num/den.
|
|
41
|
+
*
|
|
42
|
+
* @param num - Numerator of the fraction
|
|
43
|
+
* @param den - Denominator of the fraction (must not be 0)
|
|
44
|
+
*
|
|
45
|
+
* @throws {Error} If denominator is 0
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* import { Rational } from 'node-av';
|
|
50
|
+
*
|
|
51
|
+
* // Create time base for 25 fps video
|
|
52
|
+
* const timebase = new Rational(1, 25);
|
|
53
|
+
*
|
|
54
|
+
* // Create NTSC frame rate (29.97 fps)
|
|
55
|
+
* const ntsc = new Rational(30000, 1001);
|
|
56
|
+
*
|
|
57
|
+
* // Will throw error
|
|
58
|
+
* try {
|
|
59
|
+
* const invalid = new Rational(1, 0);
|
|
60
|
+
* } catch (error) {
|
|
61
|
+
* console.error('Cannot have zero denominator');
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
constructor(num: number, den: number);
|
|
66
|
+
/**
|
|
67
|
+
* Add two rational numbers.
|
|
68
|
+
*
|
|
69
|
+
* Performs addition: (a/b) + (c/d) = (ad + bc) / bd
|
|
70
|
+
*
|
|
71
|
+
* @param other - The rational number to add
|
|
72
|
+
*
|
|
73
|
+
* @returns A new Rational representing the sum
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const a = new Rational(1, 2); // 1/2
|
|
78
|
+
* const b = new Rational(1, 3); // 1/3
|
|
79
|
+
* const sum = a.add(b); // 5/6
|
|
80
|
+
* console.log(sum.toString()); // "5/6"
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
add(other: Rational): Rational;
|
|
84
|
+
/**
|
|
85
|
+
* Subtract two rational numbers.
|
|
86
|
+
*
|
|
87
|
+
* Performs subtraction: (a/b) - (c/d) = (ad - bc) / bd
|
|
88
|
+
*
|
|
89
|
+
* @param other - The rational number to subtract
|
|
90
|
+
*
|
|
91
|
+
* @returns A new Rational representing the difference
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const a = new Rational(3, 4); // 3/4
|
|
96
|
+
* const b = new Rational(1, 4); // 1/4
|
|
97
|
+
* const diff = a.sub(b); // 2/4 = 1/2
|
|
98
|
+
* console.log(diff.toString()); // "2/4"
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
sub(other: Rational): Rational;
|
|
102
|
+
/**
|
|
103
|
+
* Multiply two rational numbers.
|
|
104
|
+
*
|
|
105
|
+
* Performs multiplication: (a/b) × (c/d) = (ac) / (bd)
|
|
106
|
+
*
|
|
107
|
+
* @param other - The rational number to multiply by
|
|
108
|
+
*
|
|
109
|
+
* @returns A new Rational representing the product
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const framerate = new Rational(30, 1); // 30 fps
|
|
114
|
+
* const duration = new Rational(5, 1); // 5 seconds
|
|
115
|
+
* const frames = framerate.mul(duration); // 150/1 = 150 frames
|
|
116
|
+
* console.log(frames.toDouble()); // 150
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
mul(other: Rational): Rational;
|
|
120
|
+
/**
|
|
121
|
+
* Divide two rational numbers.
|
|
122
|
+
*
|
|
123
|
+
* Performs division: (a/b) ÷ (c/d) = (ad) / (bc)
|
|
124
|
+
*
|
|
125
|
+
* @param other - The rational number to divide by
|
|
126
|
+
*
|
|
127
|
+
* @returns A new Rational representing the quotient
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* const pixels = new Rational(1920, 1); // 1920 pixels width
|
|
132
|
+
* const aspect = new Rational(16, 9); // 16:9 aspect ratio
|
|
133
|
+
* const height = pixels.div(aspect); // Calculate height
|
|
134
|
+
* console.log(height.toDouble()); // 1080
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
div(other: Rational): Rational;
|
|
138
|
+
/**
|
|
139
|
+
* Invert the rational number (reciprocal).
|
|
140
|
+
*
|
|
141
|
+
* Returns the reciprocal: 1/(a/b) = b/a
|
|
142
|
+
*
|
|
143
|
+
* @returns A new Rational representing the reciprocal
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* const framerate = new Rational(25, 1); // 25 fps
|
|
148
|
+
* const frameDuration = framerate.inv(); // 1/25 seconds per frame
|
|
149
|
+
* console.log(frameDuration.toString()); // "1/25"
|
|
150
|
+
* console.log(frameDuration.toDouble()); // 0.04 seconds
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
inv(): Rational;
|
|
154
|
+
/**
|
|
155
|
+
* Convert rational to floating point number.
|
|
156
|
+
*
|
|
157
|
+
* Calculates the decimal value: num / den
|
|
158
|
+
* Note: This may lose precision for some rational values.
|
|
159
|
+
*
|
|
160
|
+
* @returns The floating point representation
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```typescript
|
|
164
|
+
* const ntsc = new Rational(30000, 1001); // NTSC frame rate
|
|
165
|
+
* console.log(ntsc.toDouble()); // 29.97002997...
|
|
166
|
+
*
|
|
167
|
+
* const half = new Rational(1, 2);
|
|
168
|
+
* console.log(half.toDouble()); // 0.5
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
toDouble(): number;
|
|
172
|
+
/**
|
|
173
|
+
* Check if this rational equals another.
|
|
174
|
+
*
|
|
175
|
+
* Compares using cross-multiplication to avoid floating point errors.
|
|
176
|
+
* Two rationals a/b and c/d are equal if ad = bc.
|
|
177
|
+
*
|
|
178
|
+
* @param other - The rational to compare with
|
|
179
|
+
*
|
|
180
|
+
* @returns true if the rationals are equal, false otherwise
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```typescript
|
|
184
|
+
* const a = new Rational(2, 4);
|
|
185
|
+
* const b = new Rational(1, 2);
|
|
186
|
+
* const c = new Rational(3, 4);
|
|
187
|
+
*
|
|
188
|
+
* console.log(a.equals(b)); // true (both are 1/2)
|
|
189
|
+
* console.log(a.equals(c)); // false
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
equals(other: Rational): boolean;
|
|
193
|
+
/**
|
|
194
|
+
* Get string representation of the rational.
|
|
195
|
+
*
|
|
196
|
+
* Returns the rational in "num/den" format.
|
|
197
|
+
*
|
|
198
|
+
* @returns String representation as "numerator/denominator"
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* const framerate = new Rational(30000, 1001);
|
|
203
|
+
* console.log(framerate.toString()); // "30000/1001"
|
|
204
|
+
*
|
|
205
|
+
* const timebase = new Rational(1, 90000);
|
|
206
|
+
* console.log(`Timebase: ${timebase}`); // "Timebase: 1/90000"
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
toString(): string;
|
|
210
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rational number type for precise fractional representations.
|
|
3
|
+
*
|
|
4
|
+
* Represents a rational number as a fraction (numerator/denominator).
|
|
5
|
+
* Used throughout FFmpeg for time bases, aspect ratios, frame rates,
|
|
6
|
+
* and other fractional values that require exact precision.
|
|
7
|
+
*
|
|
8
|
+
* Direct mapping to FFmpeg's AVRational structure.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { Rational } from 'node-av';
|
|
13
|
+
*
|
|
14
|
+
* // Common time bases
|
|
15
|
+
* const timebase = new Rational(1, 90000); // 90kHz for MPEG-TS
|
|
16
|
+
* const videoTimebase = new Rational(1, 25); // 25 fps
|
|
17
|
+
* const audioTimebase = new Rational(1, 48000); // 48kHz audio
|
|
18
|
+
*
|
|
19
|
+
* // Frame rates
|
|
20
|
+
* const framerate = new Rational(30, 1); // 30 fps
|
|
21
|
+
* const ntscFramerate = new Rational(30000, 1001); // 29.97 fps NTSC
|
|
22
|
+
* const palFramerate = new Rational(25, 1); // 25 fps PAL
|
|
23
|
+
*
|
|
24
|
+
* // Aspect ratios
|
|
25
|
+
* const aspectRatio = new Rational(16, 9); // 16:9
|
|
26
|
+
* const pixelAspect = new Rational(1, 1); // Square pixels
|
|
27
|
+
*
|
|
28
|
+
* // Arithmetic operations
|
|
29
|
+
* const doubled = timebase.mul(new Rational(2, 1));
|
|
30
|
+
* const inverted = framerate.inv(); // Get frame duration
|
|
31
|
+
* const sum = framerate.add(new Rational(5, 1)); // Add 5 fps
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export class Rational {
|
|
35
|
+
num;
|
|
36
|
+
den;
|
|
37
|
+
/**
|
|
38
|
+
* Create a new rational number.
|
|
39
|
+
*
|
|
40
|
+
* Represents the fraction num/den.
|
|
41
|
+
*
|
|
42
|
+
* @param num - Numerator of the fraction
|
|
43
|
+
* @param den - Denominator of the fraction (must not be 0)
|
|
44
|
+
*
|
|
45
|
+
* @throws {Error} If denominator is 0
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* import { Rational } from 'node-av';
|
|
50
|
+
*
|
|
51
|
+
* // Create time base for 25 fps video
|
|
52
|
+
* const timebase = new Rational(1, 25);
|
|
53
|
+
*
|
|
54
|
+
* // Create NTSC frame rate (29.97 fps)
|
|
55
|
+
* const ntsc = new Rational(30000, 1001);
|
|
56
|
+
*
|
|
57
|
+
* // Will throw error
|
|
58
|
+
* try {
|
|
59
|
+
* const invalid = new Rational(1, 0);
|
|
60
|
+
* } catch (error) {
|
|
61
|
+
* console.error('Cannot have zero denominator');
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
constructor(num, den) {
|
|
66
|
+
this.num = num;
|
|
67
|
+
this.den = den;
|
|
68
|
+
if (den === 0) {
|
|
69
|
+
throw new Error('Denominator cannot be zero');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Add two rational numbers.
|
|
74
|
+
*
|
|
75
|
+
* Performs addition: (a/b) + (c/d) = (ad + bc) / bd
|
|
76
|
+
*
|
|
77
|
+
* @param other - The rational number to add
|
|
78
|
+
*
|
|
79
|
+
* @returns A new Rational representing the sum
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const a = new Rational(1, 2); // 1/2
|
|
84
|
+
* const b = new Rational(1, 3); // 1/3
|
|
85
|
+
* const sum = a.add(b); // 5/6
|
|
86
|
+
* console.log(sum.toString()); // "5/6"
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
add(other) {
|
|
90
|
+
return new Rational(this.num * other.den + other.num * this.den, this.den * other.den);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Subtract two rational numbers.
|
|
94
|
+
*
|
|
95
|
+
* Performs subtraction: (a/b) - (c/d) = (ad - bc) / bd
|
|
96
|
+
*
|
|
97
|
+
* @param other - The rational number to subtract
|
|
98
|
+
*
|
|
99
|
+
* @returns A new Rational representing the difference
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* const a = new Rational(3, 4); // 3/4
|
|
104
|
+
* const b = new Rational(1, 4); // 1/4
|
|
105
|
+
* const diff = a.sub(b); // 2/4 = 1/2
|
|
106
|
+
* console.log(diff.toString()); // "2/4"
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
sub(other) {
|
|
110
|
+
return new Rational(this.num * other.den - other.num * this.den, this.den * other.den);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Multiply two rational numbers.
|
|
114
|
+
*
|
|
115
|
+
* Performs multiplication: (a/b) × (c/d) = (ac) / (bd)
|
|
116
|
+
*
|
|
117
|
+
* @param other - The rational number to multiply by
|
|
118
|
+
*
|
|
119
|
+
* @returns A new Rational representing the product
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* const framerate = new Rational(30, 1); // 30 fps
|
|
124
|
+
* const duration = new Rational(5, 1); // 5 seconds
|
|
125
|
+
* const frames = framerate.mul(duration); // 150/1 = 150 frames
|
|
126
|
+
* console.log(frames.toDouble()); // 150
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
mul(other) {
|
|
130
|
+
return new Rational(this.num * other.num, this.den * other.den);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Divide two rational numbers.
|
|
134
|
+
*
|
|
135
|
+
* Performs division: (a/b) ÷ (c/d) = (ad) / (bc)
|
|
136
|
+
*
|
|
137
|
+
* @param other - The rational number to divide by
|
|
138
|
+
*
|
|
139
|
+
* @returns A new Rational representing the quotient
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* const pixels = new Rational(1920, 1); // 1920 pixels width
|
|
144
|
+
* const aspect = new Rational(16, 9); // 16:9 aspect ratio
|
|
145
|
+
* const height = pixels.div(aspect); // Calculate height
|
|
146
|
+
* console.log(height.toDouble()); // 1080
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
div(other) {
|
|
150
|
+
return new Rational(this.num * other.den, this.den * other.num);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Invert the rational number (reciprocal).
|
|
154
|
+
*
|
|
155
|
+
* Returns the reciprocal: 1/(a/b) = b/a
|
|
156
|
+
*
|
|
157
|
+
* @returns A new Rational representing the reciprocal
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```typescript
|
|
161
|
+
* const framerate = new Rational(25, 1); // 25 fps
|
|
162
|
+
* const frameDuration = framerate.inv(); // 1/25 seconds per frame
|
|
163
|
+
* console.log(frameDuration.toString()); // "1/25"
|
|
164
|
+
* console.log(frameDuration.toDouble()); // 0.04 seconds
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
inv() {
|
|
168
|
+
return new Rational(this.den, this.num);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Convert rational to floating point number.
|
|
172
|
+
*
|
|
173
|
+
* Calculates the decimal value: num / den
|
|
174
|
+
* Note: This may lose precision for some rational values.
|
|
175
|
+
*
|
|
176
|
+
* @returns The floating point representation
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```typescript
|
|
180
|
+
* const ntsc = new Rational(30000, 1001); // NTSC frame rate
|
|
181
|
+
* console.log(ntsc.toDouble()); // 29.97002997...
|
|
182
|
+
*
|
|
183
|
+
* const half = new Rational(1, 2);
|
|
184
|
+
* console.log(half.toDouble()); // 0.5
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
toDouble() {
|
|
188
|
+
return this.num / this.den;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Check if this rational equals another.
|
|
192
|
+
*
|
|
193
|
+
* Compares using cross-multiplication to avoid floating point errors.
|
|
194
|
+
* Two rationals a/b and c/d are equal if ad = bc.
|
|
195
|
+
*
|
|
196
|
+
* @param other - The rational to compare with
|
|
197
|
+
*
|
|
198
|
+
* @returns true if the rationals are equal, false otherwise
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* const a = new Rational(2, 4);
|
|
203
|
+
* const b = new Rational(1, 2);
|
|
204
|
+
* const c = new Rational(3, 4);
|
|
205
|
+
*
|
|
206
|
+
* console.log(a.equals(b)); // true (both are 1/2)
|
|
207
|
+
* console.log(a.equals(c)); // false
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
equals(other) {
|
|
211
|
+
return this.num * other.den === other.num * this.den;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Get string representation of the rational.
|
|
215
|
+
*
|
|
216
|
+
* Returns the rational in "num/den" format.
|
|
217
|
+
*
|
|
218
|
+
* @returns String representation as "numerator/denominator"
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```typescript
|
|
222
|
+
* const framerate = new Rational(30000, 1001);
|
|
223
|
+
* console.log(framerate.toString()); // "30000/1001"
|
|
224
|
+
*
|
|
225
|
+
* const timebase = new Rational(1, 90000);
|
|
226
|
+
* console.log(`Timebase: ${timebase}`); // "Timebase: 1/90000"
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
toString() {
|
|
230
|
+
return `${this.num}/${this.den}`;
|
|
231
|
+
}
|
|
232
|
+
}
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