Мне нужно иметь возможность обрезать аудио на основе 3 входов: < /p>
- Audio Base64 Audio < /li>
, где я сталкиваюсь с проблемой, когда я называю функцию audiobuffertobase64 (...) .
я не могу преобразовать тренд Audiobuffer base audio oudio oudio. /> Пример кода < /h1>
Вот код, который у меня есть до сих пор.
Это не будет окончательной формой кода, это всего лишь poc. < /p>
Audio Trimmer
body,
.inputsContainer,
.inputField
{
display: flex;
flex-direction: column;
gap: 1rem;
}
.inputField
{
gap: 0.25rem;
}
.inputField > div
{
font-weight: bold;
}
textarea
{
--size-rowCount: 6;
--size-padding-vertical: 0.75em;
min-width: 100%;
max-width: 100%;
min-height: calc((1em * var(--size-rowCount)) + var(--size-padding-vertical));
}
class="inputsContainer"
>
Audio Base64:
class="inputField"
>
Start Time (ms):
class="inputField"
>
End Time (ms):
Trim Audio
/**
* This function builds an instance of an AudioContext.
*
* @returns
* The AudioContext.
*
* @throws
* - If no AudioContext is available.
*/
const buildAudioContext = () => {
if (window.AudioContext)
{
return new window.AudioContext();
}
if (window.webkitAudioContext)
{
return new window.webkitAudioContext();
}
throw new Error('AudioContext is not available.');
};
/**
* This function will parse the specified input time.
*
* @param time
* The time value to be parsed.
* @param minAllowedTime
* The minimum allowed time value.
* @param timeType
* The type of time being parsed.
*
* This is just used for logging/error-throwing.
*
* @returns
* The parsed time.
*
* @throws
* - If specified time is not a number, and cannot be parsed as an integer.
* - If specified time is less-than the specified minimum allowed value.
*/
const parseInputTime = (
time,
minAllowedTime,
timeType
) => {
let _time = time;
if (typeof _time !== 'number')
{
_time = parseInt(_time);
}
if (
(typeof _time !== 'number')
||
isNaN(_time)
||
(_time < minAllowedTime)
)
{
throw new Error('Invalid ' + timeType + ' time: "' + time + '"');
}
return _time;
};
/**
* This function coverts the specified Base64 string to an
* AudioBuffer.
*
* @param base64
* The Base64 string.
*
* @returns
* The AudioBuffer.
*
* @throws
* - If decoding of the audio data fails.
*/
const base64ToAudioBuffer = async (
base64
) => {
try
{
let binary = window.atob(base64);
let buffer = new ArrayBuffer(binary.length);
let bytes = new Uint8Array(buffer);
for (let i = 0; i < buffer.byteLength; i++)
{
bytes = binary.charCodeAt(i) & 0xFF;
}
return await buildAudioContext().decodeAudioData(
buffer
);
}
catch (error)
{
throw new Error(
'Failed to decode audio data.\n\t - ' + error.message,
error
);
}
};
/**
* This function converts the specified buffer to a Base64 string.
*
* @param buffer
* The buffer.
*
* @returns
* The Base64 string.
*/
const audioBufferToBase64 = (
buffer
) => {
console.log(' buffer: ', buffer);
console.log('Channel Count: ', buffer.numberOfChannels);
console.log(' Sample Rate: ', buffer.sampleRate);
console.log(' Duration: ', buffer.duration);
console.log(' Length: ', buffer.length);
for (let channelIndex=0; channelIndex {
return new Promise((
response,
reject
) => {
try
{
const audioContext = buildAudioContext();
const sampleRate = audioBuffer.sampleRate || 48000;
const startFrame = (startTimeMs * sampleRate) / 1000;
const endFrame = (endTimeMs * sampleRate) / 1000;
let channelCount = audioBuffer.numberOfChannels || 0;
if (channelCount {
document.getElementById('trimButton').addEventListener('click', trimAudioFromInputs);
};
Подробнее здесь: https://stackoverflow.com/questions/795 ... diocontext
Мобильная версия