1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_WaveSoundFileReader.cpp
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  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   $Revision: 24222 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 #include <nw/snd/snd_WaveSoundFileReader.h>
18 
19 namespace nw {
20 namespace snd {
21 namespace internal {
22 
23 namespace
24 {
25 
26 const u32 SIGNATURE_INFO_BLOCK = NW_UT_MAKE_SIGWORD( 'I', 'N', 'F', 'O' );
27 
28 const u32 SUPPORTED_FILE_VERSION = 0x01000000;  // ライブラリがサポートする最低バージョン
29 const u32 CURRENT_FILE_VERSION   = 0x01000000;  // ライブラリがサポートする最新バージョン
30 
IsValidFileHeader(const void * waveSoundFile)31 bool IsValidFileHeader( const void* waveSoundFile )
32 {
33     const ut::BinaryFileHeader& header =
34         *reinterpret_cast<const ut::BinaryFileHeader*>( waveSoundFile );
35 
36     // シグニチャ確認
37     NW_ASSERTMSG( header.signature == WaveSoundFileReader::SIGNATURE_FILE,
38             "invalid file signature." );
39     if ( header.signature != WaveSoundFileReader::SIGNATURE_FILE )
40     {
41         return false;
42     }
43 
44     // バージョン確認
45     NW_ASSERTMSG(
46             header.version >= SUPPORTED_FILE_VERSION,
47             "wavesound file is not supported version.\n"
48             "please reconvert file using new version tools.\n"
49             "(SUPPORTED_FILE_VERSION:0x%08x >= your version:0x%08x)\n",
50             SUPPORTED_FILE_VERSION, header.version
51     );
52     if ( header.version < SUPPORTED_FILE_VERSION )
53     {
54         return false;
55     }
56     NW_ASSERTMSG(
57             header.version <= CURRENT_FILE_VERSION,
58             "wavesound file is not supported version.\n"
59             "please reconvert file using new version tools.\n"
60             "(CURRENT_FILE_VERSION:0x%08x <= your version:0x%08x)\n",
61             CURRENT_FILE_VERSION, header.version
62     );
63     if ( header.version > CURRENT_FILE_VERSION )
64     {
65         return false;
66     }
67     return true;
68 }
69 
70 } // anonymous namespace
71 
WaveSoundFileReader(const void * waveSoundFile)72 WaveSoundFileReader::WaveSoundFileReader( const void* waveSoundFile )
73 : m_pHeader( NULL ),
74   m_pInfoBlockBody( NULL )
75 {
76     NW_NULL_ASSERT( waveSoundFile );
77 
78     if ( ! IsValidFileHeader( waveSoundFile ) ) return;
79 
80     m_pHeader = reinterpret_cast<const WaveSoundFile::FileHeader*>( waveSoundFile );
81 
82     // INFO ブロックチェック
83     const WaveSoundFile::InfoBlock* infoBlock = m_pHeader->GetInfoBlock();
84 
85     NW_NULL_ASSERT( infoBlock );
86     if ( infoBlock == NULL ) return;
87 
88     // シグニチャチェック
89     NW_ASSERTMSG( infoBlock->header.kind == SIGNATURE_INFO_BLOCK, "invalid block signature." );
90     m_pInfoBlockBody = &infoBlock->body;
91 }
92 
GetWaveSoundCount() const93 u32 WaveSoundFileReader::GetWaveSoundCount() const
94 {
95     NW_NULL_ASSERT( m_pInfoBlockBody );
96     return m_pInfoBlockBody->GetWaveSoundCount();
97 }
98 
GetNoteInfoCount(u32 index) const99 u32 WaveSoundFileReader::GetNoteInfoCount( u32 index ) const
100 {
101     NW_NULL_ASSERT( m_pInfoBlockBody );
102     NW_ASSERT( index < GetWaveSoundCount() );
103 
104     const WaveSoundFile::WaveSoundData& wsdData =
105         m_pInfoBlockBody->GetWaveSoundData( index );
106     return wsdData.GetNoteCount();
107 }
108 
GetTrackInfoCount(u32 index) const109 u32 WaveSoundFileReader::GetTrackInfoCount( u32 index ) const
110 {
111     NW_NULL_ASSERT( m_pInfoBlockBody );
112     NW_ASSERT( index < GetWaveSoundCount() );
113 
114     const WaveSoundFile::WaveSoundData& wsdData =
115         m_pInfoBlockBody->GetWaveSoundData( index );
116     return wsdData.GetTrackCount();
117 }
118 
ReadWaveSoundInfo(WaveSoundInfo * dst,u32 index) const119 bool WaveSoundFileReader::ReadWaveSoundInfo( WaveSoundInfo* dst, u32 index ) const
120 {
121     NW_NULL_ASSERT( m_pInfoBlockBody );
122     NW_NULL_ASSERT( dst );
123 
124     const WaveSoundFile::WaveSoundInfo& src =
125         m_pInfoBlockBody->GetWaveSoundData( index ).GetWaveSoundInfo();
126 
127     dst->pitch = src.GetPitch();
128     dst->pan = src.GetPan();
129     dst->surroundPan = src.GetSurroundPan();
130     src.GetSendValue( &dst->mainSend, dst->fxSend, AUX_BUS_NUM );
131     dst->adshr = src.GetAdshrCurve();
132 
133     return true;
134 }
135 
ReadNoteInfo(WaveSoundNoteInfo * dst,u32 index,u32 noteIndex) const136 bool WaveSoundFileReader::ReadNoteInfo(
137         WaveSoundNoteInfo* dst,
138         u32 index,
139         u32 noteIndex ) const
140 {
141     NW_NULL_ASSERT( m_pInfoBlockBody );
142     NW_NULL_ASSERT( dst );
143 
144     const WaveSoundFile::NoteInfo& src =
145         m_pInfoBlockBody->GetWaveSoundData( index ).GetNoteInfo( noteIndex );
146 
147     const Util::WaveId& waveId =
148         m_pInfoBlockBody->GetWaveIdTable().GetWaveId( src.waveIdTableIndex );
149 
150     dst->waveArchiveId = waveId.waveArchiveId;
151     dst->waveIndex = waveId.waveIndex;
152     dst->pitch = src.GetPitch();
153     dst->adshr = src.GetAdshrCurve();
154     dst->originalKey = src.GetOriginalKey();
155     dst->pan = src.GetPan();
156     dst->surroundPan = src.GetSurroundPan();
157     dst->volume = src.GetVolume();
158     return true;
159 }
160 
161 
162 } // namespace nw::snd::internal
163 } // namespace nw::snd
164 } // namespace nw
165