diff --git a/core/audio_provider.cpp b/core/audio_provider.cpp index 2d63a43d8..072f7f62f 100644 --- a/core/audio_provider.cpp +++ b/core/audio_provider.cpp @@ -188,7 +188,7 @@ AudioProvider *AudioProvider::GetAudioProvider(wxString filename, AudioDisplay * if (cacheMode == 1) final = new RAMAudioProvider(provider); // Convert to HD - //if (cacheMode == 2) final = new HDAudioProvider(provider); + if (cacheMode == 2) final = new HDAudioProvider(provider); // Reassign if (final) { diff --git a/core/audio_provider_avs.cpp b/core/audio_provider_avs.cpp index fe6cd00f9..ab076bec6 100644 --- a/core/audio_provider_avs.cpp +++ b/core/audio_provider_avs.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2005-2006, Rodrigo Braz Monteiro +// Copyright (c) 2005-2006, Rodrigo Braz Monteiro, Fredrik Mellbin // All rights reserved. // // Redistribution and use in source and binary forms, with or without @@ -45,15 +45,11 @@ #include "utils.h" #include "audio_provider_avs.h" #include "options.h" -#include "main.h" -#include "dialog_progress.h" ////////////// // Constructor AvisynthAudioProvider::AvisynthAudioProvider(wxString _filename) { - type = AUDIO_PROVIDER_NONE; - filename = _filename; try { @@ -78,12 +74,6 @@ AvisynthAudioProvider::~AvisynthAudioProvider() { void AvisynthAudioProvider::Unload() { // Clean up avisynth clip = NULL; - - // Close file - if (type == AUDIO_PROVIDER_DISK_CACHE) { - file_cache.close(); - wxRemoveFile(DiskCacheName()); - } } @@ -91,7 +81,6 @@ void AvisynthAudioProvider::Unload() { // Load audio from avisynth void AvisynthAudioProvider::OpenAVSAudio() { // Set variables - type = AUDIO_PROVIDER_AVS; AVSValue script; // Prepare avisynth @@ -104,7 +93,9 @@ void AvisynthAudioProvider::OpenAVSAudio() { LoadFromClip(script); - } catch (AvisynthError &err) { + } + + catch (AvisynthError &err) { throw wxString::Format(_T("AviSynth error: %s"), wxString(err.msg,wxConvLocal)); } } @@ -150,47 +141,6 @@ void AvisynthAudioProvider::LoadFromClip(AVSValue _clip) { } -////////////// -// Disk Cache -void AvisynthAudioProvider::ConvertToDiskCache(PClip &tempclip) { - // Check free space - wxLongLong freespace; - if (wxGetDiskSpace(DiskCachePath(), NULL, &freespace)) - if (num_samples * channels * bytes_per_sample > freespace) - throw wxString(_T("Not enough free diskspace in "))+DiskCachePath()+wxString(_T(" to cache the audio")); - - // Open output file - std::ofstream file; - char filename[512]; - strcpy(filename,DiskCacheName().mb_str(wxConvLocal)); - file.open(filename,std::ios::binary | std::ios::out | std::ios::trunc); - - // Start progress - volatile bool canceled = false; - DialogProgress *progress = new DialogProgress(NULL,_T("Load audio"),&canceled,_T("Reading to Hard Disk cache"),0,num_samples); - progress->Show(); - - // Write to disk - int block = 4096; - char *temp = new char[block * channels * bytes_per_sample]; - for (__int64 i=0;i num_samples) block = num_samples - i; - tempclip->GetAudio(temp,i,block,env); - file.write(temp,block * channels * bytes_per_sample); - progress->SetProgress(i,num_samples); - } - file.close(); - type = AUDIO_PROVIDER_DISK_CACHE; - - // Finish - if (!canceled) { - progress->Destroy(); - file_cache.open(filename,std::ios::binary | std::ios::in); - } - else - throw wxString(_T("Audio loading cancelled by user")); -} - //////////////// // Get filename wxString AvisynthAudioProvider::GetFilename() { @@ -224,33 +174,8 @@ void AvisynthAudioProvider::GetAudio(void *buf, __int64 start, __int64 count) { } if (count) { - // Disk cache - if (type == AUDIO_PROVIDER_DISK_CACHE) { - wxMutexLocker disklock(diskmutex); - file_cache.seekg(start*bytes_per_sample); - file_cache.read((char*)buf,count*bytes_per_sample*channels); - } - - // Avisynth - else { - wxMutexLocker disklock(diskmutex); - clip->GetAudio(buf,start,count,env); - } + clip->GetAudio(buf,start,count,env); } } - -/////////////////////////// -// Get disk cache path -wxString AvisynthAudioProvider::DiskCachePath() { - return AegisubApp::folderName; -} - - -/////////////////////////// -// Get disk cache filename -wxString AvisynthAudioProvider::DiskCacheName() { - return DiskCachePath() + _T("audio.tmp"); -} - #endif diff --git a/core/audio_provider_avs.h b/core/audio_provider_avs.h index db3bca7b7..41cf807a6 100644 --- a/core/audio_provider_avs.h +++ b/core/audio_provider_avs.h @@ -42,38 +42,20 @@ #include #ifdef __WINDOWS__ -#include #include #include "avisynth_wrap.h" #include "audio_provider.h" -////////////// -// Types enum -enum AudioProviderType { - AUDIO_PROVIDER_NONE, - AUDIO_PROVIDER_AVS, - AUDIO_PROVIDER_DISK_CACHE -}; - - //////////////////////// // Audio provider class class AvisynthAudioProvider : public AudioProvider, public AviSynthWrapper { private: - wxMutex diskmutex; - - std::ifstream file_cache; - - AudioProviderType type; wxString filename; PClip clip; - void ConvertToDiskCache(PClip &tempclip); void LoadFromClip(AVSValue clip); void OpenAVSAudio(); - static wxString DiskCachePath(); - static wxString DiskCacheName(); void SetFile(); void Unload(); diff --git a/core/audio_provider_hd.cpp b/core/audio_provider_hd.cpp index d4dea8cb0..538ba438d 100644 --- a/core/audio_provider_hd.cpp +++ b/core/audio_provider_hd.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2005-2006, Rodrigo Braz Monteiro +// Copyright (c) 2005-2006, Rodrigo Braz Monteiro, Fredrik Mellbin // All rights reserved. // // Redistribution and use in source and binary forms, with or without @@ -35,4 +35,112 @@ /////////// -// Headers \ No newline at end of file +// Headers +#include "dialog_progress.h" +#include "audio_provider_hd.h" +#include "main.h" + + +/////////////// +// Constructor +HDAudioProvider::HDAudioProvider(AudioProvider *source) { + // Copy parameters + bytes_per_sample = source->GetBytesPerSample(); + num_samples = source->GetNumSamples(); + channels = source->GetChannels(); + sample_rate = source->GetSampleRate(); + filename = source->GetFilename(); + + // Check free space + wxLongLong freespace; + if (wxGetDiskSpace(DiskCachePath(), NULL, &freespace)) { + if (num_samples * channels * bytes_per_sample > freespace) { + throw wxString(_T("Not enough free diskspace in "))+DiskCachePath()+wxString(_T(" to cache the audio")); + } + } + + // Open output file + std::ofstream file; + char filename[512]; + strcpy(filename,DiskCacheName().mb_str(wxConvLocal)); + file.open(filename,std::ios::binary | std::ios::out | std::ios::trunc); + + // Start progress + volatile bool canceled = false; + DialogProgress *progress = new DialogProgress(NULL,_T("Load audio"),&canceled,_T("Reading to Hard Disk cache"),0,num_samples); + progress->Show(); + + // Write to disk + int block = 4096; + char *temp = new char[block * channels * bytes_per_sample]; + for (__int64 i=0;i num_samples) block = num_samples - i; + source->GetAudio(temp,i,block); + file.write(temp,block * channels * bytes_per_sample); + progress->SetProgress(i,num_samples); + } + file.close(); + + // Finish + if (!canceled) { + progress->Destroy(); + file_cache.open(filename,std::ios::binary | std::ios::in); + } + else { + throw wxString(_T("Audio loading cancelled by user")); + } +} + + +////////////// +// Destructor +HDAudioProvider::~HDAudioProvider() { + file_cache.close(); + wxRemoveFile(DiskCacheName()); +} + + +///////////// +// Get audio +void HDAudioProvider::GetAudio(void *buf, __int64 start, __int64 count) { + // Requested beyond the length of audio + if (start+count > num_samples) { + __int64 oldcount = count; + count = num_samples-start; + if (count < 0) count = 0; + + // Fill beyond with zero + if (bytes_per_sample == 1) { + char *temp = (char *) buf; + for (int i=count;i + + +//////////////////////// +// Audio provider class +class HDAudioProvider : public AudioProvider { +private: + wxMutex diskmutex; + std::ifstream file_cache; + + static wxString DiskCachePath(); + static wxString DiskCacheName(); + +public: + HDAudioProvider(AudioProvider *source); + ~HDAudioProvider(); + + void GetAudio(void *buf, __int64 start, __int64 count); +}; diff --git a/core/audio_provider_ram.cpp b/core/audio_provider_ram.cpp index df9e90e73..68f1763cb 100644 --- a/core/audio_provider_ram.cpp +++ b/core/audio_provider_ram.cpp @@ -73,7 +73,7 @@ RAMAudioProvider::RAMAudioProvider(AudioProvider *source) { throw wxString(_T("Couldn't open audio, not enough ram available.")); } - // Copy data + // Copy parameters bytes_per_sample = source->GetBytesPerSample(); num_samples = source->GetNumSamples(); channels = source->GetChannels();