Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/audio file and piano #232

Merged
merged 5 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions apps/common-app/src/components/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ import { colors, layout } from '../styles';
interface ButtonProps {
title: string;
onPress: () => void;
disabled?: boolean;
width?: number;
}

const Button: FC<ButtonProps> = (props) => {
const { title, onPress } = props;

const Button: FC<ButtonProps> = ({
title,
onPress,
disabled = false,
width = 100,
}) => {
return (
<Pressable
disabled={disabled}
onPress={onPress}
style={({ pressed }) => [
styles.button,
{ backgroundColor: pressed ? `${colors.main}88` : colors.main },
{
backgroundColor: pressed ? `${colors.main}88` : colors.main,
opacity: disabled ? 0.5 : 1,
width: width,
},
]}
>
<Text style={styles.text}>{title}</Text>
Expand All @@ -28,7 +38,6 @@ const styles = StyleSheet.create({
button: {
padding: layout.spacing,
borderRadius: layout.radius,
width: 100,
},
text: {
color: colors.white,
Expand Down
15 changes: 11 additions & 4 deletions apps/common-app/src/examples/AudioFile/AudioFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ActivityIndicator } from 'react-native';

const AudioFile: FC = () => {
const [isPlaying, setIsPlaying] = useState(false);
const [isLoading, setIsLoading] = useState(false);

const audioContextRef = useRef<AudioContext | null>(null);
const audioBufferSourceNodeRef = useRef<AudioBufferSourceNode | null>(null);
Expand Down Expand Up @@ -38,9 +39,10 @@ const AudioFile: FC = () => {
if (result.canceled === false) {
audioBufferSourceNodeRef.current?.stop();
setIsPlaying(false);
setAudioBuffer(null);

setIsLoading(true);
await fetchAudioBuffer(result.assets[0].uri.replace('file://', ''));
setIsLoading(false);
}
} catch (error) {
console.error('Error picking file:', error);
Expand Down Expand Up @@ -86,12 +88,17 @@ const AudioFile: FC = () => {

return (
<Container centered>
<Button title={isPlaying ? 'Stop' : 'Play'} onPress={handlePress} />
{!audioBuffer && <ActivityIndicator color="#FFFFFF" />}
<Spacer.Vertical size={20} />
<Button
title="Set audio source from file"
onPress={handleSetAudioSourceFromFile}
width={200}
/>
{isLoading && <ActivityIndicator color="#FFFFFF" />}
<Spacer.Vertical size={20} />
<Button
title={isPlaying ? 'Stop' : 'Play'}
onPress={handlePress}
disabled={!audioBuffer ? true : false}
/>
</Container>
);
Expand Down
13 changes: 11 additions & 2 deletions apps/common-app/src/examples/Piano/Piano.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FC, useEffect, useRef } from 'react';
import { AudioContext, AudioBuffer } from 'react-native-audio-api';
import * as FileSystem from 'expo-file-system';

import { Container } from '../../components';
import { KeyName, sources, keyMap } from './utils';
Expand Down Expand Up @@ -27,8 +28,16 @@ const Piano: FC = () => {
}

Object.entries(sources).forEach(async ([key, url]) => {
bufferListRef.current[key as KeyName] =
await audioContextRef.current!.decodeAudioDataSource(url);
bufferListRef.current[key as KeyName] = await FileSystem.downloadAsync(
url,
FileSystem.documentDirectory + key.replace('#', 's') + '.mp3'
)
.then(({ uri }) => {
return uri.replace('file://', '');
})
.then((uri) => {
return audioContextRef.current!.decodeAudioDataSource(uri);
});
});

const newNotes: Partial<Record<KeyName, PianoNote>> = {};
Expand Down
9 changes: 6 additions & 3 deletions apps/fabric-example/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
presets: ['module:@react-native/babel-preset'],
plugins: ['react-native-reanimated/plugin', 'module:react-native-dotenv'],
module.exports = function (api) {
api.cache(false);
return {
presets: ['module:@react-native/babel-preset'],
plugins: ['react-native-reanimated/plugin', 'module:react-native-dotenv'],
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ - (int)getBufferSizeInFrames
// which is safer to base our internal AudioBus sizes.
// Buut no documentation => no guarantee :)
// If something is crackling when it should play silence, start here 📻
double maxBufferDuration = fmax(0.02, fmax(self.audioSession.IOBufferDuration, self.audioSession.preferredIOBufferDuration));
double maxBufferDuration =
fmax(0.02, fmax(self.audioSession.IOBufferDuration, self.audioSession.preferredIOBufferDuration));
return (int)(maxBufferDuration * self.audioSession.sampleRate + 1);
}

Expand Down
Loading