mirror of https://github.com/odrling/Aegisub
Made audio provider converter able to convert from 22 kHz
Originally committed to SVN as r1833.
This commit is contained in:
parent
4478f14116
commit
dd1ded0671
|
@ -792,6 +792,7 @@ void AudioDisplay::SetSamplesPercent(int percent,bool update,float pivot) {
|
||||||
void AudioDisplay::UpdateSamples() {
|
void AudioDisplay::UpdateSamples() {
|
||||||
// Set samples
|
// Set samples
|
||||||
if (!provider) return;
|
if (!provider) return;
|
||||||
|
if (w) {
|
||||||
int64_t totalSamples = provider->GetNumSamples();
|
int64_t totalSamples = provider->GetNumSamples();
|
||||||
int total = totalSamples / w;
|
int total = totalSamples / w;
|
||||||
int max = 5760000 / w; // 2 minutes at 48 kHz maximum
|
int max = 5760000 / w; // 2 minutes at 48 kHz maximum
|
||||||
|
@ -806,7 +807,8 @@ void AudioDisplay::UpdateSamples() {
|
||||||
if (PositionSample + length > totalSamples) {
|
if (PositionSample + length > totalSamples) {
|
||||||
PositionSample = totalSamples - length;
|
PositionSample = totalSamples - length;
|
||||||
if (PositionSample < 0) PositionSample = 0;
|
if (PositionSample < 0) PositionSample = 0;
|
||||||
Position = PositionSample / samples;
|
if (samples) Position = PositionSample / samples;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1915,8 +1917,8 @@ void AudioDisplay::OnSize(wxSizeEvent &event) {
|
||||||
h -= Options.AsBool(_T("Audio Draw Timeline")) ? 20 : 0;
|
h -= Options.AsBool(_T("Audio Draw Timeline")) ? 20 : 0;
|
||||||
|
|
||||||
// Update image
|
// Update image
|
||||||
if (samples) {
|
|
||||||
UpdateSamples();
|
UpdateSamples();
|
||||||
|
if (samples) {
|
||||||
UpdatePosition(PositionSample / samples);
|
UpdatePosition(PositionSample / samples);
|
||||||
}
|
}
|
||||||
UpdateImage();
|
UpdateImage();
|
||||||
|
|
|
@ -192,7 +192,7 @@ AudioProvider *AudioProviderFactory::GetAudioProvider(wxString filename, int cac
|
||||||
// Try a PCM provider first
|
// Try a PCM provider first
|
||||||
provider = CreatePCMAudioProvider(filename);
|
provider = CreatePCMAudioProvider(filename);
|
||||||
if (provider) {
|
if (provider) {
|
||||||
if (provider->GetBytesPerSample() == 2) return provider;
|
if (provider->GetBytesPerSample() == 2 && provider->GetSampleRate() >= 32000) return provider;
|
||||||
return new ConvertAudioProvider(provider);
|
return new ConvertAudioProvider(provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -221,7 +221,7 @@ AudioProvider *AudioProviderFactory::GetAudioProvider(wxString filename, int cac
|
||||||
if (!provider) throw error;
|
if (!provider) throw error;
|
||||||
|
|
||||||
// Give it a conversor if needed
|
// Give it a conversor if needed
|
||||||
if (provider->GetBytesPerSample() != 2) provider = new ConvertAudioProvider(provider);
|
if (provider->GetBytesPerSample() != 2 || provider->GetSampleRate() < 32000) provider = new ConvertAudioProvider(provider);
|
||||||
|
|
||||||
// Change provider to RAM/HD cache if needed
|
// Change provider to RAM/HD cache if needed
|
||||||
if (cache == -1) cache = Options.AsInt(_T("Audio Cache"));
|
if (cache == -1) cache = Options.AsInt(_T("Audio Cache"));
|
||||||
|
|
|
@ -50,6 +50,12 @@ ConvertAudioProvider::ConvertAudioProvider(AudioProvider *src) {
|
||||||
num_samples = source->GetNumSamples();
|
num_samples = source->GetNumSamples();
|
||||||
sample_rate = source->GetSampleRate();
|
sample_rate = source->GetSampleRate();
|
||||||
bytes_per_sample = 2;
|
bytes_per_sample = 2;
|
||||||
|
|
||||||
|
sampleMult = 1;
|
||||||
|
if (sample_rate < 16000) sampleMult = 4;
|
||||||
|
else if (sample_rate < 32000) sampleMult = 2;
|
||||||
|
sample_rate *= sampleMult;
|
||||||
|
num_samples *= sampleMult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -60,28 +66,99 @@ ConvertAudioProvider::~ConvertAudioProvider() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/////////////////////
|
||||||
|
// Convert to 16-bit
|
||||||
|
void ConvertAudioProvider::Make16Bit(const char *src, short *dst, int64_t count) {
|
||||||
|
for (int64_t i=0;i<count;i++) {
|
||||||
|
dst[i] = (src[i]-128)*255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////
|
||||||
|
// Change sample rate
|
||||||
|
// This requres 16-bit input
|
||||||
|
void ConvertAudioProvider::ChangeSampleRate(const short *src, short *dst, int64_t count) {
|
||||||
|
// Upsample by 2
|
||||||
|
if (sampleMult == 2) {
|
||||||
|
int64_t size = count/2;
|
||||||
|
short cur;
|
||||||
|
short next = 0;
|
||||||
|
for (int64_t i=0;i<size;i++) {
|
||||||
|
cur = next;
|
||||||
|
next = *src++;
|
||||||
|
*(dst++) = cur;
|
||||||
|
*(dst++) = (cur+next)/2;
|
||||||
|
}
|
||||||
|
if (count%2) *(dst++) = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upsample by 4
|
||||||
|
else if (sampleMult == 4) {
|
||||||
|
int64_t size = count/4;
|
||||||
|
short cur;
|
||||||
|
short next = 0;
|
||||||
|
for (int64_t i=0;i<size;i++) {
|
||||||
|
cur = next;
|
||||||
|
next = *(src++);
|
||||||
|
*(dst++) = cur;
|
||||||
|
*(dst++) = (cur*3+next)/4;
|
||||||
|
*(dst++) = (cur+next)/2;
|
||||||
|
*(dst++) = (cur+next*3)/4;
|
||||||
|
}
|
||||||
|
for (int i=0;i<count%4;i++) *(dst++) = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nothing to do (shouldn't really get here, but...)
|
||||||
|
else if (sampleMult == 1) memcpy((void*)src,dst,count);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/////////////
|
/////////////
|
||||||
// Get audio
|
// Get audio
|
||||||
void ConvertAudioProvider::GetAudio(void *destination, int64_t start, int64_t count) {
|
void ConvertAudioProvider::GetAudio(void *destination, int64_t start, int64_t count) {
|
||||||
// Bits per sample
|
// Bits per sample
|
||||||
int srcBps = source->GetBytesPerSample();
|
int srcBps = source->GetBytesPerSample();
|
||||||
|
|
||||||
|
// Nothing to do
|
||||||
|
if (sampleMult == 1 && srcBps == 2) {
|
||||||
|
source->GetAudio(destination,start,count);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert
|
||||||
|
else {
|
||||||
|
// Allocate buffers with sufficient size for the entire operation
|
||||||
|
size_t fullSize = count;
|
||||||
|
int64_t srcCount = count / sampleMult;
|
||||||
|
short *buffer1 = NULL;
|
||||||
|
short *buffer2 = NULL;
|
||||||
|
short *last = NULL;
|
||||||
|
|
||||||
|
// Read audio
|
||||||
|
buffer1 = new short[fullSize];
|
||||||
|
source->GetAudio(buffer1,start/sampleMult,srcCount);
|
||||||
|
|
||||||
// Convert from 8-bit to 16-bit
|
// Convert from 8-bit to 16-bit
|
||||||
if (srcBps == 1) {
|
if (srcBps == 1) {
|
||||||
unsigned char *buffer = new unsigned char[count];
|
if (sampleMult == 1) {
|
||||||
source->GetAudio(buffer,start,count);
|
Make16Bit((const char*)buffer1,(short*)destination,srcCount);
|
||||||
short temp;
|
}
|
||||||
short *dst = (short*) destination;
|
else {
|
||||||
for (int64_t i=0;i<count;i++) {
|
buffer2 = new short[fullSize];
|
||||||
temp = (short) buffer[i];
|
Make16Bit((const char*)buffer1,buffer2,srcCount);
|
||||||
dst[i] = (temp-128)*256+temp;
|
last = buffer2;
|
||||||
}
|
}
|
||||||
delete [] buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// No conversion needed
|
// Already 16-bit
|
||||||
else if (srcBps == 2) source->GetAudio(destination,start,count);
|
else if (srcBps == 2) last = buffer1;
|
||||||
|
|
||||||
// Unsupported
|
// Convert sample rate
|
||||||
else throw _T("Unknown bits per sample value.");
|
if (sampleMult != 1) {
|
||||||
|
ChangeSampleRate(last,(short*)destination,count);
|
||||||
|
}
|
||||||
|
|
||||||
|
delete [] buffer1;
|
||||||
|
delete [] buffer2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,11 @@
|
||||||
// Audio provider class
|
// Audio provider class
|
||||||
class ConvertAudioProvider : public AudioProvider {
|
class ConvertAudioProvider : public AudioProvider {
|
||||||
private:
|
private:
|
||||||
|
int sampleMult;
|
||||||
|
|
||||||
AudioProvider *source;
|
AudioProvider *source;
|
||||||
|
void Make16Bit(const char *src, short *dst, int64_t count);
|
||||||
|
void ChangeSampleRate(const short *src, short *dst, int64_t count);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ConvertAudioProvider(AudioProvider *source);
|
ConvertAudioProvider(AudioProvider *source);
|
||||||
|
|
Loading…
Reference in New Issue