web-audio-recorder-ts 1.0.0 → 1.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/README.md CHANGED
@@ -1,15 +1,22 @@
1
1
  # web-audio-recorder-ts
2
2
 
3
+ [![npm version](https://img.shields.io/npm/v/web-audio-recorder-ts.svg)](https://www.npmjs.com/package/web-audio-recorder-ts)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
3
6
  TypeScript port of [web-audio-recorder-js](https://github.com/higuma/web-audio-recorder-js) with full type support for WAV, OGG Vorbis, and MP3 audio recording in browsers.
4
7
 
8
+ **✨ No manual file copying required!** The library automatically detects and loads encoder files from the npm package.
9
+
5
10
  ## Features
6
11
 
7
- - ✅ Full TypeScript support with type definitions
8
- - ✅ Record audio in WAV, OGG Vorbis, and MP3 formats
9
- - ✅ Modern ES modules, CommonJS, and UMD builds
10
- - ✅ Zero runtime dependencies
11
- - ✅ Works in browsers and Web Workers
12
- - ✅ Type-safe API with IntelliSense support
12
+ - ✅ **Full TypeScript support** with complete type definitions
13
+ - ✅ **Record audio in WAV, OGG Vorbis, and MP3 formats**
14
+ - ✅ **Automatic encoder file detection** - no manual file copying needed!
15
+ - ✅ **Modern build system** - ES modules, CommonJS, and UMD outputs
16
+ - ✅ **Zero runtime dependencies** - pure TypeScript/JavaScript
17
+ - ✅ **Works in browsers and Web Workers**
18
+ - ✅ **Type-safe API** with full IntelliSense support
19
+ - ✅ **Production ready** - tested and published on npm
13
20
 
14
21
  ## Installation
15
22
 
@@ -60,15 +67,8 @@ const blob = await recorder.stop();
60
67
  ```typescript
61
68
  import { WebAudioRecorderOgg, loadOggVorbisEncoder } from 'web-audio-recorder-ts';
62
69
 
63
- // Load encoder script first
64
- await loadOggVorbisEncoder('/path/to/OggVorbisEncoder.min.js');
65
-
66
- // Configure memory initializer path (if needed)
67
- if (typeof window !== 'undefined') {
68
- (window as any).OggVorbisEncoderConfig = {
69
- memoryInitializerPrefixURL: '/path/to/'
70
- };
71
- }
70
+ // Load encoder script (auto-detects from node_modules - no path needed!)
71
+ await loadOggVorbisEncoder();
72
72
 
73
73
  const audioContext = new AudioContext();
74
74
  const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
@@ -79,6 +79,10 @@ const recorder = new WebAudioRecorderOgg(
79
79
  { quality: 0.7 } // OGG quality: -0.1 to 1.0
80
80
  );
81
81
 
82
+ recorder.setOnComplete((event) => {
83
+ console.log('OGG recording complete!', event.url);
84
+ });
85
+
82
86
  await recorder.start(stream);
83
87
  const blob = await recorder.stop();
84
88
  ```
@@ -88,15 +92,8 @@ const blob = await recorder.stop();
88
92
  ```typescript
89
93
  import { WebAudioRecorderMp3, loadMp3LameEncoder } from 'web-audio-recorder-ts';
90
94
 
91
- // Load encoder script first
92
- await loadMp3LameEncoder('/path/to/Mp3LameEncoder.min.js');
93
-
94
- // Configure memory initializer path (if needed)
95
- if (typeof window !== 'undefined') {
96
- (window as any).Mp3LameEncoderConfig = {
97
- memoryInitializerPrefixURL: '/path/to/'
98
- };
99
- }
95
+ // Load encoder script (auto-detects from node_modules - no path needed!)
96
+ await loadMp3LameEncoder();
100
97
 
101
98
  const audioContext = new AudioContext();
102
99
  const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
@@ -107,6 +104,10 @@ const recorder = new WebAudioRecorderMp3(
107
104
  { bitrate: 192 } // MP3 bitrate in kbps
108
105
  );
109
106
 
107
+ recorder.setOnComplete((event) => {
108
+ console.log('MP3 recording complete!', event.url);
109
+ });
110
+
110
111
  await recorder.start(stream);
111
112
  const blob = await recorder.stop();
112
113
  ```
@@ -194,15 +195,29 @@ enum RecorderStatus {
194
195
 
195
196
  ## Setup for OGG and MP3
196
197
 
197
- OGG and MP3 encoders require external JavaScript files compiled via Emscripten. You need to:
198
+ OGG and MP3 encoders require external JavaScript files compiled via Emscripten. The good news is that **you don't need to copy any files manually** - the library will automatically detect and load them from the npm package!
199
+
200
+ ### Automatic Loading (Recommended)
201
+
202
+ Simply call the loader functions without any parameters, and the library will automatically find the encoder files:
203
+
204
+ ```typescript
205
+ // For OGG - automatically finds the file in node_modules
206
+ await loadOggVorbisEncoder();
207
+
208
+ // For MP3 - automatically finds the file in node_modules
209
+ await loadMp3LameEncoder();
210
+ ```
211
+
212
+ The library will:
198
213
 
199
- 1. Copy the encoder files to your project:
200
- - `OggVorbisEncoder.min.js` and `OggVorbisEncoder.min.js.mem`
201
- - `Mp3LameEncoder.min.js` and `Mp3LameEncoder.min.js.mem`
214
+ 1. Automatically detect the package location in `node_modules`
215
+ 2. Configure the memory initializer paths for `.mem` files
216
+ 3. Load the encoder scripts from the correct location
202
217
 
203
- 2. These files are included in the `lib/` directory of this package.
218
+ ### Manual Path (If Needed)
204
219
 
205
- 3. Load the encoder scripts before creating recorders:
220
+ If automatic detection fails (e.g., custom build setup), you can still provide the path manually:
206
221
 
207
222
  ```typescript
208
223
  // For OGG
@@ -212,26 +227,110 @@ await loadOggVorbisEncoder('/path/to/OggVorbisEncoder.min.js');
212
227
  await loadMp3LameEncoder('/path/to/Mp3LameEncoder.min.js');
213
228
  ```
214
229
 
215
- 4. Configure memory initializer path if needed:
230
+ ### Advanced Configuration
231
+
232
+ If you need more control, you can use the utility functions:
216
233
 
217
234
  ```typescript
218
- // For OGG
219
- window.OggVorbisEncoderConfig = {
220
- memoryInitializerPrefixURL: '/path/to/'
221
- };
235
+ import {
236
+ configureEncoderPaths,
237
+ getEncoderBaseUrl,
238
+ findEncoderPath
239
+ } from 'web-audio-recorder-ts';
222
240
 
223
- // For MP3
224
- window.Mp3LameEncoderConfig = {
225
- memoryInitializerPrefixURL: '/path/to/'
226
- };
241
+ // Configure paths manually
242
+ configureEncoderPaths('/custom/path/to/lib');
243
+
244
+ // Get the detected base URL
245
+ const baseUrl = getEncoderBaseUrl();
246
+
247
+ // Find encoder file path
248
+ const oggPath = await findEncoderPath('OggVorbisEncoder.min.js');
249
+ ```
250
+
251
+ ### File Locations
252
+
253
+ The encoder files are included in the `lib/` directory of the npm package:
254
+
255
+ - `OggVorbisEncoder.min.js` and `OggVorbisEncoder.min.js.mem`
256
+ - `Mp3LameEncoder.min.js` and `Mp3LameEncoder.min.js.mem`
257
+
258
+ When installed via npm, they will be at:
259
+
260
+ - `node_modules/web-audio-recorder-ts/lib/`
261
+
262
+ ## Framework Support
263
+
264
+ ### Nuxt.js
265
+
266
+ If you're using Nuxt and seeing errors about TypeScript source files, see [NUXT_USAGE.md](./NUXT_USAGE.md) for detailed instructions.
267
+
268
+ Quick fix - add to `nuxt.config.ts`:
269
+
270
+ ```typescript
271
+ export default defineNuxtConfig({
272
+ vite: {
273
+ optimizeDeps: {
274
+ exclude: ['web-audio-recorder-ts']
275
+ }
276
+ }
277
+ })
227
278
  ```
228
279
 
280
+ ### Vite / Vue
281
+
282
+ Works out of the box! Just import and use.
283
+
284
+ ### Next.js
285
+
286
+ Should work with default configuration. For OGG/MP3, ensure encoder files are accessible.
287
+
229
288
  ## Browser Support
230
289
 
231
- - Chrome/Edge: Full support
232
- - Firefox: Full support
233
- - Safari: Full support (may require user gesture for audio context)
234
- - Opera: Full support
290
+ - Chrome/Edge: Full support
291
+ - Firefox: Full support
292
+ - Safari: Full support (may require user gesture for audio context)
293
+ - Opera: Full support
294
+
295
+ ### Requirements
296
+
297
+ - Modern browser with Web Audio API support
298
+ - `getUserMedia` API for microphone access
299
+ - For OGG/MP3: Server must be able to serve files from `node_modules` (or use manual paths)
300
+
301
+ ## Troubleshooting
302
+
303
+ ### Encoder files not found
304
+
305
+ If automatic detection fails, you can:
306
+
307
+ 1. **Check if files exist**: Verify that `node_modules/web-audio-recorder-ts/lib/` contains the encoder files
308
+ 2. **Use manual paths**: Provide the path explicitly:
309
+
310
+ ```typescript
311
+ await loadOggVorbisEncoder('/path/to/OggVorbisEncoder.min.js');
312
+ ```
313
+
314
+ 3. **Check server configuration**: Ensure your dev server can serve files from `node_modules` (Vite, Webpack, etc.)
315
+
316
+ ### Memory initializer errors
317
+
318
+ If you see errors about `.mem` files:
319
+
320
+ 1. The library automatically configures paths, but if needed:
321
+
322
+ ```typescript
323
+ import { configureEncoderPaths } from 'web-audio-recorder-ts';
324
+ configureEncoderPaths('/path/to/lib/');
325
+ ```
326
+
327
+ ### CORS errors
328
+
329
+ If you see CORS errors when loading encoder files:
330
+
331
+ - Ensure your server allows loading from `node_modules`
332
+ - Consider copying files to `public/` folder in development
333
+ - Use a CDN or absolute URLs in production
235
334
 
236
335
  ## License
237
336
 
@@ -244,18 +343,111 @@ See LICENSE file for details.
244
343
  ## Credits
245
344
 
246
345
  This is a TypeScript port of:
346
+
247
347
  - [web-audio-recorder-js](https://github.com/higuma/web-audio-recorder-js) by higuma
248
348
  - [ogg-vorbis-encoder-js](https://github.com/higuma/ogg-vorbis-encoder-js) by higuma
249
349
 
350
+ ## Development
351
+
352
+ ### Building
353
+
354
+ ```bash
355
+ # Install dependencies
356
+ pnpm install
357
+
358
+ # Build the library
359
+ pnpm build
360
+
361
+ # Watch mode for development
362
+ pnpm dev
363
+
364
+ # Run demo
365
+ pnpm demo
366
+ ```
367
+
368
+ ### Project Structure
369
+
370
+ ```
371
+ web-audio-recorder-ts/
372
+ ├── src/
373
+ │ ├── core/ # Core recorder classes and types
374
+ │ ├── encoders/ # Audio encoders (WAV, OGG, MP3)
375
+ │ ├── recorders/ # Format-specific recorders
376
+ │ └── utils/ # Utility functions (auto-detection, etc.)
377
+ ├── lib/ # Emscripten encoder files
378
+ ├── types/ # TypeScript declarations for Emscripten
379
+ ├── demo/ # Demo application
380
+ └── dist/ # Build output
381
+ ```
382
+
250
383
  ## Contributing
251
384
 
252
385
  Contributions are welcome! Please feel free to submit a Pull Request.
253
386
 
387
+ 1. Fork the repository
388
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
389
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
390
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
391
+ 5. Open a Pull Request
392
+
393
+ ## Examples
394
+
395
+ ### Complete Example with Error Handling
396
+
397
+ ```typescript
398
+ import { WebAudioRecorderWav } from 'web-audio-recorder-ts';
399
+
400
+ async function recordAudio() {
401
+ try {
402
+ const audioContext = new AudioContext();
403
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
404
+
405
+ const recorder = new WebAudioRecorderWav(audioContext, {
406
+ onDataAvailable: (event) => {
407
+ console.log('Data available:', event.data.length, 'bytes');
408
+ },
409
+ onComplete: (event) => {
410
+ console.log('Recording complete!');
411
+ // Download the file
412
+ const a = document.createElement('a');
413
+ a.href = event.url;
414
+ a.download = 'recording.wav';
415
+ a.click();
416
+ },
417
+ onError: (event) => {
418
+ console.error('Recording error:', event.message);
419
+ }
420
+ });
421
+
422
+ await recorder.start(stream);
423
+
424
+ // Record for 5 seconds
425
+ setTimeout(async () => {
426
+ const blob = await recorder.stop();
427
+ console.log('Blob size:', blob.size, 'bytes');
428
+ recorder.cleanup();
429
+ }, 5000);
430
+
431
+ } catch (error) {
432
+ console.error('Failed to start recording:', error);
433
+ }
434
+ }
435
+ ```
436
+
437
+ ## Repository
438
+
439
+ - **GitHub**: [https://github.com/lucasbrito-wdt/web-audio-recorder](https://github.com/lucasbrito-wdt/web-audio-recorder)
440
+ - **npm**: [https://www.npmjs.com/package/web-audio-recorder-ts](https://www.npmjs.com/package/web-audio-recorder-ts)
441
+ - **Issues**: [https://github.com/lucasbrito-wdt/web-audio-recorder/issues](https://github.com/lucasbrito-wdt/web-audio-recorder/issues)
442
+
254
443
  ## Changelog
255
444
 
256
- ### 1.0.0
445
+ ### 1.0.0 (2026-01-06)
257
446
 
258
- - Initial TypeScript port
259
- - Full type definitions
260
- - Support for WAV, OGG, and MP3 formats
261
- - Modern build system with ESM, CJS, and UMD outputs
447
+ - ✨ **Initial TypeScript port** - Complete conversion from JavaScript
448
+ - **Automatic encoder file detection** - No manual file copying required
449
+ - **Full type definitions** - Complete TypeScript support with IntelliSense
450
+ - **Support for WAV, OGG, and MP3 formats** - All three formats working
451
+ - ✨ **Modern build system** - ESM, CJS, and UMD outputs
452
+ - ✨ **Zero configuration** - Works out of the box with auto-detection
453
+ - 📦 **Published on npm** - Ready for production use
@@ -82,4 +82,3 @@ export declare class WebAudioRecorder {
82
82
  */
83
83
  cleanup(): void;
84
84
  }
85
- //# sourceMappingURL=WebAudioRecorder.d.ts.map
@@ -106,4 +106,3 @@ export type CompleteCallback = (event: CompleteEvent) => void;
106
106
  * Callback para eventos de erro
107
107
  */
108
108
  export type ErrorCallback = (event: ErrorEvent) => void;
109
- //# sourceMappingURL=types.d.ts.map
@@ -41,8 +41,7 @@ export declare class Mp3LameEncoderWrapper implements AudioEncoder {
41
41
  /**
42
42
  * Função helper para carregar o script Mp3LameEncoder
43
43
  *
44
- * @param scriptUrl - URL do script Mp3LameEncoder.min.js
44
+ * @param scriptUrl - URL do script Mp3LameEncoder.min.js (opcional, tenta auto-detectar se não fornecido)
45
45
  * @returns Promise que resolve quando o script é carregado
46
46
  */
47
- export declare function loadMp3LameEncoder(scriptUrl: string): Promise<void>;
48
- //# sourceMappingURL=Mp3LameEncoder.d.ts.map
47
+ export declare function loadMp3LameEncoder(scriptUrl?: string): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"Mp3LameEncoder.d.ts","sourceRoot":"","sources":["../../src/encoders/Mp3LameEncoder.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAczD;;GAEG;AACH,qBAAa,qBAAsB,YAAW,YAAY;IACxD,OAAO,CAAC,OAAO,CAAuC;IACtD,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,OAAO,CAAS;IAExB;;;;;;OAMG;gBACS,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,GAAE,UAAe;IAc7E;;;;OAIG;IACH,MAAM,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,IAAI;IAYrC;;;;;OAKG;IACH,MAAM,CAAC,QAAQ,GAAE,MAAqB,GAAG,IAAI;IAQ7C;;OAEG;IACH,MAAM,IAAI,IAAI;CAMf;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAqEnE"}
1
+ {"version":3,"file":"Mp3LameEncoder.d.ts","sourceRoot":"","sources":["../../src/encoders/Mp3LameEncoder.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAezD;;GAEG;AACH,qBAAa,qBAAsB,YAAW,YAAY;IACxD,OAAO,CAAC,OAAO,CAAuC;IACtD,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,OAAO,CAAS;IAExB;;;;;;OAMG;gBACS,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,GAAE,UAAe;IAc7E;;;;OAIG;IACH,MAAM,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,IAAI;IAYrC;;;;;OAKG;IACH,MAAM,CAAC,QAAQ,GAAE,MAAqB,GAAG,IAAI;IAQ7C;;OAEG;IACH,MAAM,IAAI,IAAI;CAMf;AAED;;;;;GAKG;AACH,wBAAsB,kBAAkB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAqB1E"}
@@ -41,8 +41,7 @@ export declare class OggVorbisEncoderWrapper implements AudioEncoder {
41
41
  /**
42
42
  * Função helper para carregar o script OggVorbisEncoder
43
43
  *
44
- * @param scriptUrl - URL do script OggVorbisEncoder.min.js
44
+ * @param scriptUrl - URL do script OggVorbisEncoder.min.js (opcional, tenta auto-detectar se não fornecido)
45
45
  * @returns Promise que resolve quando o script é carregado
46
46
  */
47
- export declare function loadOggVorbisEncoder(scriptUrl: string): Promise<void>;
48
- //# sourceMappingURL=OggVorbisEncoder.d.ts.map
47
+ export declare function loadOggVorbisEncoder(scriptUrl?: string): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"OggVorbisEncoder.d.ts","sourceRoot":"","sources":["../../src/encoders/OggVorbisEncoder.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAc/D;;GAEG;AACH,qBAAa,uBAAwB,YAAW,YAAY;IAC1D,OAAO,CAAC,OAAO,CAAyC;IACxD,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,OAAO,CAAS;IAExB;;;;;;OAMG;gBACS,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,GAAE,gBAAqB;IAcnF;;;;OAIG;IACH,MAAM,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,IAAI;IAYrC;;;;;OAKG;IACH,MAAM,CAAC,QAAQ,GAAE,MAAoB,GAAG,IAAI;IAQ5C;;OAEG;IACH,MAAM,IAAI,IAAI;CAMf;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAqErE"}
1
+ {"version":3,"file":"OggVorbisEncoder.d.ts","sourceRoot":"","sources":["../../src/encoders/OggVorbisEncoder.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAe/D;;GAEG;AACH,qBAAa,uBAAwB,YAAW,YAAY;IAC1D,OAAO,CAAC,OAAO,CAAyC;IACxD,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,OAAO,CAAS;IAExB;;;;;;OAMG;gBACS,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,GAAE,gBAAqB;IAcnF;;;;OAIG;IACH,MAAM,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,IAAI;IAYrC;;;;;OAKG;IACH,MAAM,CAAC,QAAQ,GAAE,MAAoB,GAAG,IAAI;IAQ5C;;OAEG;IACH,MAAM,IAAI,IAAI;CAMf;AAED;;;;;GAKG;AACH,wBAAsB,oBAAoB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAqB5E"}
@@ -44,4 +44,3 @@ export declare class WavAudioEncoder implements AudioEncoder {
44
44
  */
45
45
  private writeString;
46
46
  }
47
- //# sourceMappingURL=WavAudioEncoder.d.ts.map