1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     snd_Bcwav.h
4 
5   Copyright (C)2009 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 25790 $
14  *---------------------------------------------------------------------------*/
15 #ifndef NN_SND_BCWAV_H_
16 #define NN_SND_BCWAV_H_
17 
18 #include <nn/snd/CTR/Common/snd_Adpcm.h>
19 
20 /*! @file
21     @brief      Bcwav 構造体に関する定義
22 */
23 
24 #ifdef __cplusplus
25 
26 namespace nn { namespace snd { namespace CTR {
27 
28 /*!
29     @brief      bcwav フォーマットのファイルを扱うクラスの定義です。
30  */
31 class Bcwav {
32 private:
Bcwav()33     Bcwav() {}
~Bcwav()34     ~Bcwav() {}
35 
36 public:
37     // --- 基本型 ---
38     struct Reference
39     {
40         u16 typeId;     // ユーザーには非公開
41         u16 padding;
42         u32 offset;
43     };
44 
45     struct ReferenceWithSize : public Reference
46     {
47         u32 size;
48     };
49 
50     template< typename ITEM_TYPE >
51     struct Table
52     {
53         u32 count;
54         ITEM_TYPE item[1];
55     };
56 
57     // --- ファイルデータ構造 ---
58     struct FileHeader
59     {
60         u32 signature;          // 必ず 'CWAV'
61         u16 byteOrderMark;
62         u16 headerSize;
63         u32 version;
64         u32 fileSize;
65         u16 dataBlocks;
66         u16 reserved;
67     };
68 
69     struct BlockInfo
70     {
71         ReferenceWithSize infoBlockReference;
72         ReferenceWithSize dataBlockReference;
73     };
74 
75     struct FileInfo
76     {
77         FileHeader header;
78         BlockInfo  blockInfo;
79     };
80 
81     // --- ブロックデータ構造 ---
82     struct BlockHeader
83     {
84         u32 kind;       // 'INFO' や 'DATA'
85         u32 size;
86     };
87 
88     /*!
89         @brief      波形に関する情報
90      */
91     struct WaveInfo
92     {
93         u8  encoding;           //!< エンコードフォーマット (@ref nn::snd::Bcwav::Encoding)
94         u8  isLoop;             //!< ループの指定 (0: noloop, 1: loop)
95         u16 padding;
96         u32 sampleRate;         //!< サンプルレート
97         u32 loopStartFrame;     //!< ループ開始位置
98         u32 loopEndFrame;       //!< ループ終了位置
99     };
100 
101     struct InfoBlockBody
102     {
103         WaveInfo waveInfo;
104         u32 reserved;
105         Table<Reference> channelInfoReferenceTable;
106     };
107 
108     struct ChannelInfo
109     {
110         Reference toSamples;    // 起点はデータブロックボディ (BlockHeader 以降)
111         Reference toAdpcmInfo;
112         u32 reserved;
113     };
114 
115     /*!
116         @brief      DspAdpcm 情報
117      */
118     struct DspAdpcmInfo
119     {
120         AdpcmParam     param;          //!< Adpcm パラメータ
121         AdpcmContext   context;        //!< 初回再生時コンテキスト情報
122         AdpcmContext   loopContext;    //!< ループ再生時コンテキスト情報
123     };
124 
125     /*!
126         :private
127         @brief      ImaAdpcm コンテキスト
128      */
129     struct ImaAdpcmContext
130     {
131         s16 data;
132         u8  tableIndex;
133         u8  padding;
134     };
135 
136     /*!
137         :private
138         @brief      ImaAdpcm 情報
139      */
140     struct ImaAdpcmInfo
141     {
142         ImaAdpcmContext   context;        //!< 初回再生時コンテキスト情報
143         ImaAdpcmContext   loopContext;    //!< ループ再生時コンテキスト情報
144     };
145 
146     struct InfoBlock
147     {
148         BlockHeader header;
149         InfoBlockBody body;
150     };
151 
152     /*!
153         @brief      エンコードフォーマットを表す列挙型です。
154      */
155     typedef enum
156     {
157         ENCODING_PCM8      = 0,    //!< 8 ビット PCM フォーマットを表します。
158         ENCODING_PCM16     = 1,    //!< 16 ビット PCM フォーマットを表します。
159         ENCODING_DSP_ADPCM = 2,    //!< DSP ADPCM フォーマットを表します。
160         ENCODING_IMA_ADPCM = 3,    //!< IMA ADPCM フォーマットを表します。
161         ENCODING_NUM       = 4
162     } Encoding;
163 
164     /*!
165         @brief      チャンネルを表す列挙型です。
166      */
167     typedef enum
168     {
169         CHANNEL_INDEX_L = 0,       //!< 左チャンネルを表します。
170         CHANNEL_INDEX_R = 1        //!< 右チャンネルを表します。
171     } ChannelIndex;
172 
173     /*!
174       @name       bcwav ファイル操作
175       @{
176      */
177     /*!
178         @brief      波形の基本情報を取得します。
179         @param[in]  bcwav           データバッファ
180         @return     波形情報。
181      */
182     static const WaveInfo& GetWaveInfo(const void* bcwav);
183 
184     /*!
185         @brief      波形のチャンネル数を取得します。
186         @param[in]  bcwav           データバッファ
187         @return     チャンネル数。
188      */
189     static int GetChannelCount(const void* bcwav);
190 
191     /*!
192         @brief      ヘッダを除いた波形データを取得します。
193         @param[in]  bcwav           データバッファ
194         @param[in]  channelNo       チャンネル数
195         @return     波形データ。
196      */
197     static const void* GetWave(const void* bcwav, int channelNo);
198 
199     /*!
200         @brief      DSP ADPCM 情報を取得します。
201         @param[in]  bcwav           データバッファ
202         @param[in]  channelNo       チャンネル数
203         @return     DSP ADPCM 情報。
204      */
205     static const DspAdpcmInfo* GetDspAdpcmInfo(const void* bcwav, int channelNo);
206 
207     /*!
208         :private
209         @brief      再生開始時の IMA ADPCM コンテキストを取得します。
210         @param[in]  bcwav           データバッファ
211         @param[in]  channelNo       チャンネル数
212         @return     IMA ADPCM コンテキスト。
213      */
214     static const ImaAdpcmContext* GetImaAdpcmContext(const void* bcwav, int channelNo);
215 
216     /*!
217         :private
218         @brief      繰り返し時の IMA ADPCM コンテキストを取得します。bcwav ファイルバージョン 2.1.0.0 未満では NULL を返します。
219         @param[in]  bcwav           データバッファ
220         @param[in]  channelNo       チャンネル数
221         @return     IMA ADPCM コンテキスト。
222      */
223     static const ImaAdpcmContext* GetImaAdpcmLoopContext(const void* bcwav, int channelNo);
224 
225     /*!
226         @brief      各フォーマットのサンプル数をバイト数に換算します。
227         @param[in]  encoding        bcwav 内圧縮方式 (WaveInfo::encoding)
228         @param[in]  frame           サンプル数
229         @return     バイト数。
230      */
231     static u32 FrameToByte(u8 encoding, u32 frame);
232 
233     /*!
234         @brief      ポインタにオフセットを加えた値を返します。
235         @param[in]  ptr             基準ポインタ
236         @param[in]  offset          オフセット
237         @return     オフセットを加えたポインタ。
238      */
239     static const void* AddOffsetToPtr(const void* ptr, int offset);
240 
241     /*!
242         @brief      bcwav ファイルかどうかを判別します。
243         @param[in]  bcwav           データバッファ
244         @return     bcwav ファイルであれば true を、そうでなければ false を返します。
245      */
246     static bool IsBcwav(const void* bcwav);
247 
248     /*!
249         @}
250     */
251 };
252 
253 }}} // namespace nn::snd::CTR
254 
255 #endif // __cplusplus
256 
257 typedef enum
258 {
259     NN_SND_BCWAV_ENCODING_PCM8      = 0,    //!< 8 ビット PCM フォーマットを表します。
260     NN_SND_BCWAV_ENCODING_PCM16     = 1,    //!< 16 ビット PCM フォーマットを表します。
261     NN_SND_BCWAV_ENCODING_DSP_ADPCM = 2,    //!< DSP ADPCM フォーマットを表します。
262     NN_SND_BCWAV_ENCODING_IMA_ADPCM = 3,    //!< IMA ADPCM フォーマットを表します。
263     NN_SND_BCWAV_ENCODING_NUM       = 4
264 } nnsndBcwavEncoding;
265 
266 typedef enum
267 {
268     NN_SND_BCWAV_CHANNEL_INDEX_L = 0,       //!< 左チャンネルを表します。
269     NN_SND_BCWAV_CHANNEL_INDEX_R = 1        //!< 右チャンネルを表します。
270 } nnsndBcwavChannelIndex;
271 
272 #endif /* NN_SND_BCWAV_H_ */
273 
274