HD audio cache separated from avisynth provider

Originally committed to SVN as r173.
This commit is contained in:
Rodrigo Braz Monteiro 2006-02-25 08:39:15 +00:00
parent c8a7912319
commit ed55a9e430
6 changed files with 178 additions and 102 deletions

View File

@ -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) {

View File

@ -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 && !canceled; i+=block) {
if (block+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

View File

@ -42,38 +42,20 @@
#include <wx/wxprec.h>
#ifdef __WINDOWS__
#include <fstream>
#include <time.h>
#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();

View File

@ -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
// 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 && !canceled; i+=block) {
if (block+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<oldcount;i++) {
temp[i] = 0;
}
}
if (bytes_per_sample == 2) {
short *temp = (short *) buf;
for (int i=count;i<oldcount;i++) {
temp[i] = 0;
}
}
}
if (count) {
wxMutexLocker disklock(diskmutex);
file_cache.seekg(start*bytes_per_sample);
file_cache.read((char*)buf,count*bytes_per_sample*channels);
}
}
///////////////////////////
// Get disk cache path
wxString HDAudioProvider::DiskCachePath() {
return AegisubApp::folderName;
}
///////////////////////////
// Get disk cache filename
wxString HDAudioProvider::DiskCacheName() {
return DiskCachePath() + _T("audio.tmp");
}

View File

@ -0,0 +1,61 @@
// Copyright (c) 2006, Rodrigo Braz Monteiro
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of the Aegisub Group nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// -----------------------------------------------------------------------------
//
// AEGISUB
//
// Website: http://aegisub.cellosoft.com
// Contact: mailto:zeratul@cellosoft.com
//
#pragma once
///////////
// Headers
#include "audio_provider.h"
#include <fstream>
////////////////////////
// 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);
};

View File

@ -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();