데릭쉐퍼드와 모두 실현할 수 배열로 샘플링합니다 wav 파일 한 개 또는 2 개의 계속 그렇게 할 경우 스테레오) 내가 할 수 있도록 일부 수정해야 적용하십시오 저들이요 나도 궁금 이 작업을 쉽게 수행할 경우 (없이 대부분 외부 라이브러리). 저는 전혀 경험이 있는 판독값 사운드 파일, 그래서 내가 모르는 # 39, don& 대해 별로 주제라서요.
이 코드는 이 어떻게해야합니다 의심하게 된다. 이 웨이브 파일 변환 해야 할 수 있지만, 표준화된 연산뿐 어레이입니다 (-1) 을 1 로) 할 것이 아니라 사소한 충족되었으며 int / 파선-짧은 어레이입니다 (분리하십시오 /32768.0 '대신' 비트 및 추가 32768). 바로 이 ' []' 가 될 수 있다면 wav 파일 어레이입니다 설정되었습니다 nulll 로드되었는지 모노.
// convert two bytes to one double in the range -1 to 1
static double bytesToDouble(byte firstByte, byte secondByte) {
// convert two bytes to one short (little endian)
short s = (secondByte << 8) | firstByte;
// convert to range from -1 to (just below) 1
return s / 32768.0;
}
// Returns left and right double arrays. 'right' will be null if sound is mono.
public void openWav(string filename, out double[] left, out double[] right)
{
byte[] wav = File.ReadAllBytes(filename);
// Determine if mono or stereo
int channels = wav[22]; // Forget byte 23 as 99.999% of WAVs are 1 or 2 channels
// Get past all the other sub chunks to get to the data subchunk:
int pos = 12; // First Subchunk ID from 12 to 16
// Keep iterating until we find the data chunk (i.e. 64 61 74 61 ...... (i.e. 100 97 116 97 in decimal))
while(!(wav[pos]==100 && wav[pos+1]==97 && wav[pos+2]==116 && wav[pos+3]==97)) {
pos += 4;
int chunkSize = wav[pos] + wav[pos + 1] * 256 + wav[pos + 2] * 65536 + wav[pos + 3] * 16777216;
pos += 4 + chunkSize;
}
pos += 8;
// Pos is now positioned to start of actual sound data.
int samples = (wav.Length - pos)/2; // 2 bytes per sample (16 bit sound mono)
if (channels == 2) samples /= 2; // 4 bytes per sample (16 bit stereo)
// Allocate memory (right will be null if only mono sound)
left = new double[samples];
if (channels == 2) right = new double[samples];
else right = null;
// Write to double array/s:
int i=0;
while (pos < length) {
left[i] = bytesToDouble(wav[pos], wav[pos + 1]);
pos += 2;
if (channels == 2) {
right[i] = bytesToDouble(wav[pos], wav[pos + 1]);
pos += 2;
}
i++;
}
}
16 비트 PCM 너회의 WAV 파일 map_layer 가정할 때, 즉 가장 일반적인) 나우디오 를 사용할 수 있습니다 다음 검토완료 아웃해야 바이트 배열을 붙여넣습니다 카피됐나 배열로 편의를 위해 16 비트 정수. 이 경우, 왼쪽, 오른쪽 인터리브된 샘플링합니다 스테레오 됩니다.
using (WaveFileReader reader = new WaveFileReader("myfile.wav"))
{
Assert.AreEqual(16, reader.WaveFormat.BitsPerSample, "Only works with 16 bit audio");
byte[] buffer = new byte[reader.Length];
int read = reader.Read(buffer, 0, buffer.Length);
short[] sampleBuffer = new short[read / 2];
Buffer.BlockCopy(buffer, 0, sampleBuffer, 0, read);
}
그러니까말이야 하고 싶은 것을 확인할 수 있지만, 제 3 자 라이브러리보다는 스케쳐내 대처하기 위해 WAV 파일을 사용하면 추가 청크에서 난 그냥 피하는 등 44 바이트입니다 파일에 제안하세요 외곽진입 있다.
WAV 파일 (적어도, 비압축 수준들과) 가 상당히 쉽습니다. S # 39, there& 헤더입니다 다음 데이터 inet6.0 거잖나.
32 비트 또는 64 비트 인코딩되지 wav ruettgers 먹어서나 아니예 시에 있다.
다음 코드는 비트 처리 및 모노 스테레오: 16/32/64 /
static bool readWav( string filename, out float[] L, out float[] R )
{
L = R = null;
//float [] left = new float[1];
//float [] right;
try {
using (FileStream fs = File.Open(filename,FileMode.Open))
{
BinaryReader reader = new BinaryReader(fs);
// chunk 0
int chunkID = reader.ReadInt32();
int fileSize = reader.ReadInt32();
int riffType = reader.ReadInt32();
// chunk 1
int fmtID = reader.ReadInt32();
int fmtSize = reader.ReadInt32(); // bytes for this chunk
int fmtCode = reader.ReadInt16();
int channels = reader.ReadInt16();
int sampleRate = reader.ReadInt32();
int byteRate = reader.ReadInt32();
int fmtBlockAlign = reader.ReadInt16();
int bitDepth = reader.ReadInt16();
if (fmtSize == 18)
{
// Read any extra values
int fmtExtraSize = reader.ReadInt16();
reader.ReadBytes(fmtExtraSize);
}
// chunk 2
int dataID = reader.ReadInt32();
int bytes = reader.ReadInt32();
// DATA!
byte[] byteArray = reader.ReadBytes(bytes);
int bytesForSamp = bitDepth/8;
int samps = bytes / bytesForSamp;
float[] asFloat = null;
switch( bitDepth ) {
case 64:
double[]
asDouble = new double[samps];
Buffer.BlockCopy(byteArray, 0, asDouble, 0, bytes);
asFloat = Array.ConvertAll( asDouble, e => (float)e );
break;
case 32:
asFloat = new float[samps];
Buffer.BlockCopy(byteArray, 0, asFloat, 0, bytes);
break;
case 16:
Int16 []
asInt16 = new Int16[samps];
Buffer.BlockCopy(byteArray, 0, asInt16, 0, bytes);
asFloat = Array.ConvertAll( asInt16, e => e / (float)Int16.MaxValue );
break;
default:
return false;
}
switch( channels ) {
case 1:
L = asFloat;
R = null;
return true;
case 2:
L = new float[samps];
R = new float[samps];
for( int i=0, s=0; i<samps; i++ ) {
L[i] = asFloat[s++];
R[i] = asFloat[s++];
}
return true;
default:
return false;
}
}
}
catch {
Debug.Log( "...Failed to load note: " + filename );
return false;
//left = new float[ 1 ]{ 0f };
}
return false;
}
종료기 [놀이를하고 오디오 데이터를 어레이로부터] [1]
PlayerEx pl = new PlayerEx();
private static void PlayArray(PlayerEx pl)
{
double fs = 8000; // sample freq
double freq = 1000; // desired tone
short[] mySound = new short[4000];
for (int i = 0; i < 4000; i++)
{
double t = (double)i / fs; // current time
mySound[i] = (short)(Math.Cos(t * freq) * (short.MaxValue));
}
IntPtr format = AudioCompressionManager.GetPcmFormat(1, 16, (int)fs);
pl.OpenPlayer(format);
byte[] mySoundByte = new byte[mySound.Length * 2];
Buffer.BlockCopy(mySound, 0, mySoundByte, 0, mySoundByte.Length);
pl.AddData(mySoundByte);
pl.StartPlay();
}
[1]: # tip32 티파스트라프스, http://alvas.net/alvas.audio