首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将时间与振幅的WAV文件更改为txt文件?

如何将时间与振幅的WAV文件更改为txt文件?
EN

Stack Overflow用户
提问于 2016-12-11 08:18:09
回答 1查看 234关注 0票数 0

我想使用LTSpice的文件输入功能来模拟使用真实世界中的音频比特的电路。我需要时间vs振幅版本的数据,但不确定哪个软件包可以为我做这件事。Audacity可以将MP3转换为WAV,但从我所看到的情况来看,它不能转换为无头文本文件。

因此,将.WAV文件转换为时间/振幅两列文本文件。

有什么免费的方法吗?

EN

回答 1

Stack Overflow用户

发布于 2016-12-11 10:36:01

这里有一个使用javascript的快速的讨厌的实现。

我将把结果字符串转换成可以轻松下载的Blob作为练习,我将保留频道选择、立体声结果和选择一小部分音轨进行处理的能力。

代码语言:javascript
复制
<!doctype html>
<html>
<head>
<script>
"use strict";
function byId(id){return document.getElementById(id)}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
window.addEventListener('load', onDocLoaded, false);

var audioCtx;

function onDocLoaded(evt)
{
    audioCtx = new AudioContext();
    byId('fileInput').addEventListener('change', onFileInputChangedGeneric, false);
}

function onFileInputChangedGeneric(evt)
{
    // load file if chosen
    if (this.files.length != 0)
    {
        var fileObj = this.files[0];
        loadAndTabulateAudioFile( fileObj, byId('output') );
    }
    // clear output otherwise
    else
        byId('output').textContent = '';
}

// processes channel 0 only
//
//  creates a string that represents a 2 column table, where each row contains the time and amplitude of a sample
//  columns are tab seperated
function loadAndTabulateAudioFile( fileObj, tgtElement )
{
    var a = new FileReader();
    a.onload = loadedCallback;
    a.readAsArrayBuffer( fileObj );
    function loadedCallback(evt)
    {
        audioCtx.decodeAudioData(evt.target.result, onDataDecoded);
    }
    function onDataDecoded(buffer)
    {
        //console.log(buffer);
        var leftChannel = buffer.getChannelData(0);

        //var rightChannel = buffer.getChannelData(1);
        console.log("# samples: " + buffer.length);
        console.log(buffer);

        var result = '';
        var i, n = buffer.length, invSampleRate = 1.0 / buffer.sampleRate;
        for (i=0; i<n; i++)
        {
            var curResult = (invSampleRate*i).toFixed(8) + "\t" + leftChannel[i] + "\n";
            result += curResult;
        }
        tgtElement.textContent = result;
    }
}

</script>
<style>
</style>
</head>
<body>
    <label>Select audio file: <input type='file' id='fileInput'/></label>
    <hr>
    <pre id='output'></pre>
</body>
</html>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41081738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档