c# – 混合MP3文件中的两个音频样本

前端之家收集整理的这篇文章主要介绍了c# – 混合MP3文件中的两个音频样本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有问题通过简单地添加两个音频样本的字节将两个不同的音频样本混合成一个.

在下面的过程中,当我尝试在媒体播放器中打开mixed.mp3文件时,它说:

Windows Media Player encountered a problem while playing the file.

这是我用来混合音频文件代码

  1. byte[] bytes1,bytes2,final;
  2. int length1,length2,max;
  3.  
  4. // Getting byte[] of audio file
  5. using ( BinaryReader b = new BinaryReader(File.Open("background.mp3",FileMode.Open)) )
  6. {
  7. length1 = (int)b.BaseStream.Length;
  8. bytes1 = b.ReadBytes(length1);
  9. }
  10.  
  11. using ( BinaryReader b = new BinaryReader(File.Open("voice.mp3",FileMode.Open)) )
  12. {
  13. length2 = (int)b.BaseStream.Length;
  14. bytes2 = b.ReadBytes(length2);
  15. }
  16.  
  17. // Getting max length
  18. if(length1 > length2){
  19. max = length1;
  20. }else{
  21. max = length2;
  22. }
  23.  
  24. // Initializing output byte[] of max length
  25. final = new byte[max];
  26.  
  27. // Adding byte1 and byte2 and copying into final
  28. for (int i=0;i<max;i++)
  29. {
  30. byte b1,b2;
  31.  
  32. if(i < length1){
  33. b1 = bytes1[i];
  34. }else{
  35. b1 = 0;
  36. }
  37.  
  38. if ( i < length2 ){
  39. b2 = bytes2[i];
  40. }
  41. else{
  42. b2 = 0;
  43. }
  44.  
  45. final[i] = (byte)(b1 + b2);
  46. }
  47.  
  48. // Writing final[] as an mp3 file
  49. File.WriteAllBytes("mixed.mp3",final);

注意:我试图混合两个相同的文件,它工作,也就是说,媒体播放器没有抛出任何错误并正确播放.

解决方法

这很可能是因为在混合之前你不是MP3文件decoding.而你只是“将”样本加在一起,这将导致 clipping;您应该首先使用 library将MP3文件解码为PCM,这样您就可以将它们混合使用.

要正确混合样品,您应该这样做:

  1. final[i] = (byte)(b1 / 2 + b2 / 2);

否则你的计算会溢出(另外,我通常建议在操作它们之前将音频标准化为浮点数).还应该注意的是,你正在混合MP3文件中的所有字节,即你正在弄乱标题(因此WMP拒绝播放你的“混合”文件).您应该只混合文件的实际音频数据(样本),而不是整个文件.

我已经使用NAudio库提供了一个(注释)工作示例1(它将混合音频导出到wav文件以避免进一步的复杂化):

  1. // You can get the library via NuGet if preferred.
  2. using NAudio.Wave;
  3.  
  4. ...
  5.  
  6. var fileA = new AudioFileReader("Input path 1");
  7.  
  8. // Calculate our buffer size,since we're normalizing our samples to floats
  9. // we'll need to account for that by dividing the file's audio byte count
  10. // by its bit depth / 8.
  11. var bufferA = new float[fileA.Length / (fileA.WaveFormat.BitsPerSample / 8)];
  12.  
  13. // Now let's populate our buffer with samples.
  14. fileA.Read(bufferA,bufferA.Length);
  15.  
  16. // Do it all over again for the other file.
  17. var fileB = new AudioFileReader("Input path 2");
  18. var bufferB = new float[fileB.Length / (fileB.WaveFormat.BitsPerSample / 8)];
  19. fileB.Read(bufferB,bufferB.Length);
  20.  
  21. // Calculate the largest file (simpler than using an 'if').
  22. var maxLen = (long)Math.Max(bufferA.Length,bufferB.Length);
  23. var final = new byte[maxLen];
  24.  
  25. // For now,we'll just save our mixed data to a wav file.
  26. // (Things can get a little complicated when encoding to MP3.)
  27. using (var writer = new WaveFileWriter("Output path",fileA.WaveFormat))
  28. {
  29. for (var i = 0; i < maxLen; i++)
  30. {
  31. float a,b;
  32.  
  33. if (i < bufferA.Length)
  34. {
  35. // Reduce the amplitude of the sample by 2
  36. // to avoid clipping.
  37. a = bufferA[i] / 2;
  38. }
  39. else
  40. {
  41. a = 0;
  42. }
  43.  
  44. if (i < bufferB.Length)
  45. {
  46. b = bufferB[i] / 2;
  47. }
  48. else
  49. {
  50. b = 0;
  51. }
  52.  
  53. writer.WriteSample(a + b);
  54. }
  55. }

1输入文件必须具有相同的采样率,位深度和通道数,才能使其正常工作.

猜你在找的C#相关文章