+++ /dev/null
-#include "stdafx.h"\r
-/*\r
-* Copyright (c) 2007-2010 SlimDX Group\r
-* \r
-* Permission is hereby granted, free of charge, to any person obtaining a copy\r
-* of this software and associated documentation files (the "Software"), to deal\r
-* in the Software without restriction, including without limitation the rights\r
-* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
-* copies of the Software, and to permit persons to whom the Software is\r
-* furnished to do so, subject to the following conditions:\r
-* \r
-* The above copyright notice and this permission notice shall be included in\r
-* all copies or substantial portions of the Software.\r
-* \r
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
-* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
-* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
-* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
-* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
-* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
-* THE SOFTWARE.\r
-*/\r
-\r
-#include <windows.h>\r
-#include <vcclr.h>\r
-\r
-#include "../InternalHelpers.h"\r
-#include "../Utilities.h"\r
-#include "../DataStream.h"\r
-\r
-#include "WaveStream.h"\r
-\r
-using namespace System;\r
-using namespace System::IO;\r
-\r
-namespace SlimDX\r
-{\r
-namespace Multimedia\r
-{\r
- WaveStream::WaveStream( String^ path )\r
- {\r
- if( String::IsNullOrEmpty( path ) )\r
- throw gcnew ArgumentNullException( "path" );\r
-\r
- if( !File::Exists( path ) )\r
- throw gcnew FileNotFoundException( "Could not find wave file", path );\r
-\r
- pin_ptr<const wchar_t> pinnedPath = PtrToStringChars( path );\r
- handle = mmioOpen( const_cast<LPWSTR>( reinterpret_cast<LPCWSTR>( pinnedPath ) ), NULL, MMIO_ALLOCBUF | MMIO_READ );\r
- if( handle == NULL )\r
- throw gcnew FileLoadException( "Could not open the file", path );\r
-\r
- Init();\r
- }\r
-\r
- WaveStream::WaveStream( Stream^ stream )\r
- {\r
- InitStream( stream, 0 );\r
- }\r
-\r
- WaveStream::WaveStream( Stream^ stream, int length )\r
- {\r
- InitStream( stream, length );\r
- }\r
-\r
- void WaveStream::InitStream( Stream^ stream, int length )\r
- {\r
- if( stream == nullptr )\r
- throw gcnew ArgumentNullException( "stream" );\r
-\r
- DataStream^ ds = nullptr;\r
- array<Byte>^ bytes = Utilities::ReadStream( stream, length, &ds );\r
-\r
- if( bytes == nullptr )\r
- {\r
- Int64 size = ds->RemainingLength;\r
- internalMemory = gcnew DataStream( size, true, true );\r
- internalMemory->WriteRange( IntPtr( ds->SeekToEnd() ), size );\r
- }\r
- else\r
- {\r
- internalMemory = gcnew DataStream( bytes->LongLength, true, true );\r
- internalMemory->Write( bytes, 0, bytes->Length );\r
- }\r
- internalMemory->Position = 0;\r
-\r
- MMIOINFO info;\r
- ZeroMemory( &info, sizeof( info ) );\r
- info.fccIOProc = FOURCC_MEM;\r
- info.cchBuffer = bytes->Length;\r
- info.pchBuffer = internalMemory->PositionPointer;\r
-\r
- handle = mmioOpen( NULL, &info, MMIO_READ );\r
- if( handle == NULL )\r
- throw gcnew InvalidDataException( "Invalid wave file." );\r
-\r
- Init();\r
- }\r
-\r
- void WaveStream::Init()\r
- {\r
- MMCKINFO fileInfo;\r
- MMCKINFO chunk;\r
- PCMWAVEFORMAT pcmFormat; \r
-\r
- if( mmioDescend( handle, &fileInfo, NULL, 0 ) != 0 )\r
- throw gcnew InvalidDataException( "Invalid wave file." );\r
-\r
- if( fileInfo.ckid != FOURCC_RIFF ||\r
- fileInfo.fccType != mmioFOURCC( 'W', 'A', 'V', 'E' ) )\r
- throw gcnew InvalidDataException( "Invalid wave file." );\r
-\r
- chunk.ckid = mmioFOURCC( 'f', 'm', 't', ' ' );\r
- if( mmioDescend( handle, &chunk, &fileInfo, MMIO_FINDCHUNK ) != 0 )\r
- throw gcnew InvalidDataException( "Invalid wave file." );\r
-\r
- if( chunk.cksize < static_cast<LONG>( sizeof( PCMWAVEFORMAT ) ) )\r
- throw gcnew InvalidDataException( "Invalid wave file." );\r
-\r
- if( mmioRead( handle, reinterpret_cast<HPSTR>( &pcmFormat ), sizeof( pcmFormat ) ) != sizeof( pcmFormat ) )\r
- throw gcnew InvalidDataException( "Invalid wave file." );\r
-\r
- if( pcmFormat.wf.wFormatTag == WAVE_FORMAT_PCM )\r
- {\r
- auto_array<WAVEFORMATEX> tempFormat( reinterpret_cast<WAVEFORMATEX*>( new BYTE[sizeof( WAVEFORMATEX )] ) );\r
- memcpy( tempFormat.get(), &pcmFormat, sizeof( pcmFormat ) );\r
- tempFormat->cbSize = 0;\r
-\r
- format = WaveFormat::FromUnmanaged( *tempFormat.get() );\r
- }\r
- else\r
- {\r
- WORD extraBytes = 0;\r
- if( mmioRead( handle, reinterpret_cast<CHAR*>( &extraBytes ), sizeof( WORD ) ) != sizeof( WORD ) )\r
- throw gcnew InvalidDataException( "Invalid wave file." );\r
-\r
- auto_array<WAVEFORMATEX> tempFormat( reinterpret_cast<WAVEFORMATEX*>( new BYTE[sizeof( WAVEFORMATEX ) + extraBytes] ) );\r
- memcpy( tempFormat.get(), &pcmFormat, sizeof( pcmFormat ) );\r
- tempFormat->cbSize = extraBytes;\r
-\r
- if( mmioRead( handle, reinterpret_cast<CHAR*>( reinterpret_cast<BYTE*>( &tempFormat->cbSize ) + sizeof( WORD ) ), extraBytes ) != extraBytes )\r
- throw gcnew InvalidDataException( "Invalid wave file." );\r
-\r
- format = WaveFormatExtensible::FromBase( tempFormat.get() );\r
- }\r
-\r
- if( mmioAscend( handle, &chunk, 0 ) != 0 )\r
- throw gcnew InvalidDataException( "Invalid wave file." );\r
-\r
- chunk.ckid = mmioFOURCC( 'd', 'a', 't', 'a' );\r
- if( mmioDescend( handle, &chunk, &fileInfo, MMIO_FINDCHUNK ) != 0 )\r
- throw gcnew InvalidDataException( "Invalid wave file." );\r
-\r
- dataOffset = mmioSeek( handle, 0, SEEK_CUR );\r
- size = chunk.cksize;\r
-\r
- if( dataOffset < 0 || size <= 0 )\r
- throw gcnew InvalidDataException( "Invalid wave file." );\r
-\r
- if (internalMemory != nullptr)\r
- publicMemory = gcnew DataStream(internalMemory->RawPointer + dataOffset, internalMemory->Length - dataOffset, true, false, false);\r
- }\r
-\r
- WaveStream::~WaveStream()\r
- {\r
- Destruct();\r
- GC::SuppressFinalize( this );\r
- }\r
- \r
- WaveStream::!WaveStream()\r
- {\r
- Destruct();\r
- }\r
-\r
- void WaveStream::Destruct()\r
- {\r
- if( handle != NULL )\r
- {\r
- mmioClose( handle, 0 );\r
- handle = NULL;\r
- }\r
-\r
- if( internalMemory != nullptr )\r
- {\r
- delete internalMemory;\r
- internalMemory = nullptr;\r
- }\r
-\r
- if( publicMemory != nullptr )\r
- {\r
- delete publicMemory;\r
- publicMemory = nullptr;\r
- }\r
- }\r
-\r
- Int64 WaveStream::Seek( Int64 offset, SeekOrigin origin )\r
- {\r
- int targetPosition = static_cast<int>( offset );\r
- int targetOrigin = SEEK_CUR;\r
-\r
- if( origin == SeekOrigin::Begin )\r
- {\r
- targetOrigin = SEEK_SET;\r
- targetPosition += dataOffset;\r
- }\r
- else if( origin == SeekOrigin::End )\r
- {\r
- targetOrigin = SEEK_END;\r
- targetPosition = dataOffset;\r
- }\r
-\r
- int result = mmioSeek( handle, targetPosition, targetOrigin );\r
- if( result < 0 )\r
- throw gcnew InvalidOperationException("Cannot seek beyond the end of the stream.");\r
-\r
- return result - dataOffset;\r
- }\r
-\r
- void WaveStream::Write( array<Byte>^ buffer, int offset, int count )\r
- {\r
- SLIMDX_UNREFERENCED_PARAMETER(buffer);\r
- SLIMDX_UNREFERENCED_PARAMETER(offset);\r
- SLIMDX_UNREFERENCED_PARAMETER(count);\r
-\r
- throw gcnew NotSupportedException("WaveStream objects cannot be written to.");\r
- }\r
-\r
- int WaveStream::Read( array<Byte>^ buffer, int offset, int count )\r
- { \r
- Utilities::CheckArrayBounds( buffer, offset, count );\r
-\r
- // truncate the count to the end of the stream\r
- size_t actualCount = min( static_cast<size_t>( Length - Position ), static_cast<size_t>( count ) );\r
-\r
- pin_ptr<Byte> pinnedBuffer = &buffer[offset];\r
- int read = mmioRead( handle, reinterpret_cast<HPSTR>( pinnedBuffer ), static_cast<LONG>( actualCount ) );\r
-\r
- return read;\r
- }\r
-\r
- void WaveStream::Flush()\r
- {\r
- throw gcnew NotSupportedException("WaveStream objects cannot be flushed.");\r
- }\r
-\r
- void WaveStream::SetLength( Int64 value )\r
- {\r
- SLIMDX_UNREFERENCED_PARAMETER(value);\r
- throw gcnew NotSupportedException("WaveStream objects cannot be resized.");\r
- }\r
-\r
- Int64 WaveStream::Position::get()\r
- {\r
- return mmioSeek( handle, 0, SEEK_CUR ) - dataOffset;\r
- }\r
-\r
- void WaveStream::Position::set( System::Int64 value )\r
- {\r
- Seek( value, System::IO::SeekOrigin::Begin );\r
- }\r
- \r
- Int64 WaveStream::Length::get()\r
- {\r
- return size;\r
- }\r
-}\r
-}
\ No newline at end of file