|
| 1 | +import React from 'react'; |
| 2 | +import {StyleSheet, Button, View, Text} from 'react-native'; |
| 3 | +import {Audio} from 'expo-av'; |
| 4 | +import {FileSystem, Permissions} from 'react-native-unimodules'; |
| 5 | +import hmacSHA1 from 'crypto-js/hmac-sha1'; |
| 6 | +import Base64 from 'crypto-js/enc-base64'; |
| 7 | +import {Buffer} from 'buffer'; |
| 8 | + |
| 9 | +export default class MusicRec_Test extends React.Component { |
| 10 | + constructor(props) { |
| 11 | + super(props); |
| 12 | + this.state = {response: ''}; |
| 13 | + } |
| 14 | + async _findSong() { |
| 15 | + // Audio.setAudioModeAsync() |
| 16 | + const {status} = await Audio.requestPermissionsAsync(); |
| 17 | + console.log('Current Status ' + status); |
| 18 | + const recording = new Audio.Recording(); |
| 19 | + try { |
| 20 | + await Audio.setAudioModeAsync({ |
| 21 | + playsInSilentModeIOS: true, |
| 22 | + allowsRecordingIOS: true, |
| 23 | + }); |
| 24 | + const recordOptions = { |
| 25 | + android: { |
| 26 | + extension: '.m4a', |
| 27 | + outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_MPEG_4, |
| 28 | + audioEncoder: Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC, |
| 29 | + sampleRate: 44100, |
| 30 | + numberOfChannels: 2, |
| 31 | + bitRate: 128000, |
| 32 | + }, |
| 33 | + ios: { |
| 34 | + extension: '.wav', |
| 35 | + audioQuality: Audio.RECORDING_OPTION_IOS_AUDIO_QUALITY_HIGH, |
| 36 | + sampleRate: 8000, |
| 37 | + numberOfChannels: 1, |
| 38 | + linearPCMBitDepth: 16, |
| 39 | + linearPCMIsBigEndian: false, |
| 40 | + linearPCMIsFloat: true, |
| 41 | + }, |
| 42 | + }; |
| 43 | + await recording.prepareToRecordAsync(recordOptions); |
| 44 | + await recording.startAsync(); |
| 45 | + console.log('Recording'); |
| 46 | + await timeout(8000); |
| 47 | + console.log('Done recording'); |
| 48 | + await recording.stopAndUnloadAsync(); |
| 49 | + let recordingFile = recording.getURI(); |
| 50 | + |
| 51 | + let result = await identify(recordingFile, defaultOptions); |
| 52 | + console.log(result); |
| 53 | + //return result; |
| 54 | + } catch (error) { |
| 55 | + console.log(error); |
| 56 | + console.log('Error in this!!!!'); |
| 57 | + } |
| 58 | + } |
| 59 | + render() { |
| 60 | + return ( |
| 61 | + <View style={styles.container}> |
| 62 | + <Button title="Find Song" onPress={this._findSong} /> |
| 63 | + <Text /> |
| 64 | + </View> |
| 65 | + ); |
| 66 | + } |
| 67 | +} |
| 68 | +function timeout(ms) { |
| 69 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 70 | +} |
| 71 | +const defaultOptions = { |
| 72 | + host: 'identify-cn-north-1.acrcloud.com', |
| 73 | + endpoint: '/v1/identify', |
| 74 | + signature_version: '1', |
| 75 | + data_type: 'audio', |
| 76 | + secure: true, |
| 77 | + access_key: 'ffa04326e7218a2cc0f95828e09de997', |
| 78 | + access_secret: 'Wx4oZ4810hcz1jofHb4xMfScGmyvjNkqezn7bBE0', |
| 79 | +}; |
| 80 | +function buildStringToSign( |
| 81 | + method, |
| 82 | + uri, |
| 83 | + accessKey, |
| 84 | + dataType, |
| 85 | + signatureVersion, |
| 86 | + timestamp, |
| 87 | +) { |
| 88 | + return [method, uri, accessKey, dataType, signatureVersion, timestamp].join( |
| 89 | + '\n', |
| 90 | + ); |
| 91 | +} |
| 92 | +function signString(stringToSign, accessSecret) { |
| 93 | + return Base64.stringify(hmacSHA1(stringToSign, accessSecret)); |
| 94 | +} |
| 95 | +async function identify(uri, options) { |
| 96 | + var current_data = new Date(); |
| 97 | + var timestamp = current_data.getTime() / 1000; |
| 98 | + var stringToSign = buildStringToSign( |
| 99 | + 'POST', |
| 100 | + options.endpoint, |
| 101 | + options.access_key, |
| 102 | + options.data_type, |
| 103 | + options.signature_version, |
| 104 | + timestamp, |
| 105 | + ); |
| 106 | + let fileinfo = await FileSystem.getInfoAsync(uri, {size: true}); |
| 107 | + var signature = signString(stringToSign, options.access_secret); |
| 108 | + var formData = { |
| 109 | + sample: {uri: uri, name: 'sample.wav', type: 'audio/wav'}, |
| 110 | + access_key: options.access_key, |
| 111 | + data_type: options.data_type, |
| 112 | + signature_version: options.signature_version, |
| 113 | + signature: signature, |
| 114 | + sample_bytes: fileinfo.size, |
| 115 | + timestamp: timestamp, |
| 116 | + }; |
| 117 | + var form = new FormData(); |
| 118 | + for (let key in formData) { |
| 119 | + form.append(key, formData[key]); |
| 120 | + } |
| 121 | + |
| 122 | + let postOptions = { |
| 123 | + method: 'POST', |
| 124 | + headers: { |
| 125 | + 'Content-Type': 'multipart/form-data', |
| 126 | + }, |
| 127 | + body: form, |
| 128 | + }; |
| 129 | + console.log(postOptions.body); |
| 130 | + let response = await fetch( |
| 131 | + 'http://' + options.host + options.endpoint, |
| 132 | + postOptions, |
| 133 | + ); |
| 134 | + let result = await response.text(); |
| 135 | + console.log(result); |
| 136 | +} |
| 137 | +const styles = StyleSheet.create({ |
| 138 | + container: { |
| 139 | + flex: 1, |
| 140 | + backgroundColor: '#fff', |
| 141 | + alignItems: 'center', |
| 142 | + justifyContent: 'center', |
| 143 | + }, |
| 144 | +}); |
0 commit comments