1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_StreamSoundFileReader.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: 19484 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include <nw/snd/snd_StreamSoundFileReader.h>
19 #include <nw/snd/snd_WaveFile.h>                // EncodeMethod
20 
21 namespace nw {
22 namespace snd {
23 namespace internal {
24 
25 namespace {
26 
27 const u32 SIGNATURE_FILE = NW_UT_MAKE_SIGWORD( 'C', 'S', 'T', 'M' );
28 const u32 SIGNATURE_INFO_BLOCK = NW_UT_MAKE_SIGWORD( 'I', 'N', 'F', 'O' );
29 const u32 SIGNATURE_SEEK_BLOCK = NW_UT_MAKE_SIGWORD( 'S', 'E', 'E', 'K' );
30 const u32 SIGNATURE_DATA_BLOCK = NW_UT_MAKE_SIGWORD( 'D', 'A', 'T', 'A' );
31 
32 const int SUPPORTED_FILE_VERSION = 0x02000000;
33 const int CURRENT_FILE_VERSION = 0x02000000;
34 
35 } // anonymous namespace
36 
StreamSoundFileReader()37 StreamSoundFileReader::StreamSoundFileReader()
38 : m_pHeader( NULL ),
39   m_pInfoBlockBody( NULL )
40 {
41 }
42 
Initialize(const void * streamSoundFile)43 void StreamSoundFileReader::Initialize( const void* streamSoundFile )
44 {
45     NW_NULL_ASSERT( streamSoundFile );
46 
47     if ( ! IsValidFileHeader( streamSoundFile ) ) return;
48 
49     m_pHeader =
50         reinterpret_cast<const StreamSoundFile::FileHeader*>( streamSoundFile );
51     const StreamSoundFile::InfoBlock* infoBlock = m_pHeader->GetInfoBlock();
52     NW_ASSERT( infoBlock->header.kind == SIGNATURE_INFO_BLOCK );
53 
54     m_pInfoBlockBody = &infoBlock->body;
55     NW_ASSERT( m_pInfoBlockBody->GetStreamSoundInfo()->oneBlockBytes % 32 == 0 );
56 }
57 
Finalize()58 void StreamSoundFileReader::Finalize()
59 {
60     m_pHeader = NULL;
61     m_pInfoBlockBody = NULL;
62 }
63 
ReadStreamSoundInfo(StreamSoundFile::StreamSoundInfo * strmInfo) const64 bool StreamSoundFileReader::ReadStreamSoundInfo(
65         StreamSoundFile::StreamSoundInfo* strmInfo ) const
66 {
67     NW_NULL_ASSERT( m_pInfoBlockBody );
68     const StreamSoundFile::StreamSoundInfo* info =
69         m_pInfoBlockBody->GetStreamSoundInfo();
70 
71     NW_ALIGN32_ASSERT( info->oneBlockBytes );
72     NW_ALIGN32_ASSERT( info->lastBlockPaddedBytes );
73 
74     // struct copy
75     *strmInfo = *info;
76 
77     return true;
78 }
79 
ReadStreamTrackInfo(TrackInfo * pTrackInfo,int trackIndex) const80 bool StreamSoundFileReader::ReadStreamTrackInfo(
81         TrackInfo* pTrackInfo, int trackIndex ) const
82 {
83     NW_NULL_ASSERT( m_pInfoBlockBody );
84     NW_NULL_ASSERT( pTrackInfo );
85     const StreamSoundFile::TrackInfoTable* table =
86         m_pInfoBlockBody->GetTrackInfoTable();
87 
88     if ( trackIndex >= static_cast<int>( table->GetTrackCount() ) )
89     {
90         return false;
91     }
92 
93     const StreamSoundFile::TrackInfo* src = table->GetTrackInfo( trackIndex );
94     pTrackInfo->volume = src->volume;
95     pTrackInfo->pan = src->pan;
96     pTrackInfo->channelCount = static_cast<u8>( src->GetTrackChannelCount() ); // TODO: バイナリデータも 1 バイトでいいかも (NW4C-0.1.0)
97 
98     u32 count = ut::Min( static_cast<int>( pTrackInfo->channelCount ), WAVE_CHANNEL_MAX );
99     for ( u32 i = 0; i < count; i++ )
100     {
101         pTrackInfo->globalChannelIndex[ i ] = src->GetGlobalChannelIndex( i );
102     }
103 
104     return true;
105 }
106 
ReadDspAdpcmChannelInfo(DspAdpcmParam * pParam,DspAdpcmLoopParam * pLoopParam,int channelIndex) const107 bool StreamSoundFileReader::ReadDspAdpcmChannelInfo(
108         DspAdpcmParam* pParam,
109         DspAdpcmLoopParam* pLoopParam,
110         int channelIndex ) const
111 {
112     NW_NULL_ASSERT( m_pInfoBlockBody );
113     NW_NULL_ASSERT( pParam );
114     NW_NULL_ASSERT( pLoopParam );
115 
116     const StreamSoundFile::DspAdpcmChannelInfo* src =
117         m_pInfoBlockBody->
118         GetChannelInfoTable()->
119         GetChannelInfo( channelIndex )->
120         GetDspAdpcmChannelInfo();
121     if ( src == NULL ) return false;
122 
123     *pParam = src->param;
124     *pLoopParam = src->loopParam;
125     return true;
126 }
127 
IsValidFileHeader(const void * streamSoundFile) const128 bool StreamSoundFileReader::IsValidFileHeader( const void* streamSoundFile ) const
129 {
130     NW_NULL_ASSERT( streamSoundFile );
131 
132     const ut::BinaryFileHeader& header =
133         *reinterpret_cast<const ut::BinaryFileHeader*>( streamSoundFile );
134 
135     // シグニチャ確認
136     NW_ASSERTMSG(
137             header.signature == SIGNATURE_FILE,
138             "invalid file signature. stream data is not available." );
139     if ( header.signature != SIGNATURE_FILE ) return false;
140 
141     // バージョン確認
142     NW_ASSERTMSG(
143             header.version >= SUPPORTED_FILE_VERSION,
144             "stream file is not supported version.\n"
145             "please reconvert file using new version tools.\n"
146             "(SUPPORTED_FILE_VERSION:0x%08x >= your version:0x%08x)\n",
147             SUPPORTED_FILE_VERSION, header.version
148     );
149     if ( header.version < SUPPORTED_FILE_VERSION ) return false;
150     NW_ASSERTMSG(
151             header.version <= CURRENT_FILE_VERSION,
152             "stream file is not supported version.\n"
153             "please reconvert file using new version tools.\n"
154             "(CURRENT_FILE_VERSION:0x%08x <= your version:0x%08x)\n",
155             CURRENT_FILE_VERSION, header.version
156     );
157     if ( header.version > CURRENT_FILE_VERSION ) return false;
158 
159     return true;
160 }
161 
162 } // namespace nw::snd::internal
163 } // namespace nw::snd
164 } // namespace nw
165 
166