首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在wavesurfer.js中静音一个通道

无法在wavesurfer.js中静音一个通道
EN

Stack Overflow用户
提问于 2018-10-25 16:49:35
回答 2查看 445关注 0票数 0

我花了几个小时试图从用wavesurfer.js加载的立体声mp3文件中选择一个频道

EN

回答 2

Stack Overflow用户

发布于 2018-10-25 16:49:35

我最终想出了一个可能不是最好的解决方案,但它没有任何问题。

这个想法是简单地将我想要静音的通道中的缓冲区替换为零。为此,我首先使用wavesurfer.backend.buffer.getChannelData(0)存储原始数据,并创建一个相同长度的零数组。然后,我使用wavesurfer.backend.buffer.copyToChannel(left_zeros, 0)替换频道的内容。

就像一种护身符!

代码语言:javascript
复制
    var wavesurfer = WaveSurfer.create({
        container: '#waveform',
        waveColor: 'darkorange',
        progressColor: 'purple',
        splitChannels: true,
        height: 150,
    });

    left_data = null;
    left_zeros = null;
    right_data = null;
    right_zeros = null;

    wavesurfer.load('./stereo-file.mp3');

    wavesurfer.on('ready', function () {



        left_data = wavesurfer.backend.buffer.getChannelData(0).map(d => {
            return d;
        });
        left_zeros = left_data.map(d => {
            return 0.;
        });

        right_data = wavesurfer.backend.buffer.getChannelData(1).map(d => {
            return d;
        });
        right_zeros = right_data.map(d => {
            return 0.;
        });

    });

    function playLeft() {
        wavesurfer.backend.buffer.copyToChannel(left_zeros, 0);
        wavesurfer.backend.buffer.copyToChannel(right_data, 1);
    }

    function playRight() {
        wavesurfer.backend.buffer.copyToChannel(left_data, 0);
        wavesurfer.backend.buffer.copyToChannel(right_zeros, 1);
    }
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/wavesurfer.min.js"></script>

<div id="waveform"></div>

<p align="center">
    <button class="btn btn-primary" onclick="playLeft()">
        <i class="glyphicon glyphicon-play"></i>
        Left
    </button>
    <button class="btn btn-primary" onclick="playRight()">
        <i class="glyphicon glyphicon-play"></i>
        Right
    </button>
    <button class="btn btn-primary" onclick="wavesurfer.playPause()">
        <i class="glyphicon glyphicon-play"></i>
        Play
    </button>
</p>

票数 0
EN

Stack Overflow用户

发布于 2019-10-23 22:31:44

代码语言:javascript
复制
    export class PlayerComponent implements OnInit {
      wavesurfer: WaveSurfer;
      leftGain: any;
      rightGain: any;
      stateLeft: number = 1;
      stateRight: number = 1;

      constructor() { }

      ngOnInit() {
        let audiofile = '../assets/1.mp3';
        this.wavesurfer = WaveSurfer.create({
          container: '#waveform',
          waveColor : 'blue',
          progressColor: 'purple',
          splitChannels: true,
          responsive: true,
          normalize: true
        });

        const splitter = this.wavesurfer.backend.ac.createChannelSplitter(2);
        const merger = this.wavesurfer.backend.ac.createChannelMerger(2);
        this.leftGain = this.wavesurfer.backend.ac.createGain();
        this.rightGain = this.wavesurfer.backend.ac.createGain();
        splitter.connect(this.leftGain, 0);
        splitter.connect(this.rightGain, 1);
        this.leftGain.connect(merger, 0, 0);
        this.rightGain.connect(merger, 0, 1);
        this.wavesurfer.backend.setFilters([splitter, this.leftGain, merger]);
        this.wavesurfer.load(audiofile);
        this.wavesurfer.on('play', () => {this.wavesurfer.playPause()});
        this.wavesurfer.on('error', (msg) => {
          console.log(msg);
      });
    }

      play() {
          this.wavesurfer.playPause();
      }
      right() {
            if(this.stateRight == 1)
            this.stateRight = 0;
            else
            this.stateRight = 1;
            this.rightGain.gain.value = this.stateRight;
      }
      left()
      {
            if(this.stateLeft == 1)
            this.stateLeft = 0;
            else
            this.stateLeft = 1;
            this.leftGain.gain.value = this.stateLeft;
          }
    }



<div id="waveform"></div>
<button class="btn btn-success" (click)="play()">Play/Pause</button>
<button class="btn btn-success" (click)="left()">left</button>
<button class="btn btn-success" (click)="right()">right</button>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52985129

复制
相关文章

相似问题

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