1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_StreamSoundFileLoader.cpp
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: 31311 $
16  *---------------------------------------------------------------------------*/
17 
18 #include "precompiled.h"
19 
20 #include <nw/snd/snd_StreamSoundFileLoader.h>
21 
22 namespace nw {
23 namespace snd {
24 namespace internal {
25 
26 /* ========================================================================
27         StreamSoundFileLoader
28    ======================================================================== */
29 
30 /*---------------------------------------------------------------------------*
31   Name:         LoadFileHeader
32 
33   Description:  ストリームデータのファイルヘッダとHEADブロックをロードし、
34                 StreamSoundFileReaderに渡す
35 
36   Arguments:    buffer - ロードに使用するバッファ
37                 size - バッファサイズ
38 
39   Returns:      AdpcmInfo構造体
40  *---------------------------------------------------------------------------*/
LoadFileHeader(void * buffer,unsigned long size)41 bool StreamSoundFileLoader::LoadFileHeader( void* buffer, unsigned long size )
42 {
43     const size_t headerSize = NW_SND_ROUND_UP_32B( sizeof( StreamSoundFile::FileHeader ) );
44     u8 buffer2[ headerSize+32 ];
45         // RVL_SDK の DVDRead( DVDFileInfo*, void* addr, s32 len, s32 ofs ); の制限は、
46         // 「addr = 32 バイト境界, len = 32 バイトの倍数, ofs = 4 バイトの倍数」
47 
48     // ファイルヘッダロード
49     m_Stream.Seek( 0, io::FILE_STREAM_SEEK_BEGIN );
50     s32 readSize = m_Stream.Read( ut::RoundUp( buffer2, 32 ), headerSize );
51     if ( readSize != headerSize )
52     {
53         return false;
54     }
55 
56     StreamSoundFile::FileHeader* header =
57         reinterpret_cast<StreamSoundFile::FileHeader*>( ut::RoundUp( buffer2, 32 ) );
58 
59     StreamSoundFileReader reader;
60     if ( ! reader.IsValidFileHeader( header ) )
61     {
62         return false;
63     }
64 
65     // バッファサイズがファイルヘッダーと INFO ブロックをロードできるサイズかチェック
66     // (必ず、ヘッダーと INFO ブロックが並んで格納されているものとする)
67     u32 loadSize = header->GetInfoBlockOffset() + header->GetInfoBlockSize();
68     if ( loadSize > size )
69     {
70         return false;
71     }
72 
73     // ファイルヘッダーと INFO ブロックをロード
74     m_Stream.Seek( 0, io::FILE_STREAM_SEEK_BEGIN );
75     readSize = m_Stream.Read( buffer, loadSize );
76     if ( readSize != loadSize )
77     {
78         return false;
79     }
80 
81     m_Reader.Initialize( buffer );
82 
83     return true;
84 }
85 
86 /*---------------------------------------------------------------------------*
87   Name:         GetTrackCount
88 
89   Description:  トラック数取得
90 
91   Arguments:    なし
92 
93   Returns:      トラック数
94  *---------------------------------------------------------------------------*/
GetTrackCount() const95 u32 StreamSoundFileLoader::GetTrackCount() const
96 {
97     if ( ! m_Reader.IsAvailable() ) return 0;
98     return m_Reader.GetTrackCount();
99 }
100 
101 /*---------------------------------------------------------------------------*
102   Name:         GetChannelCount
103 
104   Description:  チャンネル数取得
105 
106   Arguments:    なし
107 
108   Returns:      チャンネル数
109  *---------------------------------------------------------------------------*/
GetChannelCount() const110 u32 StreamSoundFileLoader::GetChannelCount() const
111 {
112     if ( ! m_Reader.IsAvailable() ) return 0;
113     return m_Reader.GetChannelCount();
114 }
115 
116 /*---------------------------------------------------------------------------*
117   Name:         ReadStrmInfo
118 
119   Description:
120 
121   Arguments:    strmInfo -
122 
123   Returns:
124  *---------------------------------------------------------------------------*/
ReadStreamInfo(StreamSoundFile::StreamSoundInfo * strmInfo) const125 bool StreamSoundFileLoader::ReadStreamInfo( StreamSoundFile::StreamSoundInfo* strmInfo ) const
126 {
127     if ( ! m_Reader.IsAvailable() ) return false;
128     m_Reader.ReadStreamSoundInfo( strmInfo );
129     return true;
130 }
131 
132 /*---------------------------------------------------------------------------*
133   Name:         ReadStrmTrackInfo
134 
135   Description:
136 
137   Arguments:    trackInfo -
138                 trackIndex -
139 
140   Returns:
141  *---------------------------------------------------------------------------*/
ReadStreamTrackInfo(StreamSoundFileReader::TrackInfo * trackInfo,int trackIndex) const142 bool StreamSoundFileLoader::ReadStreamTrackInfo( StreamSoundFileReader::TrackInfo* trackInfo, int trackIndex ) const
143 {
144     if ( ! m_Reader.IsAvailable() ) return false;
145     m_Reader.ReadStreamTrackInfo( trackInfo, trackIndex );
146     return true;
147 }
148 
149 /*---------------------------------------------------------------------------*
150   Name:         ReadDspAdpcmChannelInfo
151 
152   Description:
153 
154   Arguments:    adpcmInfo -
155                 channelIndex -
156 
157   Returns:
158  *---------------------------------------------------------------------------*/
ReadDspAdpcmChannelInfo(DspAdpcmParam * adpcmParam,DspAdpcmLoopParam * adpcmLoopParam,int channelIndex) const159 bool StreamSoundFileLoader::ReadDspAdpcmChannelInfo(
160     DspAdpcmParam* adpcmParam,
161     DspAdpcmLoopParam* adpcmLoopParam,
162     int channelIndex
163 ) const
164 {
165     if ( ! m_Reader.IsAvailable() ) return false;
166     m_Reader.ReadDspAdpcmChannelInfo( adpcmParam, adpcmLoopParam, channelIndex );
167     return true;
168 }
169 
170 /*---------------------------------------------------------------------------*
171   Name:         ReadAdpcBlockData
172 
173   Description:  ADPCブロックの指定されたデータを読み取る
174 
175   Arguments:    yn1, yn2 - 読み取ったデータを格納する配列
176                 blockIndex - 読み取るデータのブロック番号
177                 channelCount - チャンネル数(yn1,yn2の配列サイズ)
178 
179   Returns:      読み取れたら true, 読み取れなかったら false
180  *---------------------------------------------------------------------------*/
ReadAdpcBlockData(u16 * yn1,u16 * yn2,int blockIndex,int channelCount)181 bool StreamSoundFileLoader::ReadAdpcBlockData( u16* yn1, u16* yn2, int blockIndex, int channelCount )
182 {
183     if ( ! m_Reader.IsAvailable() ) return false;
184 
185     s32 readOffset = static_cast<s32>(
186         m_Reader.GetSeekBlockOffset() +
187         sizeof(ut::BinaryBlockHeader) +
188         sizeof(s16) * 2 * channelCount * blockIndex
189     );
190     m_Stream.Seek( readOffset, io::FILE_STREAM_SEEK_BEGIN );
191 
192     // TODO: 以下は8チャンネル以上で動作できないコードなのでASSERT
193     u32 readDataSize = sizeof(s16) * 2 * channelCount;
194     NW_ASSERT( readDataSize <= 32 );
195 
196     // DVDは32Byte単位でしかReadできないので32Byte読んでしまう
197     NN_ATTRIBUTE_ALIGN(32) static u16 buffer[16];
198     int readSize = m_Stream.Read( buffer, sizeof( buffer ) );
199     if ( readSize != sizeof(buffer) ) return false;
200 
201     for ( int i=0; i< channelCount; i++ )
202     {
203         yn1[i] = buffer[i*2];
204         yn2[i] = buffer[i*2+1];
205     }
206 
207     return true;
208 }
209 
210 } // namespace nw::snd::internal
211 } // namespace nw::snd
212 } // namespace nw
213 
214