Пример #1
0
//////////////////////////////////////////////////////////////
//  Override, 'cause we don't want to actually LOAD the sound for streaming,
//  just make sure it's decompressed and such and ready to stream.
plSoundBuffer::ELoadReturnVal plWin32StreamingSound::IPreLoadBuffer( bool playWhenLoaded, bool isIncidental /* = false */ )
{
    if(fPlayWhenStopped)
        return plSoundBuffer::kPending;
    bool sfxPath = fNewFilename.IsValid() ? false : true;

    if (fDataStream != nil && !fNewFilename.IsValid())
        return plSoundBuffer::kSuccess;     // Already loaded

    if(!ILoadDataBuffer())
    {
        return plSoundBuffer::kError;
    }
    plSoundBuffer *buffer = (plSoundBuffer *)fDataBufferKey->ObjectIsLoaded();      
    if(!buffer)
        return plSoundBuffer::kError;
    
    // The databuffer also needs to know if the source is compressed or not
    if (fNewFilename.IsValid())
    {
        buffer->SetFileName(fNewFilename);
        buffer->SetFlag(plSoundBuffer::kStreamCompressed, fIsCompressed);
        fNewFilename = "";
        if(fReallyPlaying)
        {
            fPlayWhenStopped = true;
            return plSoundBuffer::kPending;
        }
    }

    if( buffer->IsValid() )
    {
        plAudioFileReader::StreamType type = plAudioFileReader::kStreamNative;
        fStreamType = kStreamCompressed;

        if(!buffer->HasFlag(plSoundBuffer::kStreamCompressed))
        {
            if(buffer->GetDataLengthInSecs() > plgAudioSys::GetStreamFromRAMCutoff( ))
            {
                fStreamType = kStreamFromDisk;
                type = plAudioFileReader::kStreamWAV;
            }
            else
            {
                fStreamType = kStreamFromRAM;
                type = plAudioFileReader::kStreamRAM;
            }
        }

        if(!fStartPos)
        {
            if(buffer->AsyncLoad(type, isIncidental ? 0 : STREAMING_BUFFERS * STREAM_BUFFER_SIZE ) == plSoundBuffer::kPending)
            {
                fPlayWhenLoaded = playWhenLoaded;
                fLoading = true;
                return plSoundBuffer::kPending;
            }
        }
        
        fSrcFilename = buffer->GetFileName();
        bool streamCompressed = (buffer->HasFlag(plSoundBuffer::kStreamCompressed) != 0);

        delete fDataStream;
        fDataStream = buffer->GetAudioReader();
        if(!fDataStream)
        {
            plAudioCore::ChannelSelect select = buffer->GetReaderSelect();

            bool streamCompressed = (buffer->HasFlag(plSoundBuffer::kStreamCompressed) != 0);

            /// Open da file
            plFileName strPath = plFileSystem::GetCWD();

            if (sfxPath)
                strPath = plFileName::Join(strPath, "sfx");
            strPath = plFileName::Join(strPath, fSrcFilename);
            fDataStream = plAudioFileReader::CreateReader(strPath, select,type);
        }

        if( fDataStream == nil || !fDataStream->IsValid() )
        {
            delete fDataStream;
            fDataStream = nil;
            return plSoundBuffer::kError;
        }

        IPrintDbgMessage(plString::Format("   Readied file %s for streaming", fSrcFilename.AsString().c_str()).c_str());

        // dont free sound data until we have a chance to use it in load sound

        return fDataStream ? plSoundBuffer::kSuccess : plSoundBuffer::kError;
    }

    plStatusLog::AddLineS("audio.log", "EnsureLoadable failed for streaming sound %d", fDataBufferKey->GetName().c_str());
    return plSoundBuffer::kError;
}
//////////////////////////////////////////////////////////////
//  Override, 'cause we don't want to actually LOAD the sound for streaming,
//  just make sure it's decompressed and such and ready to stream.
plSoundBuffer::ELoadReturnVal plWin32StreamingSound::IPreLoadBuffer( hsBool playWhenLoaded, hsBool isIncidental /* = false */ )
{
    if(fPlayWhenStopped)
        return plSoundBuffer::kPending;
    hsBool sfxPath = fNewFilename.size() ? false : true;
    
    if( fDataStream != nil && fNewFilename.size() == 0)
        return plSoundBuffer::kSuccess;     // Already loaded
        
    if(!ILoadDataBuffer())
    {
        return plSoundBuffer::kError;
    }
    plSoundBuffer *buffer = (plSoundBuffer *)fDataBufferKey->ObjectIsLoaded();      
    if(!buffer)
        return plSoundBuffer::kError;
    
    // The databuffer also needs to know if the source is compressed or not
    if(fNewFilename.length())
    {
        buffer->SetFileName(fNewFilename.c_str());
        buffer->SetFlag(plSoundBuffer::kStreamCompressed, fIsCompressed);
        fNewFilename.clear();
        if(fReallyPlaying)
        {
            fPlayWhenStopped = true;
            return plSoundBuffer::kPending;
        }
    }

    if( buffer->IsValid() )
    {
        plAudioFileReader::StreamType type = plAudioFileReader::kStreamNative;
        fStreamType = kStreamCompressed;

        if(!buffer->HasFlag(plSoundBuffer::kStreamCompressed))
        {
            if(buffer->GetDataLengthInSecs() > plgAudioSys::GetStreamFromRAMCutoff( ))
            {
                fStreamType = kStreamFromDisk;
                type = plAudioFileReader::kStreamWAV;
            }
            else
            {
                fStreamType = kStreamFromRAM;
                type = plAudioFileReader::kStreamRAM;
            }
        }

        if(!fStartPos)
        {
            if(buffer->AsyncLoad(type, isIncidental ? 0 : STREAMING_BUFFERS * STREAM_BUFFER_SIZE ) == plSoundBuffer::kPending)
            {
                fPlayWhenLoaded = playWhenLoaded;
                fLoading = true;
                return plSoundBuffer::kPending;
            }
        }
        
        char str[ 256 ];
        strncpy( fSrcFilename, buffer->GetFileName(), sizeof( fSrcFilename ) );
        bool streamCompressed = (buffer->HasFlag(plSoundBuffer::kStreamCompressed) != 0);

        delete fDataStream;
        fDataStream = buffer->GetAudioReader();
        if(!fDataStream)
        {
            plAudioCore::ChannelSelect select = buffer->GetReaderSelect();

            bool streamCompressed = (buffer->HasFlag(plSoundBuffer::kStreamCompressed) != 0);

            /// Open da file
            char strPath[ kFolderIterator_MaxPath ];
                
            getcwd(strPath, kFolderIterator_MaxPath);
            if(sfxPath)
                strcat( strPath, "\\sfx\\" );
            else
            {
                // if we've changing the filename don't append 'sfx', just append a '\' since a path to the folder is expected
                strcat( strPath, "\\");
            }
            strcat( strPath, fSrcFilename );
            fDataStream = plAudioFileReader::CreateReader(strPath, select,type);
        }           
        
        if( fDataStream == nil || !fDataStream->IsValid() )
        {
            delete fDataStream;
            fDataStream = nil;
            return plSoundBuffer::kError;
        }

        sprintf( str, "   Readied file %s for streaming", fSrcFilename );
        IPrintDbgMessage( str );

        // dont free sound data until we have a chance to use it in load sound

        return fDataStream ? plSoundBuffer::kSuccess : plSoundBuffer::kError;
    }

    plStatusLog::AddLineS("audio.log", "EnsureLoadable failed for streaming sound %d", fDataBufferKey->GetName().c_str());
    return plSoundBuffer::kError;
}