7
我有兩個不同的音頻樣本混合在一起的問題,只需添加兩個音頻樣本的字節。從MP3文件混合兩個音頻樣本
以下過程後,當我嘗試打開媒體播放器mixed.mp3文件,它說:
Windows Media Player中遇到的問題,同時播放該文件。
這裏是我使用的混合音頻文件的代碼:
byte[] bytes1,bytes2,final;
int length1,length2,max;
// Getting byte[] of audio file
using (BinaryReader b = new BinaryReader(File.Open("background.mp3" , FileMode.Open)))
{
length1 = (int)b.BaseStream.Length;
bytes1 = b.ReadBytes(length1);
}
using (BinaryReader b = new BinaryReader(File.Open("voice.mp3" , FileMode.Open)))
{
length2 = (int)b.BaseStream.Length;
bytes2 = b.ReadBytes(length2);
}
// Getting max length
if(length1 > length2){
max = length1;
}else{
max = length2;
}
// Initializing output byte[] of max length
final = new byte[max];
// Adding byte1 and byte2 and copying into final
for (int i=0;i<max;i++)
{
byte b1 , b2;
if(i < length1){
b1 = bytes1[i];
}else{
b1 = 0;
}
if (i < length2){
b2 = bytes2[i];
}
else{
b2 = 0;
}
final[i] = (byte)(b1 + b2);
}
// Writing final[] as an mp3 file
File.WriteAllBytes("mixed.mp3" , final);
注:我試圖將兩種相同的文件和它的工作,那就是,媒體播放器沒有扔任何錯誤並正確播放。
你的底部註釋是什麼意思?你的意思是現在的代碼有效? –
這是不可能的。 Mp3不是行波數據。像這樣添加字節將100%損壞的數據。不知道它是如何工作的 –
解碼它,做你的計算,記住作物(0..255),再次編碼。你現在所做的工作不能工作,因爲:mp3是壓縮的(除非它只包含幅度數據......我不是那裏的專家),你也必須考慮包含在文件級別工作時不應該改變的所有元數據的頭文件。 –