fix this script ''' import { useCallback, useRef, useState } from 'react'; import {…
- User
fix this script ''' import { useCallback, useRef, useState } from 'react'; import { AudioConfig } from '../types';
const DEFAULT_AUDIO_CONFIG: AudioConfig = { echoCancellation: true, noiseSuppression: true, sampleRate: 44100, };
function writeString(s, a, offset) { for (let i = 0; i < s.length; ++i) { a[offset + i] = s.charCodeAt(i); } }
function writeInt16(n, a, offset) { n = Math.floor(n);
const b1 = n & 255; const b2 = (n >> 8) & 255;
a[offset + 0] = b1; a[offset + 1] = b2; }
function writeInt32(n, a, offset) { n = Math.floor(n); const b1 = n & 255; const b2 = (n >> 8) & 255; const b3 = (n >> 16) & 255; const b4 = (n >> 24) & 255;
a[offset + 0] = b1; a[offset + 1] = b2; a[offset + 2] = b3; a[offset + 3] = b4; }
// Return the bits of the float as a 32-bit integer value. This // produces the raw bits; no intepretation of the value is done. function floatBits(f) { const buf = new ArrayBuffer(4); new Float32Array(buf)[0] = f; const bits = new Uint32Array(buf)[0]; // Return as a signed integer. return bits | 0; }
function writeAudioBuffer(audioBuffer, a, offset, asFloat) { // let n = audioBuffer.length; const n = audioBuffer.reduce((a, b) => a + b.length, 0); // let channels = audioBuffer.numberOfChannels; const channels = audioBuffer.length;
for (let i = 0; i < n; ++i) { for (let k = 0; k < channels; ++k) { // let buffer = audioBuffer.getChannelData(k); const buffer = audioBuffer[k]; if (asFloat) { const sample = floatBits(buffer[i]); writeInt32(sample, a, offset); offset += 4; } else { let sample = buffer[i] * 32768.0;
code// Clip samples to the limitations of 16-bit. // If we don't do this then we'll get nasty wrap-around distortion. if (sample < -32768) sample = -32768; if (sample > 32767) sample = 32767; writeInt16(sample, a, offset); offset += 2; } }} }
// See http://soundfile.sapp.org/doc/WaveFormat/ and // http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html // for a quick introduction to the WAVE PCM format. function createWaveFileData(audioBuffer:any, asFloat:any, sampleRate:any) { const bytesPerSample = asFloat ? 4 : 2; const frameLength = audioBuffer[0].length; //audioBuffer.length; const numberOfChannels = audioBuffer.length; // audioBuffer.numberOfChannels; // let sampleRate = audioBuffer.sampleRate; const bitsPerSample = 8 * bytesPerSample; const byteRate = (sampleRate * numberOfChannels * bitsPerSample) / 8; const blockAlign = (numberOfChannels * bitsPerSample) / 8; const wavDataByteLength = frameLength * numberOfChannels * bytesPerSample; const headerByteLength = 44; const totalLength = headerByteLength + wavDataByteLength; const waveFileData = new Uint8Array(totalLength); const subChunk1Size = 16; // for linear PCM const subChunk2Size = wavDataByteLength; const chunkSize = 4 + (8 + subChunk1Size) + (8 + subChunk2Size); writeString('RIFF', waveFileData, 0); writeInt32(chunkSize, waveFileData, 4); writeString('WAVE', waveFileData, 8); writeString('fmt ', waveFileData, 12); writeInt32(subChunk1Size, waveFileData, 16); // SubChunk1Size (4) // The format tag value is 1 for integer PCM data and 3 for IEEE // float data. writeInt16(asFloat ? 3 : 1, waveFileData, 20); // AudioFormat (2) writeInt16(numberOfChannels, waveFileData, 22); // NumChannels (2) writeInt32(sampleRate, waveFileData, 24); // SampleRate (4) writeInt32(byteRate, waveFileData, 28); // ByteRate (4) writeInt16(blockAlign, waveFileData, 32); // BlockAlign (2) writeInt32(bitsPerSample, waveFileData, 34); // BitsPerSample (4) writeString('data', waveFileData, 36); writeInt32(subChunk2Size, waveFileData, 40); // SubChunk2Size (4) // Write actual audio data starting at offset 44. writeAudioBuffer(audioBuffer, waveFileData, 44, asFloat);
- Assistant