annotate music2/nwatowav.cc @ 22:759202cf1756

Use automake
author thib
date Sat, 01 Nov 2008 21:14:12 +0000
parents 223b71206888
children 01aa5ddf7dc8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
223b71206888 Initial import
thib
parents:
diff changeset
1 /* nwatowav : Visual Arts 系のゲームのデモで使われる nwa 形式の
223b71206888 Initial import
thib
parents:
diff changeset
2 ** ファイルを wav 形式に変換する
223b71206888 Initial import
thib
parents:
diff changeset
3 **
223b71206888 Initial import
thib
parents:
diff changeset
4 ** compile : gcc -O2 -o nwatowav nwatowav.cc
223b71206888 Initial import
thib
parents:
diff changeset
5 ** usage : nwatowav [nwa-file [outfile]]
223b71206888 Initial import
thib
parents:
diff changeset
6 ** nwatowav [nwk-file [outfile]]
223b71206888 Initial import
thib
parents:
diff changeset
7 ** example : nwatowav HM06.nwa HM06.wav # BGMファイル。HM06.wav に展開される
223b71206888 Initial import
thib
parents:
diff changeset
8 ** nwatowav z2813.nwk z2813 # 音声ファイル。 z2813-100.wav などのファイル名で展開される
223b71206888 Initial import
thib
parents:
diff changeset
9 ** nwatowav z0513.ovk z0513 # 音声ファイル。 z0513-100.ogg などのファイル名で展開される
223b71206888 Initial import
thib
parents:
diff changeset
10 **
223b71206888 Initial import
thib
parents:
diff changeset
11 **
223b71206888 Initial import
thib
parents:
diff changeset
12 ** 2004.5.19 小松さん<s1100089@u-aizu.ac.jp> から CLANNAD の無圧縮nwa形式に対応する
223b71206888 Initial import
thib
parents:
diff changeset
13 ** パッチをいただいたので、適用しました。ありがとうございます。
223b71206888 Initial import
thib
parents:
diff changeset
14 ** 2006.9.10 「智代アフター」の音声ファイル形式 (complevel = 5) をサポート
223b71206888 Initial import
thib
parents:
diff changeset
15 ** .nwk という拡張子を持つファイルを受け取ると音声ファイルとして
223b71206888 Initial import
thib
parents:
diff changeset
16 ** 解釈、分割して展開するようにする
223b71206888 Initial import
thib
parents:
diff changeset
17 ** 2007.7.28 「リトルバスターズ!」の音声ファイル形式 (*.ovk; ogg 連結型)
223b71206888 Initial import
thib
parents:
diff changeset
18 ** をサポート。.ovk という拡張子をもつファイルを受け取ると
223b71206888 Initial import
thib
parents:
diff changeset
19 ** 音声ファイルとして解釈、分割して展開するようにする
223b71206888 Initial import
thib
parents:
diff changeset
20 ** 「リトルバスターズ!」のBGMファイルに多量のノイズが乗る問題も
223b71206888 Initial import
thib
parents:
diff changeset
21 **  解決(ランレングス圧縮の処理が不必要だった)
223b71206888 Initial import
thib
parents:
diff changeset
22 */
223b71206888 Initial import
thib
parents:
diff changeset
23
223b71206888 Initial import
thib
parents:
diff changeset
24 /*
223b71206888 Initial import
thib
parents:
diff changeset
25 * Copyright 2001-2007 jagarl / Kazunori Ueno <jagarl@creator.club.ne.jp>
223b71206888 Initial import
thib
parents:
diff changeset
26 * All Rights Reserved.
223b71206888 Initial import
thib
parents:
diff changeset
27 *
223b71206888 Initial import
thib
parents:
diff changeset
28 * Redistribution and use in source and binary forms, with or without
223b71206888 Initial import
thib
parents:
diff changeset
29 * modification, are permitted.
223b71206888 Initial import
thib
parents:
diff changeset
30 *
223b71206888 Initial import
thib
parents:
diff changeset
31 * このプログラムの作者は jagarl です。
223b71206888 Initial import
thib
parents:
diff changeset
32 *
223b71206888 Initial import
thib
parents:
diff changeset
33 * このプログラム、及びコンパイルによって生成したバイナリは
223b71206888 Initial import
thib
parents:
diff changeset
34 * プログラムを変更する、しないにかかわらず再配布可能です。
223b71206888 Initial import
thib
parents:
diff changeset
35 * その際、上記 Copyright 表示を保持するなどの条件は課しま
223b71206888 Initial import
thib
parents:
diff changeset
36 * せん。対応が面倒なのでバグ報告を除き、メールで連絡をする
223b71206888 Initial import
thib
parents:
diff changeset
37 * などの必要もありません。ソースの一部を流用することを含め、
223b71206888 Initial import
thib
parents:
diff changeset
38 * ご自由にお使いください。
223b71206888 Initial import
thib
parents:
diff changeset
39 *
223b71206888 Initial import
thib
parents:
diff changeset
40 * THIS SOFTWARE IS PROVIDED BY KAZUNORI 'jagarl' UENO ``AS IS'' AND ANY
223b71206888 Initial import
thib
parents:
diff changeset
41 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
223b71206888 Initial import
thib
parents:
diff changeset
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
223b71206888 Initial import
thib
parents:
diff changeset
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KAZUNORI UENO BE LIABLE
223b71206888 Initial import
thib
parents:
diff changeset
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
223b71206888 Initial import
thib
parents:
diff changeset
45 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
223b71206888 Initial import
thib
parents:
diff changeset
46 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
223b71206888 Initial import
thib
parents:
diff changeset
47 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
223b71206888 Initial import
thib
parents:
diff changeset
48 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223b71206888 Initial import
thib
parents:
diff changeset
49 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
223b71206888 Initial import
thib
parents:
diff changeset
50 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
223b71206888 Initial import
thib
parents:
diff changeset
51 * DAMAGE.
223b71206888 Initial import
thib
parents:
diff changeset
52 *
223b71206888 Initial import
thib
parents:
diff changeset
53 */
223b71206888 Initial import
thib
parents:
diff changeset
54
223b71206888 Initial import
thib
parents:
diff changeset
55 /********************************************
223b71206888 Initial import
thib
parents:
diff changeset
56 **
223b71206888 Initial import
thib
parents:
diff changeset
57 ** nwa フォーマットについて
223b71206888 Initial import
thib
parents:
diff changeset
58 **
223b71206888 Initial import
thib
parents:
diff changeset
59 ** 全体としては以下の構造を持つ
223b71206888 Initial import
thib
parents:
diff changeset
60 ** NWA Header
223b71206888 Initial import
thib
parents:
diff changeset
61 ** data offset index
223b71206888 Initial import
thib
parents:
diff changeset
62 ** data block<0>
223b71206888 Initial import
thib
parents:
diff changeset
63 ** data block<1>
223b71206888 Initial import
thib
parents:
diff changeset
64 ** ...
223b71206888 Initial import
thib
parents:
diff changeset
65 ** data block<N>
223b71206888 Initial import
thib
parents:
diff changeset
66 **
223b71206888 Initial import
thib
parents:
diff changeset
67 ** NWA Header: ファイル先頭から 44 bytes
223b71206888 Initial import
thib
parents:
diff changeset
68 ** magic number などはないのでnwa ファイルかは
223b71206888 Initial import
thib
parents:
diff changeset
69 ** データの整合性から判断する必要がある
223b71206888 Initial import
thib
parents:
diff changeset
70 ** データは全て little endian で、
223b71206888 Initial import
thib
parents:
diff changeset
71 ** short(signed 2byte)または int(signed 4byte) である。
223b71206888 Initial import
thib
parents:
diff changeset
72 **
223b71206888 Initial import
thib
parents:
diff changeset
73 ** +00 short channel 数(1/2)
223b71206888 Initial import
thib
parents:
diff changeset
74 ** +02 short sample 一つあたりの bit 数(16)
223b71206888 Initial import
thib
parents:
diff changeset
75 ** +04 int 周波数(一秒あたりのデータ数)
223b71206888 Initial import
thib
parents:
diff changeset
76 ** +08 int 圧縮レベル:-1~5.2で最小のデータ、0で最大の復元度(-1は無圧縮rawデータとみなされる)
223b71206888 Initial import
thib
parents:
diff changeset
77 ** +12 int ?
223b71206888 Initial import
thib
parents:
diff changeset
78 ** +16 int ブロック数
223b71206888 Initial import
thib
parents:
diff changeset
79 ** +20 int 展開後のデータの大きさ(バイト単位)
223b71206888 Initial import
thib
parents:
diff changeset
80 ** +24 int 圧縮時のデータの大きさ(nwa ファイルの大きさ。バイト単位)
223b71206888 Initial import
thib
parents:
diff changeset
81 ** +28 int サンプル数:展開後のデータ数(16bit dataなら short 単位==サンプル単位のデータの大きさ)
223b71206888 Initial import
thib
parents:
diff changeset
82 ** +32 int データ1ブロックを展開した時のサンプル単位のデータ数
223b71206888 Initial import
thib
parents:
diff changeset
83 ** +36 int 最終ブロックを展開した時のサンプル単位のデータ数
223b71206888 Initial import
thib
parents:
diff changeset
84 ** +40 int ?
223b71206888 Initial import
thib
parents:
diff changeset
85 **
223b71206888 Initial import
thib
parents:
diff changeset
86 ** data offset index
223b71206888 Initial import
thib
parents:
diff changeset
87 ** 全ブロック数 x 4 byte のデータ
223b71206888 Initial import
thib
parents:
diff changeset
88 ** それぞれ int のデータが全ブロック数続いている
223b71206888 Initial import
thib
parents:
diff changeset
89 **
223b71206888 Initial import
thib
parents:
diff changeset
90 ** データブロックの先頭を指すファイル先頭からの位置(オフセット)
223b71206888 Initial import
thib
parents:
diff changeset
91 ** が格納されている
223b71206888 Initial import
thib
parents:
diff changeset
92 **
223b71206888 Initial import
thib
parents:
diff changeset
93 ** data block
223b71206888 Initial import
thib
parents:
diff changeset
94 ** 長さは可変。展開することで一定の大きさをもつデータに展開される。
223b71206888 Initial import
thib
parents:
diff changeset
95 ** データはDPCM形式。元 PCM データが a,b,c ならば (a),b-a, c-b と
223b71206888 Initial import
thib
parents:
diff changeset
96 ** いった差分データが、仮数3-5bit,指数3bitの形式で保存されている。
223b71206888 Initial import
thib
parents:
diff changeset
97 ** 結果的に、16bit のデータが多くの場合 6-8bit で格納される。
223b71206888 Initial import
thib
parents:
diff changeset
98 ** 仮数のビット数は圧縮レベル0で5bit、圧縮レベル2で3bitとなる。
223b71206888 Initial import
thib
parents:
diff changeset
99 ** 以下、圧縮レベル2の場合について話を進める。
223b71206888 Initial import
thib
parents:
diff changeset
100 ** モノラルの場合:
223b71206888 Initial import
thib
parents:
diff changeset
101 ** +00 short ブロック内の最初のデータ
223b71206888 Initial import
thib
parents:
diff changeset
102 ** +02- bit stream
223b71206888 Initial import
thib
parents:
diff changeset
103 ** ステレオの場合:
223b71206888 Initial import
thib
parents:
diff changeset
104 ** +00 short 左(?)チャンネルの最初のデータ
223b71206888 Initial import
thib
parents:
diff changeset
105 ** +02 short 右(?)チャンネルの最初のデータ
223b71206888 Initial import
thib
parents:
diff changeset
106 ** +04- bit stream
223b71206888 Initial import
thib
parents:
diff changeset
107 **
223b71206888 Initial import
thib
parents:
diff changeset
108 ** 差分データの精度が高くないので各ブロックの先頭で
223b71206888 Initial import
thib
parents:
diff changeset
109 ** 正確なデータにより補正される(?)
223b71206888 Initial import
thib
parents:
diff changeset
110 **
223b71206888 Initial import
thib
parents:
diff changeset
111 ** bit stream
223b71206888 Initial import
thib
parents:
diff changeset
112 ** little endian
223b71206888 Initial import
thib
parents:
diff changeset
113 ** +0 - +2 : 指数
223b71206888 Initial import
thib
parents:
diff changeset
114 ** +3 - +5 : 仮数
223b71206888 Initial import
thib
parents:
diff changeset
115 ** の形式。例えば a,b,c という8bitデータがあれば、
223b71206888 Initial import
thib
parents:
diff changeset
116 ** a&0x07 : データ1の指数
223b71206888 Initial import
thib
parents:
diff changeset
117 ** (a>>3)&0x07 : データ1の仮数(signed ;
223b71206888 Initial import
thib
parents:
diff changeset
118 ** ((b<<2)|(a>>6))&0x07 : データ2の指数
223b71206888 Initial import
thib
parents:
diff changeset
119 ** (b>>1)&0x07 : データ2の仮数
223b71206888 Initial import
thib
parents:
diff changeset
120 ** となる。
223b71206888 Initial import
thib
parents:
diff changeset
121 ** ただし、指数の値により仮数のbit数が変化することがある。
223b71206888 Initial import
thib
parents:
diff changeset
122 ** 指数 = 1 - 6 の場合:
223b71206888 Initial import
thib
parents:
diff changeset
123 ** a=指数、b=仮数、p=前のデータとして、今回のデータd は
223b71206888 Initial import
thib
parents:
diff changeset
124 ** bの2bit目が立っている場合:
223b71206888 Initial import
thib
parents:
diff changeset
125 ** d = p - (b&3)<<(4+a)
223b71206888 Initial import
thib
parents:
diff changeset
126 ** 立ってない場合:
223b71206888 Initial import
thib
parents:
diff changeset
127 ** d = p + (b&3)<<(4+a)
223b71206888 Initial import
thib
parents:
diff changeset
128 ** 指数 = 0 の場合:仮数は存在しない(データは3bitとなる)
223b71206888 Initial import
thib
parents:
diff changeset
129 ** d = p
223b71206888 Initial import
thib
parents:
diff changeset
130 ** 「智代アフター」の音声ファイル (complevel == 5) ではランレングス圧縮用に使われている。
223b71206888 Initial import
thib
parents:
diff changeset
131 ** 指数 = 7
223b71206888 Initial import
thib
parents:
diff changeset
132 ** 次の bit が立っている場合:
223b71206888 Initial import
thib
parents:
diff changeset
133 ** d = 0 (現在未使用)
223b71206888 Initial import
thib
parents:
diff changeset
134 ** (データは4bitとなる)
223b71206888 Initial import
thib
parents:
diff changeset
135 ** 次の bit が立ってない場合:
223b71206888 Initial import
thib
parents:
diff changeset
136 ** complevel = 0,1,2:
223b71206888 Initial import
thib
parents:
diff changeset
137 ** 仮数 b = 6bit
223b71206888 Initial import
thib
parents:
diff changeset
138 ** b の 5bit 目が立っている場合:
223b71206888 Initial import
thib
parents:
diff changeset
139 ** d = p - (b&0x1f)<<(4+7)
223b71206888 Initial import
thib
parents:
diff changeset
140 ** 立ってない場合:
223b71206888 Initial import
thib
parents:
diff changeset
141 ** d = p + (b&0x1f)<<(4+7)
223b71206888 Initial import
thib
parents:
diff changeset
142 ** (データは10bitとなる)
223b71206888 Initial import
thib
parents:
diff changeset
143 ** complevel = 3,4,5:
223b71206888 Initial import
thib
parents:
diff changeset
144 ** 仮数 b = 8bit
223b71206888 Initial import
thib
parents:
diff changeset
145 ** b の 7bit 目が立っている場合:
223b71206888 Initial import
thib
parents:
diff changeset
146 ** d = p - (b&0x7f)<<9
223b71206888 Initial import
thib
parents:
diff changeset
147 ** 立ってない場合:
223b71206888 Initial import
thib
parents:
diff changeset
148 ** d = p + (b&0x1f)<<9
223b71206888 Initial import
thib
parents:
diff changeset
149 ** (データは10bitとなる)
223b71206888 Initial import
thib
parents:
diff changeset
150 **
223b71206888 Initial import
thib
parents:
diff changeset
151 ** 圧縮レベルが異なる場合、たとえば圧縮レベル==0で
223b71206888 Initial import
thib
parents:
diff changeset
152 ** 指数==1~6でdの最上位bitが立っている場合
223b71206888 Initial import
thib
parents:
diff changeset
153 ** d = p - (b&0x0f)<<(2+a)
223b71206888 Initial import
thib
parents:
diff changeset
154 ** 指数==7でdの最上位bitが立っている場合
223b71206888 Initial import
thib
parents:
diff changeset
155 ** d = p - (b&0x7f)<<(2+7)
223b71206888 Initial import
thib
parents:
diff changeset
156 ** (b : 8bitなのでデータは12bitとなる)
223b71206888 Initial import
thib
parents:
diff changeset
157 ** のように、精度だけが変化するようになっている。
223b71206888 Initial import
thib
parents:
diff changeset
158 **
223b71206888 Initial import
thib
parents:
diff changeset
159 ** ヘッダ読み込みについてはNWAData::ReadHeader()参照
223b71206888 Initial import
thib
parents:
diff changeset
160 ** bit stream からのデータ展開については NWADecode()参照
223b71206888 Initial import
thib
parents:
diff changeset
161 **************************************************************
223b71206888 Initial import
thib
parents:
diff changeset
162 */
223b71206888 Initial import
thib
parents:
diff changeset
163
223b71206888 Initial import
thib
parents:
diff changeset
164 // #define NDEBUG /* なぜか assertが入った方が速い、、、 */
223b71206888 Initial import
thib
parents:
diff changeset
165
223b71206888 Initial import
thib
parents:
diff changeset
166 #include<stdio.h>
223b71206888 Initial import
thib
parents:
diff changeset
167 #include<stdlib.h>
223b71206888 Initial import
thib
parents:
diff changeset
168 #include<unistd.h> // for isatty() function
223b71206888 Initial import
thib
parents:
diff changeset
169 #include<sys/stat.h>
223b71206888 Initial import
thib
parents:
diff changeset
170 #include<string.h>
223b71206888 Initial import
thib
parents:
diff changeset
171
223b71206888 Initial import
thib
parents:
diff changeset
172
223b71206888 Initial import
thib
parents:
diff changeset
173 #ifdef WORDS_BIGENDIAN
223b71206888 Initial import
thib
parents:
diff changeset
174 #error Sorry, This program does not support BIG-ENDIAN system yet.
223b71206888 Initial import
thib
parents:
diff changeset
175 /* もし big endian のシステムに対応させる場合
223b71206888 Initial import
thib
parents:
diff changeset
176 ** 以下の *_little_endian_* 及び
223b71206888 Initial import
thib
parents:
diff changeset
177 ** getbits() 関数を変更する必要がある
223b71206888 Initial import
thib
parents:
diff changeset
178 */
223b71206888 Initial import
thib
parents:
diff changeset
179 #endif
223b71206888 Initial import
thib
parents:
diff changeset
180
223b71206888 Initial import
thib
parents:
diff changeset
181 inline int read_little_endian_int(const char* buf) {
223b71206888 Initial import
thib
parents:
diff changeset
182 return *(int*)buf;
223b71206888 Initial import
thib
parents:
diff changeset
183 }
223b71206888 Initial import
thib
parents:
diff changeset
184
223b71206888 Initial import
thib
parents:
diff changeset
185 inline int read_little_endian_short(const char* buf) {
223b71206888 Initial import
thib
parents:
diff changeset
186 return *(short*)buf;
223b71206888 Initial import
thib
parents:
diff changeset
187 }
223b71206888 Initial import
thib
parents:
diff changeset
188
223b71206888 Initial import
thib
parents:
diff changeset
189 inline int write_little_endian_int(char* buf, int number) {
223b71206888 Initial import
thib
parents:
diff changeset
190 int c = *(int*)buf; *(int*)buf = number; return c;
223b71206888 Initial import
thib
parents:
diff changeset
191 }
223b71206888 Initial import
thib
parents:
diff changeset
192
223b71206888 Initial import
thib
parents:
diff changeset
193 inline int write_little_endian_short(char* buf, int number) {
223b71206888 Initial import
thib
parents:
diff changeset
194 int c = *(short*)buf; *(short*)buf = number; return c;
223b71206888 Initial import
thib
parents:
diff changeset
195 }
223b71206888 Initial import
thib
parents:
diff changeset
196 inline int getbits(const char*& data, int& shift, int bits) {
223b71206888 Initial import
thib
parents:
diff changeset
197 if (shift > 8) { data++; shift-=8;}
223b71206888 Initial import
thib
parents:
diff changeset
198 int ret = read_little_endian_short(data)>>shift;
223b71206888 Initial import
thib
parents:
diff changeset
199 shift += bits;
223b71206888 Initial import
thib
parents:
diff changeset
200 return ret & ((1<<bits)-1); /* mask */
223b71206888 Initial import
thib
parents:
diff changeset
201 }
223b71206888 Initial import
thib
parents:
diff changeset
202
223b71206888 Initial import
thib
parents:
diff changeset
203 /* 指定された形式のヘッダをつくる */
223b71206888 Initial import
thib
parents:
diff changeset
204 const char* make_wavheader(int size, int channels, int bps, int freq) {
223b71206888 Initial import
thib
parents:
diff changeset
205 static char wavheader[0x2c] = {
223b71206888 Initial import
thib
parents:
diff changeset
206 'R','I','F','F',
223b71206888 Initial import
thib
parents:
diff changeset
207 0,0,0,0, /* +0x04: riff size*/
223b71206888 Initial import
thib
parents:
diff changeset
208 'W','A','V','E',
223b71206888 Initial import
thib
parents:
diff changeset
209 'f','m','t',' ',
223b71206888 Initial import
thib
parents:
diff changeset
210 16,0,0,0, /* +0x10 : fmt size=0x10 */
223b71206888 Initial import
thib
parents:
diff changeset
211 1, 0, /* +0x14 : tag : pcm = 1 */
223b71206888 Initial import
thib
parents:
diff changeset
212 2, 0, /* +0x16 : channels */
223b71206888 Initial import
thib
parents:
diff changeset
213 0,0,0,0, /* +0x18 : samples per second */
223b71206888 Initial import
thib
parents:
diff changeset
214 0,0,0,0, /* +0x1c : average bytes per second */
223b71206888 Initial import
thib
parents:
diff changeset
215 0,0, /* +0x20 : block alignment */
223b71206888 Initial import
thib
parents:
diff changeset
216 0,0, /* +0x22 : bits per sample */
223b71206888 Initial import
thib
parents:
diff changeset
217 'd','a','t','a',
223b71206888 Initial import
thib
parents:
diff changeset
218 0,0,0,0};/* +0x28 : data size */
223b71206888 Initial import
thib
parents:
diff changeset
219 write_little_endian_int(wavheader+0x04, size+0x24);
223b71206888 Initial import
thib
parents:
diff changeset
220 write_little_endian_int(wavheader+0x28, size);
223b71206888 Initial import
thib
parents:
diff changeset
221 write_little_endian_short(wavheader+0x16, channels);
223b71206888 Initial import
thib
parents:
diff changeset
222 write_little_endian_short(wavheader+0x22, bps);
223b71206888 Initial import
thib
parents:
diff changeset
223 write_little_endian_int(wavheader+0x18, freq);
223b71206888 Initial import
thib
parents:
diff changeset
224 int byps = (bps+7)>>3;
223b71206888 Initial import
thib
parents:
diff changeset
225 write_little_endian_int(wavheader+0x1c, freq*byps*channels);
223b71206888 Initial import
thib
parents:
diff changeset
226 write_little_endian_short(wavheader+0x20, byps*channels);
223b71206888 Initial import
thib
parents:
diff changeset
227 return wavheader;
223b71206888 Initial import
thib
parents:
diff changeset
228 }
223b71206888 Initial import
thib
parents:
diff changeset
229
223b71206888 Initial import
thib
parents:
diff changeset
230 /* NWA の bitstream展開に必要となる情報 */
223b71206888 Initial import
thib
parents:
diff changeset
231 class NWAInfo {
223b71206888 Initial import
thib
parents:
diff changeset
232 int channels;
223b71206888 Initial import
thib
parents:
diff changeset
233 int bps;
223b71206888 Initial import
thib
parents:
diff changeset
234 int complevel;
223b71206888 Initial import
thib
parents:
diff changeset
235 bool use_runlength;
223b71206888 Initial import
thib
parents:
diff changeset
236 public:
223b71206888 Initial import
thib
parents:
diff changeset
237 NWAInfo(int c,int b,int cl) {
223b71206888 Initial import
thib
parents:
diff changeset
238 channels=c;
223b71206888 Initial import
thib
parents:
diff changeset
239 bps=b;
223b71206888 Initial import
thib
parents:
diff changeset
240 complevel=cl;
223b71206888 Initial import
thib
parents:
diff changeset
241 use_runlength = false;
223b71206888 Initial import
thib
parents:
diff changeset
242 if (cl == 5) {
223b71206888 Initial import
thib
parents:
diff changeset
243 use_runlength = true; // Tomoyo After (.nwk koe file)
223b71206888 Initial import
thib
parents:
diff changeset
244 if (channels == 2) use_runlength = false; // BGM*.nwa in Little Busters!
223b71206888 Initial import
thib
parents:
diff changeset
245 }
223b71206888 Initial import
thib
parents:
diff changeset
246 }
223b71206888 Initial import
thib
parents:
diff changeset
247 int Channels(void) const{return channels;}
223b71206888 Initial import
thib
parents:
diff changeset
248 int Bps(void) const { return bps;}
223b71206888 Initial import
thib
parents:
diff changeset
249 int CompLevel(void) const { return complevel;}
223b71206888 Initial import
thib
parents:
diff changeset
250 int UseRunLength(void) const { return use_runlength; }
223b71206888 Initial import
thib
parents:
diff changeset
251 };
223b71206888 Initial import
thib
parents:
diff changeset
252
223b71206888 Initial import
thib
parents:
diff changeset
253 template<class NWAI> void NWADecode(const NWAI& info,const char* data, char* outdata, int datasize, int outdatasize) {
223b71206888 Initial import
thib
parents:
diff changeset
254 int d[2];
223b71206888 Initial import
thib
parents:
diff changeset
255 int i,j;
223b71206888 Initial import
thib
parents:
diff changeset
256 int shift = 0;
223b71206888 Initial import
thib
parents:
diff changeset
257 const char* dataend = data+datasize;
223b71206888 Initial import
thib
parents:
diff changeset
258 /* 最初のデータを読み込む */
223b71206888 Initial import
thib
parents:
diff changeset
259 if (info.Bps() == 8) {d[0] = *data++; datasize--;}
223b71206888 Initial import
thib
parents:
diff changeset
260 else /* info.Bps() == 16 */ {d[0] = read_little_endian_short(data); data+=2; datasize-=2;}
223b71206888 Initial import
thib
parents:
diff changeset
261 if (info.Channels() == 2) {
223b71206888 Initial import
thib
parents:
diff changeset
262 if (info.Bps() == 8) {d[1] = *data++; datasize--;}
223b71206888 Initial import
thib
parents:
diff changeset
263 else /* info.Bps() == 16 */ {d[1] = read_little_endian_short(data); data+=2; datasize-=2;}
223b71206888 Initial import
thib
parents:
diff changeset
264 }
223b71206888 Initial import
thib
parents:
diff changeset
265 int dsize = outdatasize / (info.Bps()/8);
223b71206888 Initial import
thib
parents:
diff changeset
266 int flip_flag = 0; /* stereo 用 */
223b71206888 Initial import
thib
parents:
diff changeset
267 int runlength = 0;
223b71206888 Initial import
thib
parents:
diff changeset
268 for (i=0; i<dsize; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
269 if (data >= dataend) break;
223b71206888 Initial import
thib
parents:
diff changeset
270 if (runlength == 0) { // コピーループ中でないならデータ読み込み
223b71206888 Initial import
thib
parents:
diff changeset
271 int type = getbits(data, shift, 3);
223b71206888 Initial import
thib
parents:
diff changeset
272 /* type により分岐:0, 1-6, 7 */
223b71206888 Initial import
thib
parents:
diff changeset
273 if (type == 7) {
223b71206888 Initial import
thib
parents:
diff changeset
274 /* 7 : 大きな差分 */
223b71206888 Initial import
thib
parents:
diff changeset
275 /* RunLength() 有効時(CompLevel==5, 音声ファイル) では無効 */
223b71206888 Initial import
thib
parents:
diff changeset
276 if (getbits(data, shift, 1) == 1) {
223b71206888 Initial import
thib
parents:
diff changeset
277 d[flip_flag] = 0; /* 未使用 */
223b71206888 Initial import
thib
parents:
diff changeset
278 } else {
223b71206888 Initial import
thib
parents:
diff changeset
279 int BITS, SHIFT;
223b71206888 Initial import
thib
parents:
diff changeset
280 if (info.CompLevel() >= 3) {
223b71206888 Initial import
thib
parents:
diff changeset
281 BITS = 8;
223b71206888 Initial import
thib
parents:
diff changeset
282 SHIFT = 9;
223b71206888 Initial import
thib
parents:
diff changeset
283 } else {
223b71206888 Initial import
thib
parents:
diff changeset
284 BITS = 8-info.CompLevel();
223b71206888 Initial import
thib
parents:
diff changeset
285 SHIFT = 2+7+info.CompLevel();
223b71206888 Initial import
thib
parents:
diff changeset
286 }
223b71206888 Initial import
thib
parents:
diff changeset
287 const int MASK1 = (1<<(BITS-1));
223b71206888 Initial import
thib
parents:
diff changeset
288 const int MASK2 = (1<<(BITS-1))-1;
223b71206888 Initial import
thib
parents:
diff changeset
289 int b = getbits(data, shift, BITS);
223b71206888 Initial import
thib
parents:
diff changeset
290 if (b&MASK1)
223b71206888 Initial import
thib
parents:
diff changeset
291 d[flip_flag] -= (b&MASK2)<<SHIFT;
223b71206888 Initial import
thib
parents:
diff changeset
292 else
223b71206888 Initial import
thib
parents:
diff changeset
293 d[flip_flag] += (b&MASK2)<<SHIFT;
223b71206888 Initial import
thib
parents:
diff changeset
294 }
223b71206888 Initial import
thib
parents:
diff changeset
295 } else if (type != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
296 /* 1-6 : 通常の差分 */
223b71206888 Initial import
thib
parents:
diff changeset
297 int BITS, SHIFT;
223b71206888 Initial import
thib
parents:
diff changeset
298 if (info.CompLevel() >= 3) {
223b71206888 Initial import
thib
parents:
diff changeset
299 BITS = info.CompLevel()+3;
223b71206888 Initial import
thib
parents:
diff changeset
300 SHIFT = 1+type;
223b71206888 Initial import
thib
parents:
diff changeset
301 } else {
223b71206888 Initial import
thib
parents:
diff changeset
302 BITS = 5-info.CompLevel();
223b71206888 Initial import
thib
parents:
diff changeset
303 SHIFT = 2+type+info.CompLevel();
223b71206888 Initial import
thib
parents:
diff changeset
304 }
223b71206888 Initial import
thib
parents:
diff changeset
305 const int MASK1 = (1<<(BITS-1));
223b71206888 Initial import
thib
parents:
diff changeset
306 const int MASK2 = (1<<(BITS-1))-1;
223b71206888 Initial import
thib
parents:
diff changeset
307 int b = getbits(data, shift, BITS);
223b71206888 Initial import
thib
parents:
diff changeset
308 if (b&MASK1)
223b71206888 Initial import
thib
parents:
diff changeset
309 d[flip_flag] -= (b&MASK2)<<SHIFT;
223b71206888 Initial import
thib
parents:
diff changeset
310 else
223b71206888 Initial import
thib
parents:
diff changeset
311 d[flip_flag] += (b&MASK2)<<SHIFT;
223b71206888 Initial import
thib
parents:
diff changeset
312 } else { /* type == 0 */
223b71206888 Initial import
thib
parents:
diff changeset
313 /* ランレングス圧縮なしの場合はなにもしない */
223b71206888 Initial import
thib
parents:
diff changeset
314 if (info.UseRunLength() == true) {
223b71206888 Initial import
thib
parents:
diff changeset
315 /* ランレングス圧縮ありの場合 */
223b71206888 Initial import
thib
parents:
diff changeset
316 runlength = getbits(data,shift,1);
223b71206888 Initial import
thib
parents:
diff changeset
317 if (runlength==1) {
223b71206888 Initial import
thib
parents:
diff changeset
318 runlength = getbits(data,shift,2);
223b71206888 Initial import
thib
parents:
diff changeset
319 if (runlength == 3) {
223b71206888 Initial import
thib
parents:
diff changeset
320 runlength = getbits(data, shift, 8);
223b71206888 Initial import
thib
parents:
diff changeset
321 }
223b71206888 Initial import
thib
parents:
diff changeset
322 }
223b71206888 Initial import
thib
parents:
diff changeset
323 }
223b71206888 Initial import
thib
parents:
diff changeset
324 }
223b71206888 Initial import
thib
parents:
diff changeset
325 } else {
223b71206888 Initial import
thib
parents:
diff changeset
326 runlength--;
223b71206888 Initial import
thib
parents:
diff changeset
327 }
223b71206888 Initial import
thib
parents:
diff changeset
328 if (info.Bps() == 8) {
223b71206888 Initial import
thib
parents:
diff changeset
329 *outdata++ = d[flip_flag];
223b71206888 Initial import
thib
parents:
diff changeset
330 } else {
223b71206888 Initial import
thib
parents:
diff changeset
331 write_little_endian_short(outdata, d[flip_flag]);
223b71206888 Initial import
thib
parents:
diff changeset
332 outdata += 2;
223b71206888 Initial import
thib
parents:
diff changeset
333 }
223b71206888 Initial import
thib
parents:
diff changeset
334 if (info.Channels() == 2) flip_flag ^= 1; /* channel 切り替え */
223b71206888 Initial import
thib
parents:
diff changeset
335 }
223b71206888 Initial import
thib
parents:
diff changeset
336 return;
223b71206888 Initial import
thib
parents:
diff changeset
337 };
223b71206888 Initial import
thib
parents:
diff changeset
338
223b71206888 Initial import
thib
parents:
diff changeset
339 class NWAData {
223b71206888 Initial import
thib
parents:
diff changeset
340 public:
223b71206888 Initial import
thib
parents:
diff changeset
341 int channels;
223b71206888 Initial import
thib
parents:
diff changeset
342 int bps; /* bits per sample */
223b71206888 Initial import
thib
parents:
diff changeset
343 int freq; /* samples per second */
223b71206888 Initial import
thib
parents:
diff changeset
344 private:
223b71206888 Initial import
thib
parents:
diff changeset
345 int complevel; /* compression level */
223b71206888 Initial import
thib
parents:
diff changeset
346 int dummy; /* ? : 0x00 */
223b71206888 Initial import
thib
parents:
diff changeset
347 public:
223b71206888 Initial import
thib
parents:
diff changeset
348 int blocks; /* block count */
223b71206888 Initial import
thib
parents:
diff changeset
349 int datasize; /* all data size */
223b71206888 Initial import
thib
parents:
diff changeset
350 private:
223b71206888 Initial import
thib
parents:
diff changeset
351 int compdatasize; /* compressed data size */
223b71206888 Initial import
thib
parents:
diff changeset
352 int samplecount; /* all samples */
223b71206888 Initial import
thib
parents:
diff changeset
353 int blocksize; /* samples per block */
223b71206888 Initial import
thib
parents:
diff changeset
354 int restsize; /* samples of the last block */
223b71206888 Initial import
thib
parents:
diff changeset
355 int dummy2; /* ? : 0x89 */
223b71206888 Initial import
thib
parents:
diff changeset
356 int curblock;
223b71206888 Initial import
thib
parents:
diff changeset
357 int* offsets;
223b71206888 Initial import
thib
parents:
diff changeset
358 int offset_start;
223b71206888 Initial import
thib
parents:
diff changeset
359 int filesize;
223b71206888 Initial import
thib
parents:
diff changeset
360 char* tmpdata;
223b71206888 Initial import
thib
parents:
diff changeset
361 public:
223b71206888 Initial import
thib
parents:
diff changeset
362 void ReadHeader(FILE* in, int file_size=-1);
223b71206888 Initial import
thib
parents:
diff changeset
363 int CheckHeader(void); /* false: invalid true: valid */
223b71206888 Initial import
thib
parents:
diff changeset
364 NWAData(void) {
223b71206888 Initial import
thib
parents:
diff changeset
365 offsets = 0;
223b71206888 Initial import
thib
parents:
diff changeset
366 tmpdata = 0;
223b71206888 Initial import
thib
parents:
diff changeset
367 }
223b71206888 Initial import
thib
parents:
diff changeset
368 ~NWAData(void) {
223b71206888 Initial import
thib
parents:
diff changeset
369 if (offsets) delete[] offsets;
223b71206888 Initial import
thib
parents:
diff changeset
370 if (tmpdata) delete[] tmpdata;
223b71206888 Initial import
thib
parents:
diff changeset
371 }
223b71206888 Initial import
thib
parents:
diff changeset
372 int BlockLength(void) {
223b71206888 Initial import
thib
parents:
diff changeset
373 if (complevel != -1) {
223b71206888 Initial import
thib
parents:
diff changeset
374 if (offsets == 0) return false;
223b71206888 Initial import
thib
parents:
diff changeset
375 if (tmpdata == 0) return false;
223b71206888 Initial import
thib
parents:
diff changeset
376 }
223b71206888 Initial import
thib
parents:
diff changeset
377 return blocksize * (bps/8);
223b71206888 Initial import
thib
parents:
diff changeset
378 }
223b71206888 Initial import
thib
parents:
diff changeset
379 /* data は BlockLength 以上の長さを持つこと
223b71206888 Initial import
thib
parents:
diff changeset
380 ** 返り値は作成したデータの長さ。終了時は 0。
223b71206888 Initial import
thib
parents:
diff changeset
381 ** エラー時は -1
223b71206888 Initial import
thib
parents:
diff changeset
382 */
223b71206888 Initial import
thib
parents:
diff changeset
383 int Decode(FILE* in, char* data, int& skip_count);
223b71206888 Initial import
thib
parents:
diff changeset
384 void Rewind(FILE* in);
223b71206888 Initial import
thib
parents:
diff changeset
385 };
223b71206888 Initial import
thib
parents:
diff changeset
386
223b71206888 Initial import
thib
parents:
diff changeset
387 void NWAData::ReadHeader(FILE* in, int _file_size) {
223b71206888 Initial import
thib
parents:
diff changeset
388 char header[0x2c];
223b71206888 Initial import
thib
parents:
diff changeset
389 struct stat sb;
223b71206888 Initial import
thib
parents:
diff changeset
390 int i;
223b71206888 Initial import
thib
parents:
diff changeset
391 if (offsets) delete[] offsets;
223b71206888 Initial import
thib
parents:
diff changeset
392 if (tmpdata) delete[] tmpdata;
223b71206888 Initial import
thib
parents:
diff changeset
393 offsets = 0;
223b71206888 Initial import
thib
parents:
diff changeset
394 tmpdata = 0;
223b71206888 Initial import
thib
parents:
diff changeset
395 filesize = 0;
223b71206888 Initial import
thib
parents:
diff changeset
396 offset_start = ftell(in);
223b71206888 Initial import
thib
parents:
diff changeset
397 if (offset_start == -1) offset_start = 0;
223b71206888 Initial import
thib
parents:
diff changeset
398 if (_file_size != -1) filesize = _file_size;
223b71206888 Initial import
thib
parents:
diff changeset
399 curblock = -1;
223b71206888 Initial import
thib
parents:
diff changeset
400 /* header 読み込み */
223b71206888 Initial import
thib
parents:
diff changeset
401 if (in == 0 || feof(in) || ferror(in)) {
223b71206888 Initial import
thib
parents:
diff changeset
402 fprintf(stderr,"invalid stream\n");
223b71206888 Initial import
thib
parents:
diff changeset
403 return;
223b71206888 Initial import
thib
parents:
diff changeset
404 }
223b71206888 Initial import
thib
parents:
diff changeset
405 fread(header, 0x2c, 1, in);
223b71206888 Initial import
thib
parents:
diff changeset
406 if (feof(in) || ferror(in)) {
223b71206888 Initial import
thib
parents:
diff changeset
407 fprintf(stderr,"invalid stream\n");
223b71206888 Initial import
thib
parents:
diff changeset
408 return;
223b71206888 Initial import
thib
parents:
diff changeset
409 }
223b71206888 Initial import
thib
parents:
diff changeset
410 channels = read_little_endian_short(header+0x00);
223b71206888 Initial import
thib
parents:
diff changeset
411 bps = read_little_endian_short(header+0x02);
223b71206888 Initial import
thib
parents:
diff changeset
412 freq = read_little_endian_int(header+0x04);
223b71206888 Initial import
thib
parents:
diff changeset
413 complevel = read_little_endian_int(header+0x08);
223b71206888 Initial import
thib
parents:
diff changeset
414 dummy = read_little_endian_int(header+0x0c);
223b71206888 Initial import
thib
parents:
diff changeset
415 blocks = read_little_endian_int(header+0x10);
223b71206888 Initial import
thib
parents:
diff changeset
416 datasize = read_little_endian_int(header+0x14);
223b71206888 Initial import
thib
parents:
diff changeset
417 compdatasize = read_little_endian_int(header+0x18);
223b71206888 Initial import
thib
parents:
diff changeset
418 samplecount = read_little_endian_int(header+0x1c);
223b71206888 Initial import
thib
parents:
diff changeset
419 blocksize = read_little_endian_int(header+0x20);
223b71206888 Initial import
thib
parents:
diff changeset
420 restsize = read_little_endian_int(header+0x24);
223b71206888 Initial import
thib
parents:
diff changeset
421 dummy2 = read_little_endian_int(header+0x28);
223b71206888 Initial import
thib
parents:
diff changeset
422 if (complevel == -1) { /* 無圧縮rawデータ */
223b71206888 Initial import
thib
parents:
diff changeset
423 /* 適当に決め打ちする */
223b71206888 Initial import
thib
parents:
diff changeset
424 blocksize = 65536;
223b71206888 Initial import
thib
parents:
diff changeset
425 restsize = (datasize % (blocksize * (bps/8))) / (bps/8);
223b71206888 Initial import
thib
parents:
diff changeset
426 blocks = datasize / (blocksize * (bps/8)) + (restsize > 0 ? 1 : 0);
223b71206888 Initial import
thib
parents:
diff changeset
427 }
223b71206888 Initial import
thib
parents:
diff changeset
428 if (blocks <= 0 || blocks > 1000000) {
223b71206888 Initial import
thib
parents:
diff changeset
429 /* 1時間を超える曲ってのはないでしょ*/
223b71206888 Initial import
thib
parents:
diff changeset
430 fprintf(stderr,"too large blocks : %d\n",blocks);
223b71206888 Initial import
thib
parents:
diff changeset
431 return;
223b71206888 Initial import
thib
parents:
diff changeset
432 }
223b71206888 Initial import
thib
parents:
diff changeset
433 /* regular file なら filesize 読み込み */
223b71206888 Initial import
thib
parents:
diff changeset
434 if (filesize == 0 && fstat(fileno(in), &sb)==0 && (sb.st_mode&S_IFMT) == S_IFREG) {
223b71206888 Initial import
thib
parents:
diff changeset
435 int pos = ftell(in);
223b71206888 Initial import
thib
parents:
diff changeset
436 fseek(in, 0, 2);
223b71206888 Initial import
thib
parents:
diff changeset
437 filesize = ftell(in);
223b71206888 Initial import
thib
parents:
diff changeset
438 fseek(in, pos, 0);
223b71206888 Initial import
thib
parents:
diff changeset
439 if (pos+blocks*4 >= filesize) {
223b71206888 Initial import
thib
parents:
diff changeset
440 fprintf(stderr,"offset block is not exist\n");
223b71206888 Initial import
thib
parents:
diff changeset
441 return;
223b71206888 Initial import
thib
parents:
diff changeset
442 }
223b71206888 Initial import
thib
parents:
diff changeset
443 }
223b71206888 Initial import
thib
parents:
diff changeset
444 if (complevel == -1) return;
223b71206888 Initial import
thib
parents:
diff changeset
445 /* offset index 読み込み */
223b71206888 Initial import
thib
parents:
diff changeset
446 offsets = new int[blocks];
223b71206888 Initial import
thib
parents:
diff changeset
447 fread(offsets, blocks, 4, in);
223b71206888 Initial import
thib
parents:
diff changeset
448 for (i=0; i<blocks; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
449 offsets[i] = read_little_endian_int((char*)(offsets+i));
223b71206888 Initial import
thib
parents:
diff changeset
450 }
223b71206888 Initial import
thib
parents:
diff changeset
451 if (feof(in) || ferror(in)) {
223b71206888 Initial import
thib
parents:
diff changeset
452 fprintf(stderr,"invalid stream\n");
223b71206888 Initial import
thib
parents:
diff changeset
453 delete[] offsets;
223b71206888 Initial import
thib
parents:
diff changeset
454 offsets = 0;
223b71206888 Initial import
thib
parents:
diff changeset
455 return;
223b71206888 Initial import
thib
parents:
diff changeset
456 }
223b71206888 Initial import
thib
parents:
diff changeset
457 return;
223b71206888 Initial import
thib
parents:
diff changeset
458 }
223b71206888 Initial import
thib
parents:
diff changeset
459 void NWAData::Rewind(FILE* in) {
223b71206888 Initial import
thib
parents:
diff changeset
460 curblock = -1;
223b71206888 Initial import
thib
parents:
diff changeset
461 fseek(in, 0x2c, 0);
223b71206888 Initial import
thib
parents:
diff changeset
462 if (offsets) fseek(in, blocks*4, 1);
223b71206888 Initial import
thib
parents:
diff changeset
463 }
223b71206888 Initial import
thib
parents:
diff changeset
464 int NWAData::CheckHeader(void) {
223b71206888 Initial import
thib
parents:
diff changeset
465 if (complevel != -1 && offsets == 0) return false;
223b71206888 Initial import
thib
parents:
diff changeset
466 /* データそのもののチェック */
223b71206888 Initial import
thib
parents:
diff changeset
467 if (channels != 1 && channels != 2) {
223b71206888 Initial import
thib
parents:
diff changeset
468 fprintf(stderr,"This program only supports mono / stereo data : data have %d channels.\n",channels);
223b71206888 Initial import
thib
parents:
diff changeset
469 return false;
223b71206888 Initial import
thib
parents:
diff changeset
470 }
223b71206888 Initial import
thib
parents:
diff changeset
471 if (bps != 8 && bps != 16) {
223b71206888 Initial import
thib
parents:
diff changeset
472 fprintf(stderr,"This program only supports 8 / 16bit data : data is %d bits\n",bps);
223b71206888 Initial import
thib
parents:
diff changeset
473 return false;
223b71206888 Initial import
thib
parents:
diff changeset
474 }
223b71206888 Initial import
thib
parents:
diff changeset
475 if (complevel == -1) {
223b71206888 Initial import
thib
parents:
diff changeset
476 int byps = bps/8; /* bytes per sample */
223b71206888 Initial import
thib
parents:
diff changeset
477 if (datasize != samplecount*byps) {
223b71206888 Initial import
thib
parents:
diff changeset
478 fprintf(stderr,"invalid datasize : datasize %d != samplecount %d * samplesize %d\n",datasize,samplecount,byps);
223b71206888 Initial import
thib
parents:
diff changeset
479 return false;
223b71206888 Initial import
thib
parents:
diff changeset
480 }
223b71206888 Initial import
thib
parents:
diff changeset
481 if (samplecount != (blocks-1)*blocksize+restsize ) {
223b71206888 Initial import
thib
parents:
diff changeset
482 fprintf(stderr,"total sample count is invalid : samplecount %d != %d*%d+%d(block*blocksize+lastblocksize).\n",samplecount,blocks-1,blocksize,restsize);
223b71206888 Initial import
thib
parents:
diff changeset
483 return false;
223b71206888 Initial import
thib
parents:
diff changeset
484 }
223b71206888 Initial import
thib
parents:
diff changeset
485 else
223b71206888 Initial import
thib
parents:
diff changeset
486 return true;
223b71206888 Initial import
thib
parents:
diff changeset
487 }
223b71206888 Initial import
thib
parents:
diff changeset
488 //if (complevel < 0 || complevel > 2) {
223b71206888 Initial import
thib
parents:
diff changeset
489 if (complevel < 0 || complevel > 5) {
223b71206888 Initial import
thib
parents:
diff changeset
490 fprintf(stderr,"This program only supports -1,0,1,2 compression level : the level of data is %d\n",complevel);
223b71206888 Initial import
thib
parents:
diff changeset
491 return false;
223b71206888 Initial import
thib
parents:
diff changeset
492 }
223b71206888 Initial import
thib
parents:
diff changeset
493 /* 整合性チェック */
223b71206888 Initial import
thib
parents:
diff changeset
494 if (filesize != 0 && filesize != compdatasize) {
223b71206888 Initial import
thib
parents:
diff changeset
495 fprintf(stderr,"file size is invalid : %d != %d\n",filesize,compdatasize);
223b71206888 Initial import
thib
parents:
diff changeset
496 return false;
223b71206888 Initial import
thib
parents:
diff changeset
497 }
223b71206888 Initial import
thib
parents:
diff changeset
498 if (offsets[blocks-1] >= compdatasize) {
223b71206888 Initial import
thib
parents:
diff changeset
499 fprintf(stderr,"the last offset overruns the file.\n");
223b71206888 Initial import
thib
parents:
diff changeset
500 return false;
223b71206888 Initial import
thib
parents:
diff changeset
501 }
223b71206888 Initial import
thib
parents:
diff changeset
502 int byps = bps/8; /* bytes per sample */
223b71206888 Initial import
thib
parents:
diff changeset
503 if (datasize != samplecount*byps) {
223b71206888 Initial import
thib
parents:
diff changeset
504 fprintf(stderr,"invalid datasize : datasize %d != samplecount %d * samplesize %d\n",datasize,samplecount,byps);
223b71206888 Initial import
thib
parents:
diff changeset
505 return false;
223b71206888 Initial import
thib
parents:
diff changeset
506 }
223b71206888 Initial import
thib
parents:
diff changeset
507 if (samplecount != (blocks-1)*blocksize+restsize ) {
223b71206888 Initial import
thib
parents:
diff changeset
508 fprintf(stderr,"total sample count is invalid : samplecount %d != %d*%d+%d(block*blocksize+lastblocksize).\n",samplecount,blocks-1,blocksize,restsize);
223b71206888 Initial import
thib
parents:
diff changeset
509 return false;
223b71206888 Initial import
thib
parents:
diff changeset
510 }
223b71206888 Initial import
thib
parents:
diff changeset
511 tmpdata = new char[blocksize*byps*2]; /* これ以上の大きさはないだろう、、、 */
223b71206888 Initial import
thib
parents:
diff changeset
512 return true;
223b71206888 Initial import
thib
parents:
diff changeset
513 }
223b71206888 Initial import
thib
parents:
diff changeset
514
223b71206888 Initial import
thib
parents:
diff changeset
515 class NWAInfo_sw2 {
223b71206888 Initial import
thib
parents:
diff changeset
516 public:
223b71206888 Initial import
thib
parents:
diff changeset
517 int Channels(void) const{return 2;}
223b71206888 Initial import
thib
parents:
diff changeset
518 int Bps(void) const { return 16;}
223b71206888 Initial import
thib
parents:
diff changeset
519 int CompLevel(void) const { return 2;}
223b71206888 Initial import
thib
parents:
diff changeset
520 int UseRunLength(void) const { return false; }
223b71206888 Initial import
thib
parents:
diff changeset
521 };
223b71206888 Initial import
thib
parents:
diff changeset
522 int NWAData::Decode(FILE* in, char* data, int& skip_count) {
223b71206888 Initial import
thib
parents:
diff changeset
523 if (complevel == -1) { /* 無圧縮時の処理 */
223b71206888 Initial import
thib
parents:
diff changeset
524 if (feof(in) || ferror(in)) return -1;
223b71206888 Initial import
thib
parents:
diff changeset
525 if (curblock == -1) {
223b71206888 Initial import
thib
parents:
diff changeset
526 /* 最初のブロックなら、wave header 出力 */
223b71206888 Initial import
thib
parents:
diff changeset
527 memcpy(data, make_wavheader(datasize, channels, bps, freq), 0x2c);
223b71206888 Initial import
thib
parents:
diff changeset
528 curblock++;
223b71206888 Initial import
thib
parents:
diff changeset
529 fseek(in, offset_start + 0x2c, SEEK_SET);
223b71206888 Initial import
thib
parents:
diff changeset
530 return 0x2c;
223b71206888 Initial import
thib
parents:
diff changeset
531 }
223b71206888 Initial import
thib
parents:
diff changeset
532 if (skip_count > blocksize/channels) {
223b71206888 Initial import
thib
parents:
diff changeset
533 skip_count -= blocksize/channels;
223b71206888 Initial import
thib
parents:
diff changeset
534 fseek(in, blocksize*(bps/8), SEEK_CUR);
223b71206888 Initial import
thib
parents:
diff changeset
535 curblock++;
223b71206888 Initial import
thib
parents:
diff changeset
536 return -2;
223b71206888 Initial import
thib
parents:
diff changeset
537 }
223b71206888 Initial import
thib
parents:
diff changeset
538 if (curblock < blocks) {
223b71206888 Initial import
thib
parents:
diff changeset
539 int readsize = blocksize;
223b71206888 Initial import
thib
parents:
diff changeset
540 if (skip_count) {
223b71206888 Initial import
thib
parents:
diff changeset
541 fseek(in, skip_count*channels*(bps/8), SEEK_CUR);
223b71206888 Initial import
thib
parents:
diff changeset
542 readsize -= skip_count * channels;
223b71206888 Initial import
thib
parents:
diff changeset
543 skip_count = 0;
223b71206888 Initial import
thib
parents:
diff changeset
544 }
223b71206888 Initial import
thib
parents:
diff changeset
545 int err = fread(data, 1, readsize * (bps/8), in);
223b71206888 Initial import
thib
parents:
diff changeset
546 curblock++;
223b71206888 Initial import
thib
parents:
diff changeset
547 return err;
223b71206888 Initial import
thib
parents:
diff changeset
548 }
223b71206888 Initial import
thib
parents:
diff changeset
549 return -1;
223b71206888 Initial import
thib
parents:
diff changeset
550 }
223b71206888 Initial import
thib
parents:
diff changeset
551 if (offsets == 0 || tmpdata == 0) return -1;
223b71206888 Initial import
thib
parents:
diff changeset
552 if (blocks == curblock) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
553 if (feof(in) || ferror(in)) return -1;
223b71206888 Initial import
thib
parents:
diff changeset
554 if (curblock == -1) {
223b71206888 Initial import
thib
parents:
diff changeset
555 /* 最初のブロックなら、wave header 出力 */
223b71206888 Initial import
thib
parents:
diff changeset
556 memcpy(data, make_wavheader(datasize, channels, bps, freq), 0x2c);
223b71206888 Initial import
thib
parents:
diff changeset
557 curblock++;
223b71206888 Initial import
thib
parents:
diff changeset
558 return 0x2c;
223b71206888 Initial import
thib
parents:
diff changeset
559 }
223b71206888 Initial import
thib
parents:
diff changeset
560 /* 今回読み込む/デコードするデータの大きさを得る */
223b71206888 Initial import
thib
parents:
diff changeset
561 int curblocksize, curcompsize;
223b71206888 Initial import
thib
parents:
diff changeset
562 if (curblock != blocks-1) {
223b71206888 Initial import
thib
parents:
diff changeset
563 curblocksize = blocksize * (bps/8);
223b71206888 Initial import
thib
parents:
diff changeset
564 curcompsize = offsets[curblock+1] - offsets[curblock];
223b71206888 Initial import
thib
parents:
diff changeset
565 if (curblocksize >= blocksize*(bps/8)*2) return -1; // Fatal error
223b71206888 Initial import
thib
parents:
diff changeset
566 } else {
223b71206888 Initial import
thib
parents:
diff changeset
567 curblocksize = restsize * (bps/8);
223b71206888 Initial import
thib
parents:
diff changeset
568 curcompsize = blocksize*(bps/8)*2;
223b71206888 Initial import
thib
parents:
diff changeset
569 }
223b71206888 Initial import
thib
parents:
diff changeset
570 if (skip_count > blocksize/channels) {
223b71206888 Initial import
thib
parents:
diff changeset
571 skip_count -= blocksize/channels;
223b71206888 Initial import
thib
parents:
diff changeset
572 fseek(in, curcompsize, SEEK_CUR);
223b71206888 Initial import
thib
parents:
diff changeset
573 curblock++;
223b71206888 Initial import
thib
parents:
diff changeset
574 return -2;
223b71206888 Initial import
thib
parents:
diff changeset
575 }
223b71206888 Initial import
thib
parents:
diff changeset
576 /* データ読み込み */
223b71206888 Initial import
thib
parents:
diff changeset
577 fread(tmpdata, 1, curcompsize, in);
223b71206888 Initial import
thib
parents:
diff changeset
578 /* 展開 */
223b71206888 Initial import
thib
parents:
diff changeset
579 if (channels == 2 && bps == 16 && complevel == 2) {
223b71206888 Initial import
thib
parents:
diff changeset
580 NWAInfo_sw2 info;
223b71206888 Initial import
thib
parents:
diff changeset
581 NWADecode(info, tmpdata, data, curcompsize, curblocksize);
223b71206888 Initial import
thib
parents:
diff changeset
582 } else {
223b71206888 Initial import
thib
parents:
diff changeset
583 NWAInfo info(channels, bps, complevel);
223b71206888 Initial import
thib
parents:
diff changeset
584 NWADecode(info, tmpdata, data, curcompsize, curblocksize);
223b71206888 Initial import
thib
parents:
diff changeset
585 }
223b71206888 Initial import
thib
parents:
diff changeset
586 int retsize = curblocksize;
223b71206888 Initial import
thib
parents:
diff changeset
587 if (skip_count) {
223b71206888 Initial import
thib
parents:
diff changeset
588 int skip_c = skip_count * channels * (bps/8);
223b71206888 Initial import
thib
parents:
diff changeset
589 retsize -= skip_c;
223b71206888 Initial import
thib
parents:
diff changeset
590 memmove(data, data+skip_c, skip_c);
223b71206888 Initial import
thib
parents:
diff changeset
591 skip_count = 0;
223b71206888 Initial import
thib
parents:
diff changeset
592 }
223b71206888 Initial import
thib
parents:
diff changeset
593 curblock++;
223b71206888 Initial import
thib
parents:
diff changeset
594 return retsize;
223b71206888 Initial import
thib
parents:
diff changeset
595 }
223b71206888 Initial import
thib
parents:
diff changeset
596
223b71206888 Initial import
thib
parents:
diff changeset
597 #ifdef USE_MAIN
223b71206888 Initial import
thib
parents:
diff changeset
598
223b71206888 Initial import
thib
parents:
diff changeset
599 void conv(FILE* in, FILE* out, int skip_count, int in_size = -1) {
223b71206888 Initial import
thib
parents:
diff changeset
600 NWAData h;
223b71206888 Initial import
thib
parents:
diff changeset
601 h.ReadHeader(in, in_size);
223b71206888 Initial import
thib
parents:
diff changeset
602 h.CheckHeader();
223b71206888 Initial import
thib
parents:
diff changeset
603 int bs = h.BlockLength();
223b71206888 Initial import
thib
parents:
diff changeset
604 char* d = new char[bs];
223b71206888 Initial import
thib
parents:
diff changeset
605 int err;
223b71206888 Initial import
thib
parents:
diff changeset
606 while( (err=h.Decode(in, d, skip_count)) != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
607 if (err == -1) break;
223b71206888 Initial import
thib
parents:
diff changeset
608 if (err == -2) continue;
223b71206888 Initial import
thib
parents:
diff changeset
609 fwrite(d, err, 1, out);
223b71206888 Initial import
thib
parents:
diff changeset
610 }
223b71206888 Initial import
thib
parents:
diff changeset
611 return;
223b71206888 Initial import
thib
parents:
diff changeset
612 }
223b71206888 Initial import
thib
parents:
diff changeset
613 int main(int argc, char** argv) {
223b71206888 Initial import
thib
parents:
diff changeset
614 int skip_count = 0;
223b71206888 Initial import
thib
parents:
diff changeset
615
223b71206888 Initial import
thib
parents:
diff changeset
616 if (argc > 2 && strcmp(argv[1], "--skip") == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
617 skip_count = atoi(argv[2]);
223b71206888 Initial import
thib
parents:
diff changeset
618 argc -= 2;
223b71206888 Initial import
thib
parents:
diff changeset
619 argv[1] = argv[3];
223b71206888 Initial import
thib
parents:
diff changeset
620 argv[2] = argv[4];
223b71206888 Initial import
thib
parents:
diff changeset
621 }
223b71206888 Initial import
thib
parents:
diff changeset
622 if (argc != 2 && argc != 3) {
223b71206888 Initial import
thib
parents:
diff changeset
623 fprintf(stderr,"usage : nwatowav [inputfile [outputfile]]\n");
223b71206888 Initial import
thib
parents:
diff changeset
624 return -1;
223b71206888 Initial import
thib
parents:
diff changeset
625 }
223b71206888 Initial import
thib
parents:
diff changeset
626 if (strstr(argv[1], ".nwk") != 0 || strstr(argv[1], ".ovk") != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
627 bool is_ovk;
223b71206888 Initial import
thib
parents:
diff changeset
628 int headblk_sz;
223b71206888 Initial import
thib
parents:
diff changeset
629 char* out_ext;
223b71206888 Initial import
thib
parents:
diff changeset
630
223b71206888 Initial import
thib
parents:
diff changeset
631 char* outpath = new char[strlen(argv[1])+10];
223b71206888 Initial import
thib
parents:
diff changeset
632 char buf[1024];
223b71206888 Initial import
thib
parents:
diff changeset
633 memset(buf, 0, 1024);
223b71206888 Initial import
thib
parents:
diff changeset
634 FILE* in = fopen(argv[1], "rb");
223b71206888 Initial import
thib
parents:
diff changeset
635 if (in == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
636 fprintf(stderr,"Cannot open file : %s\n",argv[1]);
223b71206888 Initial import
thib
parents:
diff changeset
637 return -1;
223b71206888 Initial import
thib
parents:
diff changeset
638 }
223b71206888 Initial import
thib
parents:
diff changeset
639 if (strstr(argv[1], ".ovk") != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
640 is_ovk = true;
223b71206888 Initial import
thib
parents:
diff changeset
641 headblk_sz = 16;
223b71206888 Initial import
thib
parents:
diff changeset
642 out_ext = "ogg";
223b71206888 Initial import
thib
parents:
diff changeset
643 } else {
223b71206888 Initial import
thib
parents:
diff changeset
644 is_ovk = false;
223b71206888 Initial import
thib
parents:
diff changeset
645 headblk_sz = 12;
223b71206888 Initial import
thib
parents:
diff changeset
646 out_ext = "wav";
223b71206888 Initial import
thib
parents:
diff changeset
647 }
223b71206888 Initial import
thib
parents:
diff changeset
648 fread(buf, 1, 4, in);
223b71206888 Initial import
thib
parents:
diff changeset
649 int index = read_little_endian_int(buf);
223b71206888 Initial import
thib
parents:
diff changeset
650 if (index <= 0) {
223b71206888 Initial import
thib
parents:
diff changeset
651 if (is_ovk)
223b71206888 Initial import
thib
parents:
diff changeset
652 fprintf(stderr,"Invalid Ogg-ovk file : %s : index = %d\n",argv[1],index);
223b71206888 Initial import
thib
parents:
diff changeset
653 else
223b71206888 Initial import
thib
parents:
diff changeset
654 fprintf(stderr,"Invalid Koe-nwk file : %s : index = %d\n",argv[1],index);
223b71206888 Initial import
thib
parents:
diff changeset
655 return -1;
223b71206888 Initial import
thib
parents:
diff changeset
656 }
223b71206888 Initial import
thib
parents:
diff changeset
657 int* tbl_off = new int[index];
223b71206888 Initial import
thib
parents:
diff changeset
658 int* tbl_siz = new int[index];
223b71206888 Initial import
thib
parents:
diff changeset
659 int* tbl_cnt = new int[index];
223b71206888 Initial import
thib
parents:
diff changeset
660 int* tbl_origsiz = new int[index];
223b71206888 Initial import
thib
parents:
diff changeset
661 int i;
223b71206888 Initial import
thib
parents:
diff changeset
662 for (i=0; i<index; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
663 fread(buf, 1, headblk_sz, in);
223b71206888 Initial import
thib
parents:
diff changeset
664 tbl_siz[i] = read_little_endian_int(buf);
223b71206888 Initial import
thib
parents:
diff changeset
665 tbl_off[i] = read_little_endian_int(buf+4);
223b71206888 Initial import
thib
parents:
diff changeset
666 tbl_cnt[i] = read_little_endian_int(buf+8);
223b71206888 Initial import
thib
parents:
diff changeset
667 tbl_origsiz[i] = read_little_endian_int(buf+12);
223b71206888 Initial import
thib
parents:
diff changeset
668 }
223b71206888 Initial import
thib
parents:
diff changeset
669 fseek(in, 0, 2);
223b71206888 Initial import
thib
parents:
diff changeset
670 int fsize = ftell(in);
223b71206888 Initial import
thib
parents:
diff changeset
671 for (i=0; i<index; i++) {
223b71206888 Initial import
thib
parents:
diff changeset
672 if (tbl_off[i] <= 0 || tbl_siz[i] <= 0 || tbl_off[i]+tbl_siz[i] > fsize) {
223b71206888 Initial import
thib
parents:
diff changeset
673 fprintf(stderr,"Invalid table[%d] : cnt %d off %d size %d / %d\n",i,tbl_cnt[i],tbl_off[i],tbl_siz[i],fsize);
223b71206888 Initial import
thib
parents:
diff changeset
674 continue;
223b71206888 Initial import
thib
parents:
diff changeset
675 }
223b71206888 Initial import
thib
parents:
diff changeset
676 if (argc == 2)
223b71206888 Initial import
thib
parents:
diff changeset
677 sprintf(outpath, "%s-%d.%s", argv[1], tbl_cnt[i],out_ext);
223b71206888 Initial import
thib
parents:
diff changeset
678 else
223b71206888 Initial import
thib
parents:
diff changeset
679 sprintf(outpath, "%s-%d.%s", argv[2], tbl_cnt[i],out_ext);
223b71206888 Initial import
thib
parents:
diff changeset
680 FILE* out = fopen(outpath, "wb");
223b71206888 Initial import
thib
parents:
diff changeset
681 if (out == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
682 fprintf(stderr,"Cannot open output file %s\n",outpath);
223b71206888 Initial import
thib
parents:
diff changeset
683 continue;
223b71206888 Initial import
thib
parents:
diff changeset
684 }
223b71206888 Initial import
thib
parents:
diff changeset
685 fprintf(stderr,"Writing file %s...\n",outpath);
223b71206888 Initial import
thib
parents:
diff changeset
686 fseek(in, tbl_off[i], 0);
223b71206888 Initial import
thib
parents:
diff changeset
687 if (is_ovk) { // copy file
223b71206888 Initial import
thib
parents:
diff changeset
688 int sz = tbl_siz[i];
223b71206888 Initial import
thib
parents:
diff changeset
689 char buf[32*1024];
223b71206888 Initial import
thib
parents:
diff changeset
690 while(sz > 32*1024) {
223b71206888 Initial import
thib
parents:
diff changeset
691 fread(buf, 32*1024, 1, in);
223b71206888 Initial import
thib
parents:
diff changeset
692 fwrite(buf, 32*1024, 1, out);
223b71206888 Initial import
thib
parents:
diff changeset
693 sz -= 1024*32;
223b71206888 Initial import
thib
parents:
diff changeset
694 }
223b71206888 Initial import
thib
parents:
diff changeset
695 if (sz > 0) {
223b71206888 Initial import
thib
parents:
diff changeset
696 fread(buf, sz, 1, in);
223b71206888 Initial import
thib
parents:
diff changeset
697 fwrite(buf, sz, 1, out);
223b71206888 Initial import
thib
parents:
diff changeset
698 }
223b71206888 Initial import
thib
parents:
diff changeset
699 } else { // .nwk
223b71206888 Initial import
thib
parents:
diff changeset
700 conv(in, out, 0, tbl_siz[i]);
223b71206888 Initial import
thib
parents:
diff changeset
701 }
223b71206888 Initial import
thib
parents:
diff changeset
702 fclose(out);
223b71206888 Initial import
thib
parents:
diff changeset
703 }
223b71206888 Initial import
thib
parents:
diff changeset
704 fclose(in);
223b71206888 Initial import
thib
parents:
diff changeset
705 return 0;
223b71206888 Initial import
thib
parents:
diff changeset
706 }
223b71206888 Initial import
thib
parents:
diff changeset
707 FILE* in = fopen(argv[1],"rb");
223b71206888 Initial import
thib
parents:
diff changeset
708 if (in == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
709 fprintf(stderr,"Cannot open file : %s\n",argv[1]);
223b71206888 Initial import
thib
parents:
diff changeset
710 return -1;
223b71206888 Initial import
thib
parents:
diff changeset
711 }
223b71206888 Initial import
thib
parents:
diff changeset
712 FILE* out;
223b71206888 Initial import
thib
parents:
diff changeset
713 if (argc != 3 && (!isatty(fileno(stdout)))) { // wave file is written to stdout if stdout is redirected to a file
223b71206888 Initial import
thib
parents:
diff changeset
714 out = stdout;
223b71206888 Initial import
thib
parents:
diff changeset
715 } else { // make a new file or use argv[2] for output file name
223b71206888 Initial import
thib
parents:
diff changeset
716 char* outpath = new char[strlen(argv[1])+10];
223b71206888 Initial import
thib
parents:
diff changeset
717 sprintf(outpath, "%s.wav",argv[1]);
223b71206888 Initial import
thib
parents:
diff changeset
718 if (argc == 3) outpath = argv[2];
223b71206888 Initial import
thib
parents:
diff changeset
719 out = fopen(outpath, "wb");
223b71206888 Initial import
thib
parents:
diff changeset
720 if (out == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
721 fprintf(stderr,"Cannot open file : %s\n",outpath);
223b71206888 Initial import
thib
parents:
diff changeset
722 return -1;
223b71206888 Initial import
thib
parents:
diff changeset
723 }
223b71206888 Initial import
thib
parents:
diff changeset
724 }
223b71206888 Initial import
thib
parents:
diff changeset
725 conv(in, out, skip_count);
223b71206888 Initial import
thib
parents:
diff changeset
726 fclose(in);
223b71206888 Initial import
thib
parents:
diff changeset
727 if (out != stdout) fclose(out);
223b71206888 Initial import
thib
parents:
diff changeset
728 return 0;
223b71206888 Initial import
thib
parents:
diff changeset
729 }
223b71206888 Initial import
thib
parents:
diff changeset
730 #else
223b71206888 Initial import
thib
parents:
diff changeset
731
223b71206888 Initial import
thib
parents:
diff changeset
732 #include"wavfile.h"
223b71206888 Initial import
thib
parents:
diff changeset
733
223b71206888 Initial import
thib
parents:
diff changeset
734 void NWAFILE::Seek(int count) {
223b71206888 Initial import
thib
parents:
diff changeset
735 if (data == 0) data = new char[block_size];
223b71206888 Initial import
thib
parents:
diff changeset
736 nwa->Rewind(stream);
223b71206888 Initial import
thib
parents:
diff changeset
737 int dmy = 0;
223b71206888 Initial import
thib
parents:
diff changeset
738 nwa->Decode(stream, data, dmy); // skip wav header
223b71206888 Initial import
thib
parents:
diff changeset
739 data_len = 0;
223b71206888 Initial import
thib
parents:
diff changeset
740 skip_count = count;
223b71206888 Initial import
thib
parents:
diff changeset
741 }
223b71206888 Initial import
thib
parents:
diff changeset
742 NWAFILE::NWAFILE(FILE* _stream) {
223b71206888 Initial import
thib
parents:
diff changeset
743 skip_count = 0;
223b71206888 Initial import
thib
parents:
diff changeset
744 data = 0;
223b71206888 Initial import
thib
parents:
diff changeset
745 stream = _stream;
223b71206888 Initial import
thib
parents:
diff changeset
746 nwa = new NWAData;
223b71206888 Initial import
thib
parents:
diff changeset
747 nwa->ReadHeader(stream);
223b71206888 Initial import
thib
parents:
diff changeset
748 if (!nwa->CheckHeader()) {
223b71206888 Initial import
thib
parents:
diff changeset
749 return;
223b71206888 Initial import
thib
parents:
diff changeset
750 }
223b71206888 Initial import
thib
parents:
diff changeset
751 block_size = nwa->BlockLength();
223b71206888 Initial import
thib
parents:
diff changeset
752 data = new char[block_size];
223b71206888 Initial import
thib
parents:
diff changeset
753 data_len = 0;
223b71206888 Initial import
thib
parents:
diff changeset
754
223b71206888 Initial import
thib
parents:
diff changeset
755 wavinfo.SamplingRate = nwa->freq;
223b71206888 Initial import
thib
parents:
diff changeset
756 wavinfo.Channels = nwa->channels;
223b71206888 Initial import
thib
parents:
diff changeset
757 wavinfo.DataBits = nwa->bps;
223b71206888 Initial import
thib
parents:
diff changeset
758
223b71206888 Initial import
thib
parents:
diff changeset
759 int dmy = 0;
223b71206888 Initial import
thib
parents:
diff changeset
760 data_len = nwa->Decode(stream, data, dmy); // skip wav header
223b71206888 Initial import
thib
parents:
diff changeset
761
223b71206888 Initial import
thib
parents:
diff changeset
762 return;
223b71206888 Initial import
thib
parents:
diff changeset
763 }
223b71206888 Initial import
thib
parents:
diff changeset
764 NWAFILE::~NWAFILE() {
223b71206888 Initial import
thib
parents:
diff changeset
765 if (stream) fclose(stream);
223b71206888 Initial import
thib
parents:
diff changeset
766 if (data) delete[] data;
223b71206888 Initial import
thib
parents:
diff changeset
767 if (nwa) delete nwa;
223b71206888 Initial import
thib
parents:
diff changeset
768 }
223b71206888 Initial import
thib
parents:
diff changeset
769 int NWAFILE::Read(char* buf, int blksize, int blklen) {
223b71206888 Initial import
thib
parents:
diff changeset
770 if (data == 0) return -1; // end of file
223b71206888 Initial import
thib
parents:
diff changeset
771
223b71206888 Initial import
thib
parents:
diff changeset
772 if (data_len > blksize * blklen) {
223b71206888 Initial import
thib
parents:
diff changeset
773 int len = blksize * blklen;
223b71206888 Initial import
thib
parents:
diff changeset
774 memcpy(buf, data, len);
223b71206888 Initial import
thib
parents:
diff changeset
775 memmove(data, data+len, data_len-len);
223b71206888 Initial import
thib
parents:
diff changeset
776 data_len -= len;
223b71206888 Initial import
thib
parents:
diff changeset
777 return blklen;
223b71206888 Initial import
thib
parents:
diff changeset
778 }
223b71206888 Initial import
thib
parents:
diff changeset
779 memcpy(buf, data, data_len);
223b71206888 Initial import
thib
parents:
diff changeset
780 int copied_length = data_len;
223b71206888 Initial import
thib
parents:
diff changeset
781 data_len = 0;
223b71206888 Initial import
thib
parents:
diff changeset
782
223b71206888 Initial import
thib
parents:
diff changeset
783 if (stream == 0) {
223b71206888 Initial import
thib
parents:
diff changeset
784 delete[] data;
223b71206888 Initial import
thib
parents:
diff changeset
785 data = 0;
223b71206888 Initial import
thib
parents:
diff changeset
786 return copied_length / blksize;
223b71206888 Initial import
thib
parents:
diff changeset
787 }
223b71206888 Initial import
thib
parents:
diff changeset
788
223b71206888 Initial import
thib
parents:
diff changeset
789 // read
223b71206888 Initial import
thib
parents:
diff changeset
790 do {
223b71206888 Initial import
thib
parents:
diff changeset
791 int err;
223b71206888 Initial import
thib
parents:
diff changeset
792 retry:
223b71206888 Initial import
thib
parents:
diff changeset
793 err = nwa->Decode(stream, data, skip_count);
223b71206888 Initial import
thib
parents:
diff changeset
794 if (err == 0 || err == -1) { // eof or error
223b71206888 Initial import
thib
parents:
diff changeset
795 delete[] data;
223b71206888 Initial import
thib
parents:
diff changeset
796 data = 0;
223b71206888 Initial import
thib
parents:
diff changeset
797 return copied_length / blksize;
223b71206888 Initial import
thib
parents:
diff changeset
798 }
223b71206888 Initial import
thib
parents:
diff changeset
799 if (err == -2) goto retry; // EAGAIN
223b71206888 Initial import
thib
parents:
diff changeset
800 data_len = err;
223b71206888 Initial import
thib
parents:
diff changeset
801 if (copied_length + data_len < blklen*blksize) {
223b71206888 Initial import
thib
parents:
diff changeset
802 memcpy(buf+copied_length, data, data_len);
223b71206888 Initial import
thib
parents:
diff changeset
803 copied_length += data_len;
223b71206888 Initial import
thib
parents:
diff changeset
804 goto retry;
223b71206888 Initial import
thib
parents:
diff changeset
805 }
223b71206888 Initial import
thib
parents:
diff changeset
806 } while(0);
223b71206888 Initial import
thib
parents:
diff changeset
807
223b71206888 Initial import
thib
parents:
diff changeset
808 // determine return length
223b71206888 Initial import
thib
parents:
diff changeset
809 int datablks = (data_len+copied_length)/blksize;
223b71206888 Initial import
thib
parents:
diff changeset
810 if (datablks <= 0) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
811 if (datablks > blklen) datablks = blklen;
223b71206888 Initial import
thib
parents:
diff changeset
812 int rest_len = datablks * blksize - copied_length;
223b71206888 Initial import
thib
parents:
diff changeset
813 if (rest_len) {
223b71206888 Initial import
thib
parents:
diff changeset
814 memcpy(buf+copied_length, data, rest_len);
223b71206888 Initial import
thib
parents:
diff changeset
815 memmove(data, data+rest_len, data_len-rest_len);
223b71206888 Initial import
thib
parents:
diff changeset
816 data_len -= rest_len;
223b71206888 Initial import
thib
parents:
diff changeset
817 }
223b71206888 Initial import
thib
parents:
diff changeset
818 return datablks;
223b71206888 Initial import
thib
parents:
diff changeset
819 }
223b71206888 Initial import
thib
parents:
diff changeset
820
223b71206888 Initial import
thib
parents:
diff changeset
821 char* NWAFILE::ReadAll(FILE* in, int& total_size) {
223b71206888 Initial import
thib
parents:
diff changeset
822 NWAData h;
223b71206888 Initial import
thib
parents:
diff changeset
823 if (in == 0) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
824 h.ReadHeader(in);
223b71206888 Initial import
thib
parents:
diff changeset
825 h.CheckHeader();
223b71206888 Initial import
thib
parents:
diff changeset
826 int bs = h.BlockLength();
223b71206888 Initial import
thib
parents:
diff changeset
827 total_size = h.datasize+0x2c;
223b71206888 Initial import
thib
parents:
diff changeset
828 char* d = new char[total_size + bs*2];
223b71206888 Initial import
thib
parents:
diff changeset
829 int dcur = 0;
223b71206888 Initial import
thib
parents:
diff changeset
830 int err;
223b71206888 Initial import
thib
parents:
diff changeset
831 int skip = 0;
223b71206888 Initial import
thib
parents:
diff changeset
832 while(dcur < total_size+bs && (err=h.Decode(in, d+dcur, skip)) != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
833 if (err == -1) break;
223b71206888 Initial import
thib
parents:
diff changeset
834 if (err == -2) continue;
223b71206888 Initial import
thib
parents:
diff changeset
835 dcur += err;
223b71206888 Initial import
thib
parents:
diff changeset
836 }
223b71206888 Initial import
thib
parents:
diff changeset
837 return d;
223b71206888 Initial import
thib
parents:
diff changeset
838 }
223b71206888 Initial import
thib
parents:
diff changeset
839
223b71206888 Initial import
thib
parents:
diff changeset
840 #include"music.h"
223b71206888 Initial import
thib
parents:
diff changeset
841
223b71206888 Initial import
thib
parents:
diff changeset
842 char* decode_koe_nwa(AvgKoeInfo info, int* data_len) {
223b71206888 Initial import
thib
parents:
diff changeset
843 NWAData h;
223b71206888 Initial import
thib
parents:
diff changeset
844 if (info.stream == 0) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
845 fseek(info.stream, info.offset, 0);
223b71206888 Initial import
thib
parents:
diff changeset
846 h.ReadHeader(info.stream, info.length);
223b71206888 Initial import
thib
parents:
diff changeset
847 if (h.CheckHeader() == false) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
848 int bs = h.BlockLength();
223b71206888 Initial import
thib
parents:
diff changeset
849 int total = h.datasize + 0x2c;
223b71206888 Initial import
thib
parents:
diff changeset
850 char* d = new char[total + bs*2];
223b71206888 Initial import
thib
parents:
diff changeset
851 int dcur = 0;
223b71206888 Initial import
thib
parents:
diff changeset
852 int err;
223b71206888 Initial import
thib
parents:
diff changeset
853 int skip = 0;
223b71206888 Initial import
thib
parents:
diff changeset
854 while(dcur < total+bs && (err=h.Decode(info.stream, d+dcur, skip)) != 0) {
223b71206888 Initial import
thib
parents:
diff changeset
855 if (err == -1) break;
223b71206888 Initial import
thib
parents:
diff changeset
856 if (err == -2) continue;
223b71206888 Initial import
thib
parents:
diff changeset
857 dcur += err;
223b71206888 Initial import
thib
parents:
diff changeset
858 }
223b71206888 Initial import
thib
parents:
diff changeset
859 if (data_len) {
223b71206888 Initial import
thib
parents:
diff changeset
860 *data_len = dcur;
223b71206888 Initial import
thib
parents:
diff changeset
861 if (*data_len > total) *data_len = total;
223b71206888 Initial import
thib
parents:
diff changeset
862 }
223b71206888 Initial import
thib
parents:
diff changeset
863 return d;
223b71206888 Initial import
thib
parents:
diff changeset
864 }
223b71206888 Initial import
thib
parents:
diff changeset
865
223b71206888 Initial import
thib
parents:
diff changeset
866 #endif