Fix ram audio provider: this uses mutex.h (from google) in libaegisub and switches to use agi::io. The progress code has been stubbed out as well as a few path methods. There's no reason to guess at fixing them the only way to do it is after it's actually put into use.

Originally committed to SVN as r5310.
This commit is contained in:
Amar Takhar 2011-02-06 19:52:50 +00:00
parent 2edbc8c8c1
commit 4968fc2b55
3 changed files with 61 additions and 44 deletions

View File

@ -30,6 +30,7 @@ SRC = \
audio/dummy_audio.cpp \
audio/pcm.cpp \
cache/audio_ram.cpp \
cache/audio_hd.cpp \
cache/video_cache.cpp \
$(SRC_OPT)

View File

@ -37,17 +37,21 @@
#include "config.h"
#ifndef AGI_PRE
#include <wx/filefn.h>
#include <wx/filename.h>
//#include <wx/filefn.h>
//#include <wx/filename.h>
#endif
#include "audio_provider_hd.h"
#include "compat.h"
#include "dialog_progress.h"
#include "frame_main.h"
#include "main.h"
#include "standard_paths.h"
#include "utils.h"
#include "audio_hd.h"
//#include "compat.h"
//#include "dialog_progress.h"
//#include "frame_main.h"
//#include "main.h"
//#include "standard_paths.h"
//#include "utils.h"
#include <libaegisub/io.h>
namespace media {
/// @brief Constructor
/// @param source
@ -63,23 +67,26 @@ HDAudioProvider::HDAudioProvider(AudioProvider *src) {
samples_native_endian = source->AreSamplesNativeEndian();
// Check free space
wxLongLong freespace;
if (wxGetDiskSpace(DiskCachePath(), NULL, &freespace)) {
if (num_samples * channels * bytes_per_sample > freespace) {
throw AudioOpenError("Not enough free disk space in " + STD_STR(DiskCachePath()) + " to cache the audio");
}
}
uint64_t freespace;
// XXX: fixme (add diskspace method to agi::util)
// if (wxGetDiskSpace(DiskCachePath(), NULL, &freespace)) {
// if (num_samples * channels * bytes_per_sample > freespace) {
// throw AudioOpenError("Not enough free disk space in " + STD_STR(DiskCachePath()) + " to cache the audio");
// }
// }
// Open output file
diskCacheFilename = DiskCacheName();
file_cache.Create(diskCacheFilename,true,wxS_DEFAULT);
file_cache.Open(diskCacheFilename,wxFile::read_write);
if (!file_cache.IsOpened()) throw AudioOpenError("Unable to write to audio disk cache.");
// file_cache.Create(diskCacheFilename,true,wxS_DEFAULT);
io::Save file(diskCacheFilename);
std::ofstream& file_cache = file.Get();
// file_cache.Open(diskCacheFilename,wxFile::read_write);
if (!file_cache.is_open()) throw AudioOpenError("Unable to write to audio disk cache.");
// Start progress
volatile bool canceled = false;
DialogProgress *progress = new DialogProgress(AegisubApp::Get()->frame,_T("Load audio"),&canceled,_T("Reading to Hard Disk cache"),0,num_samples);
progress->Show();
// DialogProgress *progress = new DialogProgress(AegisubApp::Get()->frame,_T("Load audio"),&canceled,_T("Reading to Hard Disk cache"),0,num_samples);
// progress->Show();
// Write to disk
int block = 4096;
@ -87,25 +94,24 @@ HDAudioProvider::HDAudioProvider(AudioProvider *src) {
for (int64_t i=0;i<num_samples && !canceled; i+=block) {
if (block+i > num_samples) block = num_samples - i;
source->GetAudio(data,i,block);
file_cache.Write(data,block * channels * bytes_per_sample);
progress->SetProgress(i,num_samples);
file_cache.write(data,block * channels * bytes_per_sample);
// progress->SetProgress(i,num_samples);
}
file_cache.Seek(0);
file_cache.seekp(0);
// Finish
if (canceled) {
file_cache.Close();
// file_cache.Close();
delete[] data;
throw agi::UserCancelException("Audio loading cancelled by user");
}
progress->Destroy();
// progress->Destroy();
}
/// @brief Destructor
///
HDAudioProvider::~HDAudioProvider() {
file_cache.Close();
wxRemoveFile(diskCacheFilename);
//XXX wxRemoveFile(diskCacheFilename);
delete[] data;
}
@ -137,29 +143,34 @@ void HDAudioProvider::GetAudio(void *buf, int64_t start, int64_t count) const {
}
if (count) {
wxMutexLocker disklock(diskmutex);
file_cache.Seek(start*bytes_per_sample);
file_cache.Read((char*)buf,count*bytes_per_sample*channels);
diskmutex.Lock();
file_cache.seekp(start*bytes_per_sample);
file_cache.read((char*)buf,count*bytes_per_sample*channels);
}
}
/// @brief Get disk cache path
/// @return
///
wxString HDAudioProvider::DiskCachePath() {
std::string HDAudioProvider::DiskCachePath() {
// Default
wxString path = lagi_wxString(OPT_GET("Audio/Cache/HD/Location")->GetString());
/*
std::string path = lagi_wxString(OPT_GET("Audio/Cache/HD/Location")->GetString());
if (path == _T("default")) return StandardPaths::DecodePath(_T("?temp/"));
// Specified
return DecodeRelativePath(path,StandardPaths::DecodePath(_T("?user/")));
*/
return "XXX: fixme";
}
/// @brief Get disk cache filename
///
wxString HDAudioProvider::DiskCacheName() {
std::string HDAudioProvider::DiskCacheName() {
/*
XXX: fixme
// Get pattern
wxString pattern = lagi_wxString(OPT_GET("Audio/Cache/HD/Name")->GetString());
std::string pattern = lagi_wxString(OPT_GET("Audio/Cache/HD/Name")->GetString());
if (pattern.Find(_T("%02i")) == wxNOT_FOUND) pattern = _T("audio%02i.tmp");
// Try from 00 to 99
@ -168,5 +179,8 @@ wxString HDAudioProvider::DiskCacheName() {
wxString curStringTry = DiskCachePath() + wxString::Format(pattern.c_str(),i);
if (!wxFile::Exists(curStringTry)) return curStringTry;
}
return L"";
*/
return "";
}
} // namespace media

View File

@ -35,12 +35,12 @@
///
#ifndef AGI_PRE
#include <wx/file.h>
#include <wx/thread.h>
#endif
#include "include/aegisub/audio_provider.h"
#include <libaegisub/mutex.h>
#include "libmedia/audio.h"
namespace media {
/// DOCME
/// @class HDAudioProvider
/// @brief DOCME
@ -48,13 +48,13 @@
/// DOCME
class HDAudioProvider : public AudioProvider {
/// DOCME
mutable wxMutex diskmutex;
mutable agi::Mutex diskmutex;
/// DOCME
mutable wxFile file_cache;
mutable std::fstream file_cache;
/// DOCME
wxString diskCacheFilename;
std::string diskCacheFilename;
/// DOCME
bool samples_native_endian;
@ -62,8 +62,8 @@ class HDAudioProvider : public AudioProvider {
/// DOCME
char *data;
static wxString DiskCachePath();
static wxString DiskCacheName();
static std::string DiskCachePath();
static std::string DiskCacheName();
public:
HDAudioProvider(AudioProvider *source);
@ -73,3 +73,5 @@ public:
void GetAudio(void *buf, int64_t start, int64_t count) const;
};
} // namespace media