annotate music2/nwatowav.cc @ 66:d112357a0ec1

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