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/dummy_audio.cpp \
audio/pcm.cpp \ audio/pcm.cpp \
cache/audio_ram.cpp \ cache/audio_ram.cpp \
cache/audio_hd.cpp \
cache/video_cache.cpp \ cache/video_cache.cpp \
$(SRC_OPT) $(SRC_OPT)

View file

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

View file

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