forked from AgoraIO-Extensions/react-native-agora
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoiceChanger.tsx
More file actions
377 lines (357 loc) · 10.2 KB
/
Copy pathVoiceChanger.tsx
File metadata and controls
377 lines (357 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import React, { Component, useState } from 'react';
import {
Button,
PermissionsAndroid,
Platform,
ScrollView,
StyleSheet,
Text,
TextInput,
View,
} from 'react-native';
import Slider from '@react-native-community/slider';
import { Picker } from '@react-native-picker/picker';
import RtcEngine, {
AudioEffectPreset,
AudioEqualizationBandFrequency,
AudioProfile,
AudioReverbType,
AudioScenario,
ChannelProfile,
ClientRole,
RtcEngineContext,
VoiceBeautifierPreset,
} from 'react-native-agora';
import Item from '../../../components/Item';
const config = require('../../../config/agora.config.json');
interface State {
channelId: string;
isJoined: boolean;
isBeautifierOnly: boolean;
}
export default class VoiceChanger extends Component<{}, State, any> {
_engine?: RtcEngine;
_voiceBeautifierPreset: VoiceBeautifierPreset = 0;
_audioEffectPreset: AudioEffectPreset = 0;
_param1: number = 0;
_param2: number = 0;
_audioReverbType: AudioReverbType = 0;
_audioReverbValue: number = 0;
_audioEqualizationBandFrequency: AudioEqualizationBandFrequency = 0;
_bandGain: number = 0;
_localVoicePitch: number = 0;
constructor(props: {}) {
super(props);
this.state = {
channelId: config.channelId,
isJoined: false,
isBeautifierOnly: false,
};
}
UNSAFE_componentWillMount() {
this._initEngine();
}
componentWillUnmount() {
this._engine?.destroy();
}
_initEngine = async () => {
if (Platform.OS === 'android') {
await PermissionsAndroid.request('android.permission.RECORD_AUDIO');
}
this._engine = await RtcEngine.createWithContext(
new RtcEngineContext(config.appId)
);
this._addListeners();
// Before calling the method, you need to set the profile
// parameter of setAudioProfile to AUDIO_PROFILE_MUSIC_HIGH_QUALITY(4)
// or AUDIO_PROFILE_MUSIC_HIGH_QUALITY_STEREO(5), and to set
// scenario parameter to AUDIO_SCENARIO_GAME_STREAMING(3)
await this._engine.setAudioProfile(
AudioProfile.MusicHighQualityStereo,
AudioScenario.GameStreaming
);
// make myself a broadcaster
await this._engine.setChannelProfile(ChannelProfile.LiveBroadcasting);
await this._engine?.setClientRole(ClientRole.Broadcaster);
// disable video module
await this._engine?.disableVideo();
// Set audio route to speaker
await this._engine.setDefaultAudioRoutetoSpeakerphone(true);
};
_addListeners = () => {
this._engine?.addListener('Warning', (warningCode) => {
console.info('Warning', warningCode);
});
this._engine?.addListener('Error', (errorCode) => {
console.info('Error', errorCode);
});
this._engine?.addListener('JoinChannelSuccess', (channel, uid, elapsed) => {
console.info('JoinChannelSuccess', channel, uid, elapsed);
// RtcLocalView.SurfaceView must render after engine init and channel join
this.setState({ isJoined: true });
});
this._engine?.addListener('LeaveChannel', (stats) => {
console.info('LeaveChannel', stats);
// RtcLocalView.SurfaceView must render after engine init and channel join
this.setState({ isJoined: false });
});
};
_joinChannel = async () => {
// start joining channel
// 1. Users can only see each other after they join the
// same channel successfully using the same app id.
// 2. If app certificate is turned on at dashboard, token is needed
// when joining channel. The channel name and uid used to calculate
// the token has to match the ones used for channel join
await this._engine?.joinChannel(
config.token,
this.state.channelId,
null,
config.uid
);
};
_leaveChannel = async () => {
await this._engine?.leaveChannel();
};
render() {
const { channelId, isJoined, isBeautifierOnly } = this.state;
return (
<View style={styles.container}>
<View style={styles.top}>
<TextInput
style={styles.input}
onChangeText={(text) => this.setState({ channelId: text })}
placeholder={'Channel ID'}
value={channelId}
/>
<Item
title={'setVoiceBeautifierPreset Only'}
isShowSwitch
onSwitchValueChange={(value) => {
this.setState({ isBeautifierOnly: value });
}}
/>
<Button
onPress={isJoined ? this._leaveChannel : this._joinChannel}
title={`${isJoined ? 'Leave' : 'Join'} channel`}
/>
</View>
{isJoined && (
<ScrollView>
{isBeautifierOnly ? this._renderBeautifier() : this._renderOthers()}
</ScrollView>
)}
</View>
);
}
_renderBeautifier = () => {
return (
<View style={styles.container}>
<PickerView
alertTitle={'Select AudioEffectPreset:'}
type={VoiceBeautifierPreset}
onPress={(value: VoiceBeautifierPreset) => {
this._voiceBeautifierPreset = value;
}}
/>
<Button
title={'Set Voice Beautifier Preset'}
onPress={() => {
this._engine?.setVoiceBeautifierPreset(this._voiceBeautifierPreset);
}}
/>
<Text>{'param1'}</Text>
<Slider
minimumValue={0}
maximumValue={10}
step={1}
onValueChange={(value) => {
this._param1 = value;
}}
/>
<Text>{'param2'}</Text>
<Slider
minimumValue={0}
maximumValue={10}
step={1}
onValueChange={(value) => {
this._param2 = value;
}}
/>
<Button
title={'Set Voice Beautifier Parameters'}
onPress={() => {
this._engine?.setVoiceBeautifierParameters(
this._voiceBeautifierPreset,
this._param1,
this._param2
);
}}
/>
</View>
);
};
_renderOthers = () => {
return (
<View style={styles.container}>
<View>
<PickerView
alertTitle={'Select AudioEffectPreset:'}
type={AudioEffectPreset}
onPress={(value: AudioEffectPreset) => {
this._audioEffectPreset = value;
}}
/>
<Button
title={'Set Audio Effect Preset'}
onPress={() => {
this._engine?.setAudioEffectPreset(this._audioEffectPreset);
}}
/>
<Text>{'param1'}</Text>
<Slider
minimumValue={0}
maximumValue={10}
step={1}
onValueChange={(value) => {
this._param1 = value;
}}
/>
<Text>{'param2'}</Text>
<Slider
minimumValue={0}
maximumValue={10}
step={1}
onValueChange={(value) => {
this._param2 = value;
}}
/>
<Button
title={'Set Audio Effect Parameters'}
onPress={() => {
this._engine?.setAudioEffectParameters(
this._audioEffectPreset,
this._param1,
this._param2
);
}}
/>
</View>
<View>
<PickerView
alertTitle={'Select AudioReverbType:'}
type={AudioReverbType}
onPress={(value: AudioReverbType) => {
this._audioReverbType = value;
}}
/>
<Text>{'Select Local Voice Reverb Value:'}</Text>
<Slider
minimumValue={-20}
maximumValue={10}
step={1}
onValueChange={(value) => {
this._audioReverbValue = value;
}}
/>
<Button
title={'Set Local Voice Reverb'}
onPress={() => {
this._engine?.setLocalVoiceReverb(
this._audioReverbType,
this._audioReverbValue
);
}}
/>
</View>
<View>
<PickerView
alertTitle={'Select AudioEqualizationBandFrequency:'}
type={AudioEqualizationBandFrequency}
onPress={(value: AudioEqualizationBandFrequency) => {
this._audioEqualizationBandFrequency = value;
}}
/>
<Text>{'Select Local Voice Equalization Value:'}</Text>
<Slider
minimumValue={-15}
maximumValue={15}
step={1}
onValueChange={(value) => {
this._bandGain = value;
}}
/>
<Button
title={'Set Local Voice Equalization'}
onPress={() => {
this._engine?.setLocalVoiceEqualization(
this._audioEqualizationBandFrequency,
this._bandGain
);
}}
/>
</View>
<View>
<Text>{'Select Local Voice Pitch Value:'}</Text>
<Slider
minimumValue={0.5}
maximumValue={2.0}
step={0.1}
onValueChange={(value) => {
this._localVoicePitch = value;
}}
/>
<Button
title={'Set Local Voice Pitch'}
onPress={() => {
this._engine?.setLocalVoicePitch(this._localVoicePitch);
}}
/>
</View>
</View>
);
};
}
const PickerView = ({
alertTitle,
type,
onPress,
}: {
alertTitle: string;
type: any;
onPress: Function;
}) => {
const items = Object.values(type);
const keys = items.filter((v) => typeof v === 'string') as string[];
const values = items.filter((v) => typeof v === 'number') as number[];
const [value, setValue] = useState(values[0]);
return (
<View>
<Text>{alertTitle}</Text>
<Picker
selectedValue={value}
onValueChange={(itemValue) => {
setValue(itemValue);
onPress(itemValue);
}}
>
{keys.map((v, i) => (
<Picker.Item key={i} label={v} value={values[i]} />
))}
</Picker>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
top: {
width: '100%',
},
input: {
borderColor: 'gray',
borderWidth: 1,
color: 'black',
},
});