想写一个将双声道的 wav 转成 单声道的 wav,生成的却是噪音,不知哪里错了。

幽饮烛 幽饮烛 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Test1
{
public static void main(String[] args) throws Exception
{
File file = new File("T:\\temp\\tada.wav").getAbsoluteFile();
System.out.println(file.getName());
BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
RIFFHeader riff = new RIFFHeader(input);
System.out.println(riff.getID());
System.out.println(riff.getSize());
System.out.println(riff.getFormat());
WAVEHeader wave = new WAVEHeader(input);
System.out.println(wave.getID());
System.out.println(wave.getSize());
System.out.println(wave.getFormatTag());
System.out.println(wave.getChannels());
System.out.println(wave.getSamplesPerSec());
System.out.println(wave.getAvgBytesPerSec());
System.out.println(wave.getBlockAlign());
System.out.println(wave.getBitsPerSample());
System.out.println(wave.getOther());
DataHeader data = new DataHeader(input);
System.out.println(data.getID());
System.out.println(data.getSize());
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(new File(file.getParentFile(), file.getName() + ".out")));
writeString(output, riff.getID());
writeInteger(output, 4, data.getSize() / wave.getChannels() + 8 + wave.getSize() + 8 + 4);
writeString(output, riff.getFormat());
writeString(output, wave.getID());
writeInteger(output, 4, wave.getSize());
writeInteger(output, 2, wave.getFormatTag());
writeInteger(output, 2, 1);
writeInteger(output, 4, wave.getSamplesPerSec());
writeInteger(output, 4, wave.getAvgBytesPerSec() / wave.getChannels());
writeInteger(output, 2, wave.getBlockAlign() / wave.getChannels());
writeInteger(output, 2, wave.getBitsPerSample());
if (wave.getSize() != 16)
{
writeString(output, wave.getOther());
}
writeString(output, data.getID());
writeInteger(output, 4, data.getSize() / wave.getChannels());
int byteCount = wave.getBitsPerSample() / 8;
int frameCount = data.getSize() / wave.getChannels() / byteCount;
int[] frames = new int[wave.getChannels()];
int frame;
for (int i = 0; i < frameCount; i++)
{
for (int f = 0; f < frames.length; f++)
{
frame = 0;
for (int d = 0; d < byteCount; d++)
{
frame += input.read() << (d * 8);
}
frames[f] = frame;
}
frame = 0;
for (int f : frames)
{
frame += f;
}
frame /= frames.length;
writeInteger(output, byteCount, frame);
}
input.close();
output.close();
}

private static void writeInteger(final OutputStream stream, final int size, final int value) throws IOException
{
for (int i = 0; i < size; i++)
{
stream.write((value >>> (i * 8)) & 0xFF);
}
}

public static void writeString(final OutputStream stream, final String string) throws IOException
{
char[] chars = string.toCharArray();
for (char c : chars)
{
stream.write(c);
}
}

public static class DataHeader
{
private final String id;
private final int size;

public String getID()
{
return id;
}

public int getSize()
{
return size;
}

public DataHeader(final InputStream stream) throws Exception
{
byte[] buffer = new byte[4];
stream.read(buffer);
char[] chars = new char[buffer.length];
for (int i = 0; i < chars.length; i++)
{
chars[i] = (char) buffer[i];
}
id = new String(chars);
size = (stream.read() & 255) + ((stream.read() & 255) << 8) + ((stream.read() & 255) << 16) + ((stream.read() & 255) << 24);
}
}

public static class WAVEHeader
{
private final String id;
private final int size;
private final int formatTag;
private final int channels;
private final int samplesPerSec;
private final int avgBytesPerSec;
private final int blockAlign;
private final int bitsPerSample;
private final String other;

public int getBlockAlign()
{
return blockAlign;
}

public int getBitsPerSample()
{
return bitsPerSample;
}

public String getOther()
{
return other;
}

public int getSize()
{
return size;
}

public int getFormatTag()
{
return formatTag;
}

public int getChannels()
{
return channels;
}

public int getSamplesPerSec()
{
return samplesPerSec;
}

public int getAvgBytesPerSec()
{
return avgBytesPerSec;
}

public String getID()
{
return id;
}

public WAVEHeader(final InputStream stream) throws Exception
{
byte[] buffer = new byte[4];
stream.read(buffer);
char[] chars = new char[buffer.length];
for (int i = 0; i < chars.length; i++)
{
chars[i] = (char) buffer[i];
}
id = new String(chars);
size = (stream.read() & 255) + ((stream.read() & 255) << 8) + ((stream.read() & 255) << 16) + ((stream.read() & 255) << 24);
formatTag = (stream.read() & 255) + ((stream.read() & 255) << 8);
channels = (stream.read() & 255) + ((stream.read() & 255) << 8);
samplesPerSec = (stream.read() & 255) + ((stream.read() & 255) << 8) + ((stream.read() & 255) << 16) + ((stream.read() & 255) << 24);
avgBytesPerSec = (stream.read() & 255) + ((stream.read() & 255) << 8) + ((stream.read() & 255) << 16) + ((stream.read() & 255) << 24);
blockAlign = (stream.read() & 255) + ((stream.read() & 255) << 8);
bitsPerSample = (stream.read() & 255) + ((stream.read() & 255) << 8);
if (size == 16)
{
other = "";
}
else
{
buffer = new byte[size - 16];
stream.read(buffer);
chars = new char[buffer.length];
for (int i = 0; i < chars.length; i++)
{
chars[i] = (char) buffer[i];
}
other = new String(chars);
}
}
}

public static class RIFFHeader
{
private final String id;
private final int size;
private final String format;

public String getID()
{
return id;
}

public int getSize()
{
return size;
}

public String getFormat()
{
return format;
}

public RIFFHeader(final InputStream stream) throws Exception
{
byte[] buffer = new byte[4];
stream.read(buffer);
char[] chars = new char[buffer.length];
for (int i = 0; i < chars.length; i++)
{
chars[i] = (char) buffer[i];
}
id = new String(chars);
size = (stream.read() & 255) + ((stream.read() & 255) << 8) + ((stream.read() & 255) << 16) + ((stream.read() & 255) << 24);
stream.read(buffer);
for (int i = 0; i < chars.length; i++)
{
chars[i] = (char) buffer[i];
}
format = new String(chars);
}
}
}
...全文

相关推荐

音频测试文件:wav格式,双声道(10hz-35khz)频率正弦波,可用于测试

单个时长5秒,双声道,有10Hz、20Hz、30Hz、50Hz、100Hz、200Hz、500Hz、1000Hz、2000Hz、10kHz、15kHz、20kHz、25kHz、30kHz、35kHz可供测试。注意太高频率损伤听力!

肥肥牛

后缀.wav语音测试文件

百度语音测试文件,转换出来文字,保证可以使用,已经亲自测试过了。

废墟小肥猫

【白噪音和粉红噪音】煲机必备无损噪音 下载

煲机的概述:   指新买回的音响器材得通电一段时间后才会让重放的音质变好。 煲机是一种快速使器材老化稳定的措施。有些元器件例如晶体管、集成电路、电容全新的时候电器参数不稳定,经过一段时间的使用后才能逐渐稳定。对于耳机来说,煲机实际就是在煲振膜折环,新耳机振膜折环机械顺性差,导致失真比较大,经过一段时间使用后,顺性逐渐变好,失真也会逐渐降到正常的水平。 煲机是人为的以非正常使用的方式加速器材进入成熟期的过程,一般是让器材连续工作一定的时间。而未“煲”过或未“煲”够火候儿的音箱和耳机,声音的特点可以用“紧、冷、硬、粗”四个字来形容,而“煲”后的产品则醇和、顺滑、细腻很多,仿佛发酵不全的新酒与陈年佳酿相比一般,它们的“味道”还是大不一样的。 这个“煲”字用得可是形象贴切的紧,意思是要着意伺弄,不宜“炒”、不宜“炸”,更不宜“烧”,而是要文火慢炖,慢功出细活。 新的音响器材呢,就像新车,都有个磨合期。 这里介绍一种比较安全、简单的煲机方法,你就拿着白噪音和粉红噪音轮流播放着煲去吧。 音量要适中,可逐渐加大。 下面这段是转的: 白噪声或粉红噪声之类是自然状态的噪声。之所以叫白噪声,粉红噪声,是由光波的谱线图就是光谱图类比而来,白噪声的特点是各频段的能量均匀(频谱类似太阳光谱即白光光谱),粉红噪声是在低频段强在高频段弱的噪声(频谱图类似偏红的光谱即粉红光谱)。 1: 白噪音white noise 所谓白噪音是指一段声音中的频率分量的功率在整个可听范围(0~20KHZ)内都是均匀的。由于人耳对高频敏感一点这种声音听上去是很吵耳的沙沙声。 2: 粉红噪音pink noise 粉红噪音是自然界最常见的噪音,简单说来,粉红噪音的频率分量功率主要分布在中低频段。粉红噪音从人耳中听到的是平直的频率响应——"非常悦耳的一种噪声"最常用于进行声学测试的声音。 3: 电视机无信号时的背景噪声和调频收音机无台时的背景噪声均是白噪声。白噪声可用来测量扬声器和耳机的谐振和灵敏度等。 4: 从波形角度看,粉红噪音是分形的,在一定的范围内音频数据具有相同或类似的能量。粉红噪声的电平从低频向高频不断衰减,其幅度与频率成反比(1/f)。其幅度每倍频程(一个8度)下降3dB。噪声能量在每倍频程内是相等的。 5: 从频谱仪的图形上看,白噪声在全频谱内是一条平直的线。 6: 从频谱仪的图形上看,粉红噪声是在一个小段频谱内平直的线,并且以其倍数频率向下衰减。即1倍频,2倍频……频率越高谱线高度越低。 白噪声是一种无规噪声,它的瞬时值是随机变化的。它的幅值对时间的分布满足正态分布。它具有连续的噪声谱,包含有各种频率成分的噪声。它的功率谱密度与频率无关,几个频率能量的分布是均匀的。它的等带宽输出的能量是相等的。它在线性坐标中,输出是一根平行与横坐标的直线。在对数坐标中,输出是按每倍频程带宽增加3dB的斜率而上升的。 粉红噪声与白噪声一样也是一种无规噪声,也具有连续的噪声谱。不同之处在于,它的功率谱密度与频率成反比,在对数坐标中,起输出为一水平线,在线性坐标中,其输出以每倍频程3dB下降。 在人耳可听的频率范围内,具有相同能量的噪声称为白噪声。白噪声广泛用于环境声学测量中。 ===================================分割线================================= 附上wav格式的白噪音 粉红噪音各一个(文件大小:18.7M)

shangqingnan

WhiteNoise_白噪音信号_

标准白噪音信号

周玉坤举重

测试用的0DB WAV音频文件

测试用的0DB WAV音频文件 1KHz-stero 10KHz-stero 20Hz-stero

txx200723
评论 10
收藏 已收藏
分享
返回顶部
幽饮烛
Java SE 62,621 成员 307,251 内容