WAV相关:从PCM16 Little Endian数据转WAV文件
数据格式
[0.0, -0.0, -0.0, 0.0, 0.0, 0.0, 5.960464477539063e-08, 5.960464477539063e-08, 1.1920928955078125e-07, 1.7881393432617188e-07, 2.384185791015625e-07,.....]
转换方法:c#
using System;
using System.IO;
using System.Text; namespace deserialfromPCMData
{
public static class BinaryWriterExtensions
{
private const int HeaderSize = ; private const int Hz = ; //frequency or sampling rate private const float RescaleFactor = ; //to convert float to Int16 public static void AppendWaveData<T>(this T stream, float[] buffer)
where T : Stream
{
if (stream.Length > HeaderSize)
{
stream.Seek(, SeekOrigin.End);
}
else
{
stream.SetLength(HeaderSize);
stream.Position = HeaderSize;
} // rescale
var floats = Array.ConvertAll(buffer, x => (short)(x * RescaleFactor)); // Copy to bytes
var result = new byte[floats.Length * sizeof(short)];
Buffer.BlockCopy(floats, , result, , result.Length); // write to stream
stream.Write(result, , result.Length); // Update Header
UpdateHeader(stream);
} public static void UpdateHeader(Stream stream)
{
var writer = new BinaryWriter(stream); writer.Seek(, SeekOrigin.Begin); writer.Write(Encoding.ASCII.GetBytes("RIFF")); //RIFF marker. Marks the file as a riff file. Characters are each 1 byte long.
writer.Write((int)(writer.BaseStream.Length - )); //file-size (equals file-size - 8). Size of the overall file - 8 bytes, in bytes (32-bit integer). Typically, you'd fill this in after creation.
writer.Write(Encoding.ASCII.GetBytes("WAVE")); //File Type Header. For our purposes, it always equals "WAVE".
writer.Write(Encoding.ASCII.GetBytes("fmt ")); //Mark the format section. Format chunk marker. Includes trailing null.
writer.Write(); //Length of format data. Always 16.
writer.Write((short)); //Type of format (1 is PCM, other number means compression) . 2 byte integer. Wave type PCM
writer.Write((short)); //Number of Channels - 2 byte integer
writer.Write(Hz); //Sample Rate - 32 byte integer. Sample Rate = Number of Samples per second, or Hertz.
writer.Write(Hz * * ); // sampleRate * bytesPerSample * number of channels, here 16000*2*1.
writer.Write((short)( * )); //channels * bytesPerSample, here 1 * 2 // Bytes Per Sample: 1=8 bit Mono, 2 = 8 bit Stereo or 16 bit Mono, 4 = 16 bit Stereo
writer.Write((short)); //Bits per sample (BitsPerSample * Channels) ?? should be 8???
writer.Write(Encoding.ASCII.GetBytes("data")); //"data" chunk header. Marks the beginning of the data section.
writer.Write((int)(writer.BaseStream.Length - HeaderSize)); //Size of the data section. data-size (equals file-size - 44). or NumSamples * NumChannels * bytesPerSample ??
}
} //end of class
}
using System;
using System.IO;
using System.Text; namespace deserialfromPCMData
{
class SaveAudioStreamToWav
{
static void Main(string[] args)
{ Stream instream = File.OpenRead(@"g:\sample.txt"); BufferedStream bfs = new BufferedStream(instream);
byte[] array = new byte[bfs.Length];
instream.Read(array, , array.Length); string str = Encoding.Default.GetString(array); var StreamSample = str.Substring(, str.Length - ).Split(','); var floatsArray = new float[StreamSample.Length];
floatsArray = Array.ConvertAll(StreamSample, x => (float)Convert.ToDouble(x)); using (var stream = new FileStream(@"g:\sample2.wav", FileMode.Create, FileAccess.ReadWrite))
{
stream.AppendWaveData(floatsArray);
}
}
}
}
Java方法:
package com; import com.sun.media.sound.WaveFileWriter;
import org.junit.Test; import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import java.io.*;
import java.util.Arrays; //https://stackoverflow.com/questions/3599378/java-read-wav-file-as-a-float-array
//
//https://stackoverflow.com/questions/26824663/how-do-i-use-audio-sample-data-from-java-sound
//
//https://stackoverflow.com/questions/4440015/java-pcm-to-wav
public class TestWavFile { //采样率16kHz
private float SAMPLING_RATE = 16000;
private float sampleSizeBits = 16; //把short(2字节)拆解成字节流byte[2]
public byte[] get16BitPcm(short[] data) { byte[] resultData = new byte[2 * data.length];
int iter = 0;
for (short sample : data) {
resultData[iter++] = (byte)(sample & 0x00ff);
resultData[iter++] = (byte)((sample & 0xff00) >>> 8);
}
return resultData;
} @Test
public void test() throws IOException{ BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(
new FileInputStream("g:/sample.txt"))); StringBuffer buffer = new StringBuffer();
String line = null; while ( (line = bufferedReader.readLine()) != null)
{
buffer.append(line);
} int len = buffer.length(); String raw = buffer.substring(1,len-1); Short[] data = Arrays.stream(raw.split(","))
.map(track->( Float.valueOf(track) * 0x7fff ))
.map(item->item.shortValue()).toArray(Short[]::new); short [] frameData = new short[data.length]; for (int i = 0; i< data.length; i++) {
frameData[i] = data[i];
} WaveFileWriter writer = new WaveFileWriter(); FileOutputStream outStream = new FileOutputStream("g:/sample.wav");
//(采样率,比特位,通道,是否有符号,大小端)
//比特位:short 2个字节 2*8 = 16
//是否有符号:是否有负数
//
AudioFormat format = new AudioFormat(SAMPLING_RATE,16,1,true,false); AudioInputStream audioInputStream = new AudioInputStream(new ByteArrayInputStream(get16BitPcm(frameData)), format, frameData.length); writer.write(audioInputStream, AudioFileFormat.Type.WAVE, outStream); }
}
java封装工具:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.media.sound.WaveFileWriter;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils; import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays; @Slf4j
public final class WavFileGenerator { //采样率16kHz
private float SAMPLING_RATE = 16000;
private float sampleSizeBits = 16; private int HeaderSize = 44; //frequency or sampling rate
private int Hz = 16_000; private byte[] values; public WavFileGenerator(String ctx)
{
this(convert(ctx));
}
public WavFileGenerator(String []values)
{
convert(toPrimitive(Arrays.asList(values).stream().map(Float::valueOf).toArray(size -> new Float[size])));
} public WavFileGenerator(short[] values)
{
/*sizeof(short) was 2 bytes in java platform*/
byte[] result = new byte[values.length * 2]; // 1short = 2bytes
int iter = 0;
for (short sample : values) {
result[iter++] = (byte)(sample & 0x00ff);
result[iter++] = (byte)((sample & 0xff00) >>> 8);
} this.values = result;
}
public WavFileGenerator(float[] floatValues)
{
this(convert(floatValues));
} static float[] toPrimitive(Float[] floats)
{
return ArrayUtils.toPrimitive(floats);
} public static final short[] convert(float[] values)
{
//PCM 16bit little endian
short[] shortValues = new short[values.length];
for (int i = 0; i < values.length; i++) {
float value = values[i];
shortValues[i] = (short) (value < 0 ? value* 0x8000 : value*0x7fff);
}
return shortValues;
} public static final float[] convert(String ctx)
{
ObjectMapper objectMapper = new ObjectMapper();
try {
Float[] data = objectMapper.readValue(ctx, Float[].class);
return toPrimitive(data);
} catch (IOException e) {
log.error("转换出错:{}",e);
}
return new float[0];
} public byte[] getFmtChunk()
{
return null;
} public byte[] getDataChunk()
{
return values;
} void builder()
{ } public ByteBuffer getAudioStream()
{
return null;
} public void saveFile(String dest) throws IOException { WaveFileWriter writer = new WaveFileWriter();
FileOutputStream outStream = new FileOutputStream(dest);
AudioFormat format = new AudioFormat(SAMPLING_RATE,16,1,true,false);
int frames = values.length / 2;
AudioInputStream audioInputStream = new AudioInputStream(new ByteArrayInputStream(values), format, frames);
writer.write(audioInputStream, AudioFileFormat.Type.WAVE, outStream);
audioInputStream.close();
outStream.close();
}
}
运行以上两个文件,最终会成功wav文件。
WAV相关:从PCM16 Little Endian数据转WAV文件的更多相关文章
- 【应用】:shell crontab定时生成oracle表的数据到txt文件,并上传到ftp
一.本人环境描述 1.oracle服务端装在win7 32位上,oracle版本为10.2.0.1.0 2.Linux为centos6.5 32位,安装在Oracle VM Vir ...
- HTML5操作麦克风获取音频数据(WAV)的一些基础技能
基于HTML5的新特性,操作其实思路很简单. 首先通过navigator获取设备,然后通过设备监听语音数据,进行原始数据采集. 相关的案例比较多,最典型的就是链接:https://developer. ...
- pcm数据生成wav文件
Qt由pcm数据生成wav文件 void AudioGrabber::saveWave(const QString &fileName, const QByteArray &raw, ...
- C#中使用WavHelper保存录音数据为wav文件
C#将录音数据文件保存为wav格式文件,这里使用到的是WavHelper工具类. WavHelper工具类: using System; using System.Collections.Generi ...
- php添加数据到xml文件的例子
php添加数据到xml文件中 时间:2015-12-17 06:30:37来源:网络 导读:php添加数据到xml文件中 xml文件:stu.xml: 复制代码代码如下: <?xml ver ...
- MYSQL数据库导入大数据量sql文件失败的解决方案
1.在讨论这个问题之前首先介绍一下什么是"大数据量sql文件". 导出sql文件.选择数据库-----右击选择"转储SQL文件"-----选择"结构和 ...
- [大数据]-Fscrawler导入文件(txt,html,pdf,worf...)到Elasticsearch5.3.1并配置同义词过滤
fscrawler是ES的一个文件导入插件,只需要简单的配置就可以实现将本地文件系统的文件导入到ES中进行检索,同时支持丰富的文件格式(txt.pdf,html,word...)等等.下面详细介绍下f ...
- Python:将爬取的网页数据写入Excel文件中
Python:将爬取的网页数据写入Excel文件中 通过网络爬虫爬取信息后,我们一般是将内容存入txt文件或者数据库中,也可以写入Excel文件中,这里介绍关于使用Excel文件保存爬取到的网页数据的 ...
- 30G 上亿数据的超大文件,如何快速导入生产环境?
Hello,大家好,我是楼下小黑哥~ 如果给你一个包含一亿行数据的超大文件,让你在一周之内将数据转化导入生产数据库,你会如何操作? 上面的问题其实是小黑哥前段时间接到一个真实的业务需求,将一个老系统历 ...
随机推荐
- c语言逆序
#include <stdio.h> #define MAXS 20 void reverse( char *p ); void reverse( char *p ) { int i = ...
- LightOJ - 1311 - Unlucky Bird(相遇问题)
链接: https://vjudge.net/problem/LightOJ-1311 题意: A bird was flying on a train line, singing and passi ...
- HDFS写机制
HDFS写机制: 1.client客户端调用分布式文件系统对象DistributedFileSystem对象的create方法,创建一个文件输出流FSDataOutputStream对象. 2.Dis ...
- git 在 A 项目中引用 B 项目
git 在 A 项目中引用 B 项目 场景: 需要在项目calcDLL(http://XXX/XXXA.git) 中 引用 项目libindex(http://XXX/XXXB.git). 解决方 ...
- (尚003).Vue_模板语法
1.双大括号表达式 2.指令一:强制数据绑定 3.指令二;绑定事件监听 test003.html<!DOCTYPE html><html lang="en"> ...
- win10 安装Borland C++Builder 6后编译运行出
win10系统安装bcb后打开bcb后显示 Unable to rename ‘c:\Program Files(x86)\Borland\CBuilder6\Bin\bcb.$$$'to‘cc:\P ...
- 2-使用git管理一个单片机程序
https://www.cnblogs.com/yangfengwu/p/10842205.html 我用电脑压缩一个文件,然后通过git上传,然后在新买的linux系统通过wget 网络下载这个压缩 ...
- git 的使用方法以及要注意的地方~
1.假如你在一个分支,非master分支,例如avatar,在你修改之前一定要 get merge master,git pull,再开始写代码.如果改好了,也要先git merge master,g ...
- Parse发布Bolts,一个面向iOS和Android的底层库集合
转载自:http://www.infoq.com/cn/news/2014/02/parse-announces-bolts 数月前,Parse被Facebook收购.最近,它开源了一个面向iOS和A ...
- mysql 使用的三个小技巧
mysql 使用的三个小技巧 快速阅读 Mysql查询工具中如何查询多条语名,Mysql中如何设置变量,Mysql中如何查特定字段,后面再加* Mysql查询工具中如何查询多条语名 默认myslq只能 ...