前田稔(Maeda Minoru)の超初心者のプログラム入門
![]()
![]()
//★ Mono 8bit 前田 稔 ★
using System;
using System.IO;
class Sound
{
static string file_name = "C:\\data\\test\\ringin.wav";
static byte[] byt = new byte[20000000];
static string out_name = "C:\\data\\test\\w.wav";
public static int Main()
{
int leng, siz;
int idx, p, v;
if (!File.Exists(file_name)) return -1; //ファイルの有無をチェック
FileStream reader = File.Open(file_name,FileMode.Open);
leng= reader.Read(byt,0,byt.Length);
reader.Close();
if (get_str(0)!="RIFF")
{ Console.WriteLine("Header Error=" + get_str(0));
Console.ReadLine();
return -1;
}
if (get_str(8)!="WAVE")
{ Console.WriteLine("Header Error=" + get_str(8));
Console.ReadLine();
return -1;
}
if (get_str(12)!="fmt ")
{ Console.WriteLine("Header Error=" + get_str(12));
Console.ReadLine();
return -1;
}
Console.WriteLine("len:" + get_int(16));
if (get_short(20) != 1)
{
Console.WriteLine("Format Error=" + get_short(20));
Console.ReadLine();
return -1;
}
if (get_short(22) != 1)
{
Console.WriteLine("Stereo Error=" + get_short(22));
Console.ReadLine();
return -1;
}
Console.WriteLine("Mono:8bit Sampling:" + get_int(24));
idx = 12;
idx = idx+get_int(idx+4)+8;
Console.WriteLine("次のチャンク:" + get_str(idx) + " idx:" + idx);
if (get_str(idx) != "data") return -1;
siz = get_int(idx+4);
Console.WriteLine("data size:" + siz);
idx = idx + 8;
for (int i=0; i<100; i++)
{ p = idx+i;
Console.WriteLine(byt[p]);
}
Console.ReadLine();
return 0;
}
// WAVE 領域からデータを取得
static string get_str(int idx)
{
char[] chary = new char[4];
chary[0] = (char)byt[idx];
chary[1] = (char)byt[idx + 1];
chary[2] = (char)byt[idx + 2];
chary[3] = (char)byt[idx + 3];
return new string(chary);
}
static short get_short(int idx)
{
short val;
val = (short)(byt[idx] + (byt[idx+1] << 8));
return val;
}
static int get_int(int idx)
{
int val;
val = byt[idx] + (byt[idx + 1] << 8) + (byt[idx + 2] << 16) + (byt[idx + 3] << 24);
return val;
}
// WAVE 領域からデータを設定
static void set_short(short v, int idx)
{
byt[idx] = (byte)(v & 0XFF);
byt[idx+1] = (byte)(v >> 8);
}
static void set_int(int v, int idx)
{
byt[idx] = (byte)(v & 0XFF);
byt[idx+1] = (byte)((v>>8) & 0XFF);
byt[idx+2] = (byte)((v>>16) & 0XFF);
byt[idx+3] = (byte)((v>>24) & 0XFF);
}
}
|
| 64617461 | data チャンク |
| FD260000 | データサイズ(9981) |
| 7B30658D... | 8ビットの音声データが続く |
//★ Mono 8bit 前田 稔 ★
・・・
static string out_name = "C:\\data\\test\\w.wav";
・・・
for (int i=0; i<siz; i++)
{ p = idx+i;
v = byt[p];
if (v<128) v = 128-(128-v)/2;
else v = (v-128)/2+128;
byt[p] = (byte)v;
}
FileStream writer = File.Create(out_name);
writer.Write(byt,0,leng);
writer.Close();
Console.Write("complete");
Console.ReadLine();
return 0;
}
|
![]()