Skip to content

Commit a45124e

Browse files
author
Maciej Makowski
committed
feat: added support for require based audio source
1 parent 1c00c5f commit a45124e

File tree

6 files changed

+39
-42
lines changed

6 files changed

+39
-42
lines changed

apps/common-app/src/examples/AudioFile/AudioFile.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ const AudioFile: FC = () => {
3434
}
3535

3636
setAudioBuffer(
37-
audioContextRef.current.decodeAudioDataSource(
38-
'/Users/maciejmakowski/projects/react-native-audio-api/apps/common-app/src/examples/AudioFile/runaway_kanye_west.mp3'
39-
)
4037
// audioContextRef.current.decodeAudioDataSource(
41-
// 'https://audio-ssl.itunes.apple.com/apple-assets-us-std-000001/AudioPreview18/v4/9c/db/54/9cdb54b3-5c52-3063-b1ad-abe42955edb5/mzaf_520282131402737225.plus.aac.p.m4a'
38+
// '/Users/maciejmakowski/projects/react-native-audio-api/apps/common-app/src/examples/AudioFile/runaway_kanye_west.mp3'
4239
// )
40+
audioContextRef.current.decodeAudioDataSource(
41+
'https://audio-ssl.itunes.apple.com/apple-assets-us-std-000001/AudioPreview18/v4/9c/db/54/9cdb54b3-5c52-3063-b1ad-abe42955edb5/mzaf_520282131402737225.plus.aac.p.m4a'
42+
)
4343
);
4444
};
4545

packages/react-native-audio-api/ios/AudioDecoder/AudioDecoder.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
@interface AudioDecoder : NSObject
77

88
@property (nonatomic, strong) AVAudioPCMBuffer *buffer;
9-
@property (nonatomic, strong) AVAudioFormat *format;
109
@property (nonatomic, assign) int sampleRate;
1110

1211
- (instancetype)initWithSampleRate:(int)sampleRate;

packages/react-native-audio-api/ios/AudioDecoder/AudioDecoder.m

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ - (instancetype)initWithSampleRate:(int)sampleRate
66
{
77
if (self = [super init]) {
88
self.sampleRate = sampleRate;
9-
self.format = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32
10-
sampleRate:self.sampleRate
11-
channels:2
12-
interleaved:NO];
139
}
1410
return self;
1511
}
@@ -20,13 +16,15 @@ - (const AudioBufferList *)decode:(NSString *)pathOrURL
2016
NSURL *url = [NSURL URLWithString:pathOrURL];
2117

2218
if (url && url.scheme) {
23-
return [self decodeWithURL:url];
19+
self.buffer = [self decodeWithURL:url];
2420
} else {
25-
return [self decodeWithFilePath:pathOrURL];
21+
self.buffer = [self decodeWithFilePath:pathOrURL];
2622
}
23+
24+
return self.buffer.audioBufferList;
2725
}
2826

29-
- (const AudioBufferList *)decodeWithFilePath:(NSString *)path
27+
- (AVAudioPCMBuffer *)decodeWithFilePath:(NSString *)path
3028
{
3129
NSError *error = nil;
3230
NSURL *fileURL = [NSURL fileURLWithPath:path];
@@ -36,23 +34,28 @@ - (const AudioBufferList *)decodeWithFilePath:(NSString *)path
3634
NSLog(@"Error occurred while opening the audio file: %@", [error localizedDescription]);
3735
return nil;
3836
}
39-
self.buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:[audioFile processingFormat]
40-
frameCapacity:[audioFile length]];
37+
AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:[audioFile processingFormat]
38+
frameCapacity:[audioFile length]];
39+
40+
AVAudioFormat *format = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32
41+
sampleRate:self.sampleRate
42+
channels:buffer.audioBufferList->mNumberBuffers
43+
interleaved:NO];
4144

42-
[audioFile readIntoBuffer:self.buffer error:&error];
45+
[audioFile readIntoBuffer:buffer error:&error];
4346
if (error) {
4447
NSLog(@"Error occurred while reading the audio file: %@", [error localizedDescription]);
4548
return nil;
4649
}
4750

4851
if (self.sampleRate != audioFile.processingFormat.sampleRate) {
49-
[self convertFromFormat:self.buffer.format];
52+
return [self convertBuffer:buffer ToFormat:format];
5053
}
5154

52-
return self.buffer.audioBufferList;
55+
return buffer;
5356
}
5457

55-
- (const AudioBufferList *)decodeWithURL:(NSURL *)url
58+
- (AVAudioPCMBuffer *)decodeWithURL:(NSURL *)url
5659
{
5760
__block NSURL *tempFileURL = nil;
5861

@@ -127,20 +130,19 @@ - (void)downloadFileFromURL:(NSURL *)url completion:(void (^)(NSURL *tempFileURL
127130
[downloadTask resume];
128131
}
129132

130-
- (void)convertFromFormat:(AVAudioFormat *)format
133+
- (AVAudioPCMBuffer *)convertBuffer:(AVAudioPCMBuffer *)buffer ToFormat:(AVAudioFormat *)format
131134
{
132135
NSError *error = nil;
133-
AVAudioConverter *converter = [[AVAudioConverter alloc] initFromFormat:format toFormat:self.format];
136+
AVAudioConverter *converter = [[AVAudioConverter alloc] initFromFormat:buffer.format toFormat:format];
134137
AVAudioPCMBuffer *convertedBuffer =
135-
[[AVAudioPCMBuffer alloc] initWithPCMFormat:self.format
136-
frameCapacity:(AVAudioFrameCount)self.buffer.frameCapacity];
138+
[[AVAudioPCMBuffer alloc] initWithPCMFormat:format frameCapacity:(AVAudioFrameCount)buffer.frameCapacity];
137139

138140
AVAudioConverterInputBlock inputBlock =
139141
^AVAudioBuffer *(AVAudioPacketCount inNumberOfPackets, AVAudioConverterInputStatus *outStatus)
140142
{
141-
if (self.buffer.frameLength > 0) {
143+
if (buffer.frameLength > 0) {
142144
*outStatus = AVAudioConverterInputStatus_HaveData;
143-
return self.buffer;
145+
return buffer;
144146
} else {
145147
*outStatus = AVAudioConverterInputStatus_NoDataNow;
146148
return nil;
@@ -151,16 +153,15 @@ - (void)convertFromFormat:(AVAudioFormat *)format
151153

152154
if (error) {
153155
NSLog(@"Error occurred while converting the audio file: %@", [error localizedDescription]);
154-
return;
156+
return nil;
155157
}
156158

157-
self.buffer = convertedBuffer;
159+
return convertedBuffer;
158160
}
159161

160162
- (void)cleanup
161163
{
162164
self.buffer = nil;
163-
self.format = nil;
164165
}
165166

166167
@end

packages/react-native-audio-api/src/core/BaseAudioContext.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IBaseAudioContext } from '../interfaces';
2-
import { ContextState, PeriodicWaveConstraints } from './types';
2+
import { ContextState, PeriodicWaveConstraints, AudioSource } from './types';
33
import AudioDestinationNode from './AudioDestinationNode';
44
import OscillatorNode from './OscillatorNode';
55
import GainNode from './GainNode';
@@ -9,6 +9,7 @@ import AudioBufferSourceNode from './AudioBufferSourceNode';
99
import AudioBuffer from './AudioBuffer';
1010
import PeriodicWave from './PeriodicWave';
1111
import { InvalidAccessError } from '../errors';
12+
import { resolveAudioSource } from '../utils/resolveAudioSource';
1213

1314
export default class BaseAudioContext {
1415
readonly destination: AudioDestinationNode;
@@ -95,7 +96,9 @@ export default class BaseAudioContext {
9596
);
9697
}
9798

98-
decodeAudioDataSource(source: string): AudioBuffer {
99-
return new AudioBuffer(this.context.decodeAudioDataSource(source));
99+
decodeAudioDataSource(source: AudioSource | number): AudioBuffer {
100+
return new AudioBuffer(
101+
this.context.decodeAudioDataSource(resolveAudioSource(source))
102+
);
100103
}
101104
}

packages/react-native-audio-api/src/core/types.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ export interface PeriodicWaveConstraints {
2828
/**
2929
* A type that defines the source of the audio which can be expressed in several forms:
3030
* 1. A string path, which could be an HTTPS URL or a local file path.
31-
* 2. An object specifying more detailed information about the source, including URI and HTTP headers.
31+
* 2. A string, which is a result of resolving an asset source.
3232
*/
33-
export type AudioSource =
34-
| string
35-
| {
36-
uri?: string;
37-
headers?: Record<string, string>;
38-
};
33+
export type AudioSource = string;

packages/react-native-audio-api/src/utils/resolveAudioSource.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { AudioSource } from '../core/types';
2+
// not sure if it works
3+
// @ts-ignore
4+
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
25

36
export function resolveAudioSource(
47
source: AudioSource | string | number
58
): AudioSource {
6-
if (typeof source === 'string') {
7-
return { uri: source };
8-
}
99
if (typeof source === 'number') {
10-
// TODO: Add support for reading from the asset bundle
11-
return 'todo';
10+
return resolveAssetSource(source);
1211
}
1312

1413
return source;

0 commit comments

Comments
 (0)