Audio HD cache improvements

Originally committed to SVN as r621.
This commit is contained in:
Rodrigo Braz Monteiro 2006-12-26 03:00:15 +00:00
parent 4a1c778595
commit b54198b721
4 changed files with 39 additions and 15 deletions

View file

@ -37,6 +37,8 @@
/////////// ///////////
// Headers // Headers
#include <wx/filename.h> #include <wx/filename.h>
#include <wx/file.h>
#include <wx/filefn.h>
#include "dialog_progress.h" #include "dialog_progress.h"
#include "audio_provider_hd.h" #include "audio_provider_hd.h"
#include "main.h" #include "main.h"
@ -63,10 +65,10 @@ HDAudioProvider::HDAudioProvider(AudioProvider *source) {
} }
// Open output file // Open output file
std::ofstream file; diskCacheFilename = DiskCacheName();
char filename[512]; file_cache.Create(diskCacheFilename,true,wxS_DEFAULT);
strcpy(filename,DiskCacheName().mb_str(wxConvLocal)); file_cache.Open(diskCacheFilename,wxFile::read_write);
file.open(filename,std::ios::binary | std::ios::out | std::ios::trunc); if (!file_cache.IsOpened()) throw _T("Unable to write to disk cache.");
// Start progress // Start progress
volatile bool canceled = false; volatile bool canceled = false;
@ -79,17 +81,17 @@ HDAudioProvider::HDAudioProvider(AudioProvider *source) {
for (__int64 i=0;i<num_samples && !canceled; i+=block) { for (__int64 i=0;i<num_samples && !canceled; i+=block) {
if (block+i > num_samples) block = num_samples - i; if (block+i > num_samples) block = num_samples - i;
source->GetAudio(temp,i,block); source->GetAudio(temp,i,block);
file.write(temp,block * channels * bytes_per_sample); file_cache.Write(temp,block * channels * bytes_per_sample);
progress->SetProgress(i,num_samples); progress->SetProgress(i,num_samples);
} }
file.close(); file_cache.Seek(0);
// Finish // Finish
if (!canceled) { if (!canceled) {
progress->Destroy(); progress->Destroy();
file_cache.open(filename,std::ios::binary | std::ios::in);
} }
else { else {
file_cache.Close();
throw wxString(_T("Audio loading cancelled by user")); throw wxString(_T("Audio loading cancelled by user"));
} }
} }
@ -98,8 +100,8 @@ HDAudioProvider::HDAudioProvider(AudioProvider *source) {
////////////// //////////////
// Destructor // Destructor
HDAudioProvider::~HDAudioProvider() { HDAudioProvider::~HDAudioProvider() {
file_cache.close(); file_cache.Close();
wxRemoveFile(DiskCacheName()); wxRemoveFile(diskCacheFilename);
} }
@ -129,8 +131,8 @@ void HDAudioProvider::GetAudio(void *buf, __int64 start, __int64 count) {
if (count) { if (count) {
wxMutexLocker disklock(diskmutex); wxMutexLocker disklock(diskmutex);
file_cache.seekg(start*bytes_per_sample); file_cache.Seek(start*bytes_per_sample);
file_cache.read((char*)buf,count*bytes_per_sample*channels); file_cache.Read((char*)buf,count*bytes_per_sample*channels);
} }
} }
@ -150,5 +152,24 @@ wxString HDAudioProvider::DiskCachePath() {
/////////////////////////// ///////////////////////////
// Get disk cache filename // Get disk cache filename
wxString HDAudioProvider::DiskCacheName() { wxString HDAudioProvider::DiskCacheName() {
return DiskCachePath() + _T("audio.tmp"); // Get pattern
wxString pattern = Options.AsText(_T("Audio HD Cache Name"));
if (pattern.Find(_T("%02i")) == wxNOT_FOUND) pattern = _T("audio%02i.tmp");
// Try from 00 to 99
for (int i=0;i<100;i++) {
// File exists?
wxString curStringTry = DiskCachePath() + wxString::Format(pattern.c_str(),i);
if (!wxFile::Exists(curStringTry)) return curStringTry;
// Exists, see if it can be opened (disabled because wx doesn't seem to lock the files...)
if (false) {
wxFile test(curStringTry,wxFile::write);
if (test.IsOpened()) {
test.Close();
return curStringTry;
}
}
}
return _T("");
} }

View file

@ -40,7 +40,7 @@
/////////// ///////////
// Headers // Headers
#include "audio_provider.h" #include "audio_provider.h"
#include <fstream> #include <wx/file.h>
//////////////////////// ////////////////////////
@ -48,7 +48,8 @@
class HDAudioProvider : public AudioProvider { class HDAudioProvider : public AudioProvider {
private: private:
wxMutex diskmutex; wxMutex diskmutex;
std::ifstream file_cache; wxFile file_cache;
wxString diskCacheFilename;
static wxString DiskCachePath(); static wxString DiskCachePath();
static wxString DiskCacheName(); static wxString DiskCacheName();

View file

@ -43,7 +43,8 @@ Please visit http://aegisub.net to download latest version
- Improved Syntax Highlighter to include more features of ASS. (AMZ) - Improved Syntax Highlighter to include more features of ASS. (AMZ)
- Added an inline Hunspell-based Spell Checker. (AMZ) - Added an inline Hunspell-based Spell Checker. (AMZ)
- Added an inline MyThes-based Thesaurus. (AMZ) - Added an inline MyThes-based Thesaurus. (AMZ)
- Added an option ("Audio HD Cache Location") that allows you to specify where to keep the audio cache. (AMZ) - Added two options ("Audio HD Cache Location" and "Audio HD Cache Name") that allows you to specify where to keep the audio cache. (AMZ)
- Flexibible audio cache names now allow you to have more than one copy of Aegisub open with audio loaded to HD cache. (AMZ)
= 1.10 beta - 2006.08.07 =========================== = 1.10 beta - 2006.08.07 ===========================

View file

@ -244,6 +244,7 @@ void OptionsManager::LoadDefaults() {
SetInt(_T("Audio Display Height"),100); SetInt(_T("Audio Display Height"),100);
SetBool(_T("Audio Spectrum"),false); SetBool(_T("Audio Spectrum"),false);
SetText(_T("Audio HD Cache Location"),_T("default")); SetText(_T("Audio HD Cache Location"),_T("default"));
SetText(_T("Audio HD Cache Name"),_T("audio%02i.tmp"));
SetInt(_T("Timing processor key start before thres"),5); SetInt(_T("Timing processor key start before thres"),5);
SetInt(_T("Timing processor key start after thres"),4); SetInt(_T("Timing processor key start after thres"),4);