我花了几个小时试图从加载了waveurfer.js的立体声mp3文件中选择一个通道
答案 0 :(得分:0)
我终于想出了一个可能不是最好的解决方案,但是没有问题。
这个想法是简单地将我要静音的通道的缓冲区替换为零。为此,我首先使用wavesurfer.backend.buffer.getChannelData(0)
存储原始数据,然后创建具有相同长度的零数组。然后,我使用wavesurfer.backend.buffer.copyToChannel(left_zeros, 0)
替换频道的内容。
像魅力一样工作!
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);
}
<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>
答案 1 :(得分:0)
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>