react-native-audio-api 0.4.13 → 0.5.0

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 (115) hide show
  1. package/android/src/main/cpp/audioapi/android/core/AudioDecoder.cpp +87 -5
  2. package/android/src/main/java/com/swmansion/audioapi/AudioAPIModule.kt +7 -6
  3. package/android/src/main/java/com/swmansion/audioapi/AudioAPIPackage.kt +25 -24
  4. package/common/cpp/audioapi/HostObjects/AnalyserNodeHostObject.h +17 -41
  5. package/common/cpp/audioapi/HostObjects/AudioBufferHostObject.h +25 -34
  6. package/common/cpp/audioapi/HostObjects/AudioBufferSourceNodeHostObject.h +2 -17
  7. package/common/cpp/audioapi/HostObjects/AudioContextHostObject.h +14 -4
  8. package/common/cpp/audioapi/HostObjects/AudioParamHostObject.h +5 -8
  9. package/common/cpp/audioapi/HostObjects/BaseAudioContextHostObject.h +38 -9
  10. package/common/cpp/audioapi/HostObjects/BiquadFilterNodeHostObject.h +8 -19
  11. package/common/cpp/audioapi/core/AudioContext.cpp +14 -4
  12. package/common/cpp/audioapi/core/AudioContext.h +2 -2
  13. package/common/cpp/audioapi/core/BaseAudioContext.cpp +16 -2
  14. package/common/cpp/audioapi/core/BaseAudioContext.h +2 -1
  15. package/common/cpp/audioapi/core/effects/BiquadFilterNode.cpp +5 -6
  16. package/common/cpp/audioapi/core/effects/BiquadFilterNode.h +4 -3
  17. package/common/cpp/audioapi/core/sources/AudioBufferSourceNode.cpp +5 -11
  18. package/common/cpp/audioapi/core/sources/AudioBufferSourceNode.h +3 -31
  19. package/common/cpp/audioapi/core/utils/AudioDecoder.h +2 -1
  20. package/common/cpp/audioapi/jsi/AudioArrayBuffer.cpp +13 -0
  21. package/common/cpp/audioapi/jsi/AudioArrayBuffer.h +23 -0
  22. package/ios/audioapi/ios/core/AudioDecoder.mm +45 -1
  23. package/lib/module/api.js +1 -1
  24. package/lib/module/api.js.map +1 -1
  25. package/lib/module/api.web.js +1 -2
  26. package/lib/module/api.web.js.map +1 -1
  27. package/lib/module/core/AnalyserNode.js.map +1 -1
  28. package/lib/module/core/AudioBuffer.js.map +1 -1
  29. package/lib/module/core/AudioBufferSourceNode.js +0 -6
  30. package/lib/module/core/AudioBufferSourceNode.js.map +1 -1
  31. package/lib/module/core/AudioParam.js.map +1 -1
  32. package/lib/module/core/BaseAudioContext.js +6 -2
  33. package/lib/module/core/BaseAudioContext.js.map +1 -1
  34. package/lib/module/core/BiquadFilterNode.js.map +1 -1
  35. package/lib/module/utils/index.js +6 -0
  36. package/lib/module/utils/index.js.map +1 -0
  37. package/lib/module/web-core/AnalyserNode.js +4 -20
  38. package/lib/module/web-core/AnalyserNode.js.map +1 -1
  39. package/lib/module/web-core/AudioBuffer.js +2 -6
  40. package/lib/module/web-core/AudioBuffer.js.map +1 -1
  41. package/lib/module/web-core/AudioBufferSourceNode.js +161 -21
  42. package/lib/module/web-core/AudioBufferSourceNode.js.map +1 -1
  43. package/lib/module/web-core/AudioContext.js +10 -8
  44. package/lib/module/web-core/AudioContext.js.map +1 -1
  45. package/lib/module/web-core/AudioParam.js +1 -1
  46. package/lib/module/web-core/AudioParam.js.map +1 -1
  47. package/lib/module/web-core/BiquadFilterNode.js +1 -9
  48. package/lib/module/web-core/BiquadFilterNode.js.map +1 -1
  49. package/lib/module/web-core/custom/signalsmithStretch/SignalsmithStretch.mjs +2 -2
  50. package/lib/module/web-core/custom/signalsmithStretch/SignalsmithStretch.mjs.map +1 -1
  51. package/lib/typescript/api.d.ts +1 -1
  52. package/lib/typescript/api.d.ts.map +1 -1
  53. package/lib/typescript/api.web.d.ts +1 -2
  54. package/lib/typescript/api.web.d.ts.map +1 -1
  55. package/lib/typescript/core/AnalyserNode.d.ts +4 -4
  56. package/lib/typescript/core/AnalyserNode.d.ts.map +1 -1
  57. package/lib/typescript/core/AudioBuffer.d.ts +3 -3
  58. package/lib/typescript/core/AudioBuffer.d.ts.map +1 -1
  59. package/lib/typescript/core/AudioBufferSourceNode.d.ts +0 -3
  60. package/lib/typescript/core/AudioBufferSourceNode.d.ts.map +1 -1
  61. package/lib/typescript/core/AudioParam.d.ts +1 -1
  62. package/lib/typescript/core/AudioParam.d.ts.map +1 -1
  63. package/lib/typescript/core/BaseAudioContext.d.ts +4 -3
  64. package/lib/typescript/core/BaseAudioContext.d.ts.map +1 -1
  65. package/lib/typescript/core/BiquadFilterNode.d.ts +1 -1
  66. package/lib/typescript/core/BiquadFilterNode.d.ts.map +1 -1
  67. package/lib/typescript/interfaces.d.ts +13 -13
  68. package/lib/typescript/interfaces.d.ts.map +1 -1
  69. package/lib/typescript/types.d.ts +3 -1
  70. package/lib/typescript/types.d.ts.map +1 -1
  71. package/lib/typescript/utils/index.d.ts +2 -0
  72. package/lib/typescript/utils/index.d.ts.map +1 -0
  73. package/lib/typescript/web-core/AnalyserNode.d.ts +4 -4
  74. package/lib/typescript/web-core/AnalyserNode.d.ts.map +1 -1
  75. package/lib/typescript/web-core/AudioBuffer.d.ts +2 -2
  76. package/lib/typescript/web-core/AudioBuffer.d.ts.map +1 -1
  77. package/lib/typescript/web-core/AudioBufferSourceNode.d.ts +58 -6
  78. package/lib/typescript/web-core/AudioBufferSourceNode.d.ts.map +1 -1
  79. package/lib/typescript/web-core/AudioContext.d.ts +4 -5
  80. package/lib/typescript/web-core/AudioContext.d.ts.map +1 -1
  81. package/lib/typescript/web-core/AudioParam.d.ts +1 -1
  82. package/lib/typescript/web-core/AudioParam.d.ts.map +1 -1
  83. package/lib/typescript/web-core/BaseAudioContext.d.ts +3 -2
  84. package/lib/typescript/web-core/BaseAudioContext.d.ts.map +1 -1
  85. package/lib/typescript/web-core/BiquadFilterNode.d.ts +1 -1
  86. package/lib/typescript/web-core/BiquadFilterNode.d.ts.map +1 -1
  87. package/package.json +6 -6
  88. package/src/api.ts +0 -1
  89. package/src/api.web.ts +0 -2
  90. package/src/core/AnalyserNode.ts +4 -4
  91. package/src/core/AudioBuffer.ts +3 -3
  92. package/src/core/AudioBufferSourceNode.ts +0 -9
  93. package/src/core/AudioParam.ts +1 -1
  94. package/src/core/BaseAudioContext.ts +22 -5
  95. package/src/core/BiquadFilterNode.ts +3 -3
  96. package/src/interfaces.ts +15 -16
  97. package/src/types.ts +3 -1
  98. package/src/utils/index.ts +3 -0
  99. package/src/web-core/AnalyserNode.tsx +8 -30
  100. package/src/web-core/AudioBuffer.tsx +4 -14
  101. package/src/web-core/AudioBufferSourceNode.tsx +357 -31
  102. package/src/web-core/AudioContext.tsx +23 -13
  103. package/src/web-core/AudioParam.tsx +2 -6
  104. package/src/web-core/BaseAudioContext.tsx +4 -3
  105. package/src/web-core/BiquadFilterNode.tsx +6 -16
  106. package/src/web-core/custom/signalsmithStretch/SignalsmithStretch.mjs +4 -3
  107. package/common/cpp/audioapi/core/types/TimeStretchType.h +0 -7
  108. package/lib/module/web-core/StretcherNode.js +0 -81
  109. package/lib/module/web-core/StretcherNode.js.map +0 -1
  110. package/lib/module/web-core/custom/signalsmithStretch/SignalsmithStretch.js +0 -823
  111. package/lib/module/web-core/custom/signalsmithStretch/SignalsmithStretch.js.map +0 -1
  112. package/lib/typescript/web-core/StretcherNode.d.ts +0 -48
  113. package/lib/typescript/web-core/StretcherNode.d.ts.map +0 -1
  114. package/src/web-core/StretcherNode.tsx +0 -145
  115. package/src/web-core/custom/signalsmithStretch/SignalsmithStretch.js +0 -946
@@ -17,7 +17,7 @@ export default class AudioBuffer {
17
17
  this.numberOfChannels = buffer.numberOfChannels;
18
18
  }
19
19
 
20
- public getChannelData(channel: number): number[] {
20
+ public getChannelData(channel: number): Float32Array {
21
21
  if (channel < 0 || channel >= this.numberOfChannels) {
22
22
  throw new IndexSizeError(
23
23
  `The channel number provided (${channel}) is outside the range [0, ${this.numberOfChannels - 1}]`
@@ -27,7 +27,7 @@ export default class AudioBuffer {
27
27
  }
28
28
 
29
29
  public copyFromChannel(
30
- destination: number[],
30
+ destination: Float32Array,
31
31
  channelNumber: number,
32
32
  startInChannel: number = 0
33
33
  ): void {
@@ -47,7 +47,7 @@ export default class AudioBuffer {
47
47
  }
48
48
 
49
49
  public copyToChannel(
50
- source: number[],
50
+ source: Float32Array,
51
51
  channelNumber: number,
52
52
  startInChannel: number = 0
53
53
  ): void {
@@ -4,7 +4,6 @@ import BaseAudioContext from './BaseAudioContext';
4
4
  import AudioBuffer from './AudioBuffer';
5
5
  import AudioParam from './AudioParam';
6
6
  import { InvalidStateError, RangeError } from '../errors';
7
- import { TimeStretchType } from '../types';
8
7
 
9
8
  export default class AudioBufferSourceNode extends AudioScheduledSourceNode {
10
9
  readonly playbackRate: AudioParam;
@@ -58,14 +57,6 @@ export default class AudioBufferSourceNode extends AudioScheduledSourceNode {
58
57
  (this.node as IAudioBufferSourceNode).loopEnd = value;
59
58
  }
60
59
 
61
- public get timeStretch(): TimeStretchType {
62
- return (this.node as IAudioBufferSourceNode).timeStretch;
63
- }
64
-
65
- public set timeStretch(value: TimeStretchType) {
66
- (this.node as IAudioBufferSourceNode).timeStretch = value;
67
- }
68
-
69
60
  public start(when: number = 0, offset: number = 0, duration?: number): void {
70
61
  if (when < 0) {
71
62
  throw new RangeError(
@@ -85,7 +85,7 @@ export default class AudioParam {
85
85
  }
86
86
 
87
87
  public setValueCurveAtTime(
88
- values: number[],
88
+ values: Float32Array,
89
89
  startTime: number,
90
90
  duration: number
91
91
  ): AudioParam {
@@ -1,5 +1,9 @@
1
1
  import { IBaseAudioContext } from '../interfaces';
2
- import { ContextState, PeriodicWaveConstraints } from '../types';
2
+ import {
3
+ ContextState,
4
+ PeriodicWaveConstraints,
5
+ AudioBufferSourceNodeOptions,
6
+ } from '../types';
3
7
  import AudioDestinationNode from './AudioDestinationNode';
4
8
  import OscillatorNode from './OscillatorNode';
5
9
  import GainNode from './GainNode';
@@ -46,8 +50,15 @@ export default class BaseAudioContext {
46
50
  return new BiquadFilterNode(this, this.context.createBiquadFilter());
47
51
  }
48
52
 
49
- createBufferSource(): AudioBufferSourceNode {
50
- return new AudioBufferSourceNode(this, this.context.createBufferSource());
53
+ createBufferSource(
54
+ options?: AudioBufferSourceNodeOptions
55
+ ): AudioBufferSourceNode {
56
+ const pitchCorrection = options?.pitchCorrection ?? false;
57
+
58
+ return new AudioBufferSourceNode(
59
+ this,
60
+ this.context.createBufferSource(pitchCorrection)
61
+ );
51
62
  }
52
63
 
53
64
  createBuffer(
@@ -79,8 +90,8 @@ export default class BaseAudioContext {
79
90
  }
80
91
 
81
92
  createPeriodicWave(
82
- real: number[],
83
- imag: number[],
93
+ real: Float32Array,
94
+ imag: Float32Array,
84
95
  constraints?: PeriodicWaveConstraints
85
96
  ): PeriodicWave {
86
97
  if (real.length !== imag.length) {
@@ -110,4 +121,10 @@ export default class BaseAudioContext {
110
121
  await this.context.decodeAudioDataSource(sourcePath)
111
122
  );
112
123
  }
124
+
125
+ async decodeAudioData(arrayBuffer: ArrayBuffer): Promise<AudioBuffer> {
126
+ return new AudioBuffer(
127
+ await this.context.decodeAudioData(new Uint8Array(arrayBuffer))
128
+ );
129
+ }
113
130
  }
@@ -28,9 +28,9 @@ export default class BiquadFilterNode extends AudioNode {
28
28
  }
29
29
 
30
30
  public getFrequencyResponse(
31
- frequencyArray: number[],
32
- magResponseOutput: number[],
33
- phaseResponseOutput: number[]
31
+ frequencyArray: Float32Array,
32
+ magResponseOutput: Float32Array,
33
+ phaseResponseOutput: Float32Array
34
34
  ) {
35
35
  if (
36
36
  frequencyArray.length !== magResponseOutput.length ||
package/src/interfaces.ts CHANGED
@@ -5,7 +5,6 @@ import {
5
5
  ChannelCountMode,
6
6
  ChannelInterpretation,
7
7
  WindowType,
8
- TimeStretchType,
9
8
  } from './types';
10
9
 
11
10
  export interface AudioAPIInstaller {
@@ -22,19 +21,20 @@ export interface IBaseAudioContext {
22
21
  createGain(): IGainNode;
23
22
  createStereoPanner(): IStereoPannerNode;
24
23
  createBiquadFilter: () => IBiquadFilterNode;
25
- createBufferSource: () => IAudioBufferSourceNode;
24
+ createBufferSource: (pitchCorrection: boolean) => IAudioBufferSourceNode;
26
25
  createBuffer: (
27
26
  channels: number,
28
27
  length: number,
29
28
  sampleRate: number
30
29
  ) => IAudioBuffer;
31
30
  createPeriodicWave: (
32
- real: number[],
33
- imag: number[],
31
+ real: Float32Array,
32
+ imag: Float32Array,
34
33
  disableNormalization: boolean
35
34
  ) => IPeriodicWave;
36
35
  createAnalyser: () => IAnalyserNode;
37
36
  decodeAudioDataSource: (sourcePath: string) => Promise<IAudioBuffer>;
37
+ decodeAudioData: (arrayBuffer: ArrayBuffer) => Promise<IAudioBuffer>;
38
38
  }
39
39
 
40
40
  export interface IAudioContext extends IBaseAudioContext {
@@ -71,9 +71,9 @@ export interface IBiquadFilterNode extends IAudioNode {
71
71
  type: BiquadFilterType;
72
72
 
73
73
  getFrequencyResponse(
74
- frequencyArray: number[],
75
- magResponseOutput: number[],
76
- phaseResponseOutput: number[]
74
+ frequencyArray: Float32Array,
75
+ magResponseOutput: Float32Array,
76
+ phaseResponseOutput: Float32Array
77
77
  ): void;
78
78
  }
79
79
 
@@ -100,7 +100,6 @@ export interface IAudioBufferSourceNode extends IAudioScheduledSourceNode {
100
100
  loopEnd: number;
101
101
  detune: IAudioParam;
102
102
  playbackRate: IAudioParam;
103
- timeStretch: TimeStretchType;
104
103
 
105
104
  start: (when?: number, offset?: number, duration?: number) => void;
106
105
  }
@@ -111,14 +110,14 @@ export interface IAudioBuffer {
111
110
  readonly sampleRate: number;
112
111
  readonly numberOfChannels: number;
113
112
 
114
- getChannelData(channel: number): number[];
113
+ getChannelData(channel: number): Float32Array;
115
114
  copyFromChannel(
116
- destination: number[],
115
+ destination: Float32Array,
117
116
  channelNumber: number,
118
117
  startInChannel: number
119
118
  ): void;
120
119
  copyToChannel(
121
- source: number[],
120
+ source: Float32Array,
122
121
  channelNumber: number,
123
122
  startInChannel: number
124
123
  ): void;
@@ -139,7 +138,7 @@ export interface IAudioParam {
139
138
  timeConstant: number
140
139
  ) => void;
141
140
  setValueCurveAtTime: (
142
- values: number[],
141
+ values: Float32Array,
143
142
  startTime: number,
144
143
  duration: number
145
144
  ) => void;
@@ -157,8 +156,8 @@ export interface IAnalyserNode extends IAudioNode {
157
156
  smoothingTimeConstant: number;
158
157
  window: WindowType;
159
158
 
160
- getFloatFrequencyData: (array: number[]) => void;
161
- getByteFrequencyData: (array: number[]) => void;
162
- getFloatTimeDomainData: (array: number[]) => void;
163
- getByteTimeDomainData: (array: number[]) => void;
159
+ getFloatFrequencyData: (array: Float32Array) => void;
160
+ getByteFrequencyData: (array: Uint8Array) => void;
161
+ getFloatTimeDomainData: (array: Float32Array) => void;
162
+ getByteTimeDomainData: (array: Uint8Array) => void;
164
163
  }
package/src/types.ts CHANGED
@@ -31,4 +31,6 @@ export interface AudioContextOptions {
31
31
 
32
32
  export type WindowType = 'blackman' | 'hann';
33
33
 
34
- export type TimeStretchType = 'linear' | 'speech-music';
34
+ export interface AudioBufferSourceNodeOptions {
35
+ pitchCorrection: boolean;
36
+ }
@@ -0,0 +1,3 @@
1
+ export function clamp(value: number, min: number, max: number): number {
2
+ return Math.min(Math.max(value, min), max);
3
+ }
@@ -29,41 +29,19 @@ export default class AnalyserNode extends AudioNode {
29
29
  );
30
30
  }
31
31
 
32
- public getByteFrequencyData(array: number[]): void {
33
- const data = new Uint8Array(array);
34
-
35
- (this.node as globalThis.AnalyserNode).getByteFrequencyData(data);
36
-
37
- for (let i = 0; i < array.length; i++) {
38
- array[i] = data[i];
39
- }
32
+ public getByteFrequencyData(array: Uint8Array): void {
33
+ (this.node as globalThis.AnalyserNode).getByteFrequencyData(array);
40
34
  }
41
35
 
42
- public getByteTimeDomainData(array: number[]): void {
43
- const data = new Uint8Array(array);
44
-
45
- (this.node as globalThis.AnalyserNode).getByteTimeDomainData(data);
46
-
47
- for (let i = 0; i < array.length; i++) {
48
- array[i] = data[i];
49
- }
36
+ public getByteTimeDomainData(array: Uint8Array): void {
37
+ (this.node as globalThis.AnalyserNode).getByteTimeDomainData(array);
50
38
  }
51
39
 
52
- public getFloatFrequencyData(array: number[]): void {
53
- const data = new Float32Array(array);
54
- (this.node as globalThis.AnalyserNode).getFloatFrequencyData(data);
55
-
56
- for (let i = 0; i < array.length; i++) {
57
- array[i] = data[i];
58
- }
40
+ public getFloatFrequencyData(array: Float32Array): void {
41
+ (this.node as globalThis.AnalyserNode).getFloatFrequencyData(array);
59
42
  }
60
43
 
61
- public getFloatTimeDomainData(array: number[]): void {
62
- const data = new Float32Array(array);
63
- (this.node as globalThis.AnalyserNode).getFloatTimeDomainData(data);
64
-
65
- for (let i = 0; i < array.length; i++) {
66
- array[i] = data[i];
67
- }
44
+ public getFloatTimeDomainData(array: Float32Array): void {
45
+ (this.node as globalThis.AnalyserNode).getFloatTimeDomainData(array);
68
46
  }
69
47
  }
@@ -28,7 +28,7 @@ export default class AudioBuffer {
28
28
  }
29
29
 
30
30
  public copyFromChannel(
31
- destination: number[],
31
+ destination: Float32Array,
32
32
  channelNumber: number,
33
33
  startInChannel: number = 0
34
34
  ): void {
@@ -44,17 +44,11 @@ export default class AudioBuffer {
44
44
  );
45
45
  }
46
46
 
47
- const array = new Float32Array(destination);
48
-
49
- this.buffer.copyFromChannel(array, channelNumber, startInChannel);
50
-
51
- for (let i = 0; i < destination.length; i++) {
52
- destination[i] = array[i];
53
- }
47
+ this.buffer.copyFromChannel(destination, channelNumber, startInChannel);
54
48
  }
55
49
 
56
50
  public copyToChannel(
57
- source: number[],
51
+ source: Float32Array,
58
52
  channelNumber: number,
59
53
  startInChannel: number = 0
60
54
  ): void {
@@ -70,10 +64,6 @@ export default class AudioBuffer {
70
64
  );
71
65
  }
72
66
 
73
- this.buffer.copyToChannel(
74
- new Float32Array(source),
75
- channelNumber,
76
- startInChannel
77
- );
67
+ this.buffer.copyToChannel(source, channelNumber, startInChannel);
78
68
  }
79
69
  }