Skip to content

Commit

Permalink
GOAudioSection::m_SampleGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg68 committed Feb 22, 2024
1 parent 2c17967 commit 2ac71d4
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 107 deletions.
5 changes: 2 additions & 3 deletions src/grandorgue/GOOrganController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,17 @@ bool GOOrganController::IsCacheable() { return m_Cacheable; }

GOHashType GOOrganController::GenerateCacheHash() {
GOHash hash;

UpdateHash(hash);
hash.Update(sizeof(GOAudioSection));
hash.Update(sizeof(GOSoundingPipe));
hash.Update(sizeof(GOSoundReleaseAlignTable));
hash.Update(BLOCK_HISTORY);
hash.Update(MAX_READAHEAD);
hash.Update(SHORT_LOOP_LENGTH);
hash.Update(sizeof(attack_section_info));
hash.Update(sizeof(release_section_info));
GOSoundProvider::UpdateCacheHash(hash);
hash.Update(sizeof(audio_start_data_segment));
hash.Update(sizeof(audio_end_data_segment));

return hash.getHash();
}

Expand Down
79 changes: 18 additions & 61 deletions src/grandorgue/sound/GOSoundAudioSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void GOAudioSection::ClearData() {
m_SampleRate = 0;
m_BitsPerSample = 0;
m_BytesPerSample = 0;
m_Compressed = false;
m_IsCompressed = false;
m_Channels = 0;
if (m_Data) {
m_Pool.Free(m_Data);
Expand Down Expand Up @@ -81,7 +81,9 @@ bool GOAudioSection::LoadCache(GOCache &cache) {
return false;
if (!cache.Read(&m_BytesPerSample, sizeof(m_BytesPerSample)))
return false;
if (!cache.Read(&m_Compressed, sizeof(m_Compressed)))
if (!cache.Read(&m_SampleGroup, sizeof(m_SampleGroup)))
return false;
if (!cache.Read(&m_IsCompressed, sizeof(m_IsCompressed)))
return false;
if (!cache.Read(&m_Channels, sizeof(m_Channels)))
return false;
Expand Down Expand Up @@ -158,7 +160,9 @@ bool GOAudioSection::SaveCache(GOCacheWriter &cache) const {
return false;
if (!cache.Write(&m_BytesPerSample, sizeof(m_BytesPerSample)))
return false;
if (!cache.Write(&m_Compressed, sizeof(m_Compressed)))
if (!cache.Write(&m_SampleGroup, sizeof(m_SampleGroup)))
return false;
if (!cache.Write(&m_IsCompressed, sizeof(m_IsCompressed)))
return false;
if (!cache.Write(&m_Channels, sizeof(m_Channels)))
return false;
Expand Down Expand Up @@ -654,6 +658,7 @@ void GOAudioSection::Setup(
const unsigned pcm_data_sample_rate,
const unsigned pcm_data_nb_samples,
const std::vector<GOWaveLoop> *loop_points,
int8_t sampleGroup,
bool compress,
unsigned loopCrossfadeLength,
unsigned releaseCrossfadeLength) {
Expand Down Expand Up @@ -829,7 +834,8 @@ void GOAudioSection::Setup(
m_SampleCount = total_alloc_samples;
m_SampleFracBits = m_BitsPerSample - 1;
m_Channels = pcm_data_channels;
m_Compressed = false;
m_IsCompressed = false;
m_SampleGroup = sampleGroup;

/* Store the main data blob. */
memcpy(m_Data, pcm_data, m_AllocSize);
Expand Down Expand Up @@ -888,59 +894,10 @@ void GOAudioSection::Compress(bool format16) {
}
}

#if 0
GOAudioSection new_section(m_Pool);
new_section.m_BitsPerSample = m_BitsPerSample;
new_section.m_Channels = m_Channels;
new_section.m_Data = data;
new_section.m_AllocSize = output_len;
new_section.m_Compressed = true;
DecompressionCache tmp;
InitDecompressionCache(tmp);

for (unsigned i = 0; i < m_SampleCount; i++)
{
for (unsigned j = 0; j < m_StartSegments.size(); j++)
{
if (m_StartSegments[j].start_offset == i && i)
{
if (tmp.position != m_StartSegments[j].cache.position)
wxLogError(wxT("StartSeg %d: Pos %d %d"), j, tmp.position, m_StartSegments[j].cache.position);
if(tmp.prev[0] != m_StartSegments[j].cache.prev[0] ||
tmp.prev[1] != m_StartSegments[j].cache.prev[1])
wxLogError(wxT("StartSeg %d: prev %d %d - %d %d "), j, tmp.prev[0], m_StartSegments[j].cache.prev[0],
tmp.prev[1], m_StartSegments[j].cache.prev[1]);
if(tmp.value[0] != m_StartSegments[j].cache.value[0] ||
tmp.value[1] != m_StartSegments[j].cache.value[1])
wxLogError(wxT("StartSeg %d: prev %d %d - %d %d "), j, tmp.value[0], m_StartSegments[j].cache.value[0],
tmp.value[1], m_StartSegments[j].cache.value[1]);
if(tmp.last[0] != m_StartSegments[j].cache.last[0] ||
tmp.last[1] != m_StartSegments[j].cache.last[1])
wxLogError(wxT("StartSeg %d: last %d %d - %d %d "), j, tmp.last[0], m_StartSegments[j].cache.last[0],
tmp.last[1], m_StartSegments[j].cache.last[1]);
if (tmp.ptr != data + (intptr_t)m_StartSegments[j].cache.ptr)
wxLogError(wxT("StartSeg %d: ptr %p %p"), j, tmp.ptr, data + (intptr_t)m_StartSegments[j].cache.ptr);
}
}

for (unsigned j = 0; j < m_Channels; j++)
{
int old_value = GetSample(i, j);
int new_value = new_section.GetSample(i, j, &tmp);
if (old_value != new_value)
wxLogError(wxT("%d %d: %d %d"), i, j, old_value, new_value);
}
}

new_section.m_Data = NULL;

wxLogError(wxT("Compress: %d %d"), m_AllocSize, output_len);
#endif

m_Pool.Free(m_Data);
m_Data = data;
m_AllocSize = output_len;
m_Compressed = true;
m_IsCompressed = true;

m_Data = (unsigned char *)m_Pool.MoveToPool(m_Data, m_AllocSize);
if (m_Data == NULL)
Expand Down Expand Up @@ -1010,17 +967,17 @@ void GOAudioSection::InitStream(
stream->decode_call = GetDecodeBlockFunction(
m_Channels,
m_BitsPerSample,
m_Compressed,
m_IsCompressed,
stream->resample_coefs->interpolation,
false);
stream->end_decode_call = GetDecodeBlockFunction(
m_Channels,
m_BitsPerSample,
m_Compressed,
m_IsCompressed,
stream->resample_coefs->interpolation,
true);
stream->margin
= GetMargin(m_Compressed, stream->resample_coefs->interpolation);
= GetMargin(m_IsCompressed, stream->resample_coefs->interpolation);
assert(stream->margin <= MAX_READAHEAD);
stream->read_end = limited_diff(end.read_end, stream->margin);
stream->end_pos = end.end_pos - stream->margin;
Expand Down Expand Up @@ -1055,17 +1012,17 @@ void GOAudioSection::InitAlignedStream(
stream->decode_call = GetDecodeBlockFunction(
m_Channels,
m_BitsPerSample,
m_Compressed,
m_IsCompressed,
stream->resample_coefs->interpolation,
false);
stream->end_decode_call = GetDecodeBlockFunction(
m_Channels,
m_BitsPerSample,
m_Compressed,
m_IsCompressed,
stream->resample_coefs->interpolation,
true);
stream->margin
= GetMargin(m_Compressed, stream->resample_coefs->interpolation);
= GetMargin(m_IsCompressed, stream->resample_coefs->interpolation);
assert(stream->margin <= MAX_READAHEAD);
stream->read_end = limited_diff(end.read_end, stream->margin);
stream->end_pos = end.end_pos - stream->margin;
Expand Down Expand Up @@ -1095,7 +1052,7 @@ void GOAudioSection::GetHistory(
stream->audio_section->m_Channels,
stream->end_ptr);
} else {
if (!stream->audio_section->m_Compressed) {
if (!stream->audio_section->m_IsCompressed) {
for (unsigned i = 0; i < BLOCK_HISTORY; i++)
for (unsigned j = 0; j < stream->audio_section->m_Channels; j++)
history[i][j] = GetSampleData(
Expand Down
15 changes: 9 additions & 6 deletions src/grandorgue/sound/GOSoundAudioSection.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,13 @@ class GOAudioSection {
unsigned m_SampleRate;

/* Type of the data which is stored in the data pointer */
unsigned m_Compressed;
unsigned m_BitsPerSample;
unsigned m_BytesPerSample;
unsigned m_Channels;

int8_t m_SampleGroup;
bool m_IsCompressed;

/* Size of the section in BYTES */
GOMemoryPool &m_Pool;
unsigned m_AllocSize;
Expand All @@ -174,7 +176,9 @@ class GOAudioSection {
GOAudioSection(GOMemoryPool &pool);
~GOAudioSection();
void ClearData();
unsigned GetChannels() const;
inline unsigned GetChannels() const { return m_Channels; }
inline int8_t GetSampleGroup() const { return m_SampleGroup; }

unsigned GetBytesPerSample() const;
unsigned GetLength() const;
unsigned GetReleaseCrossfadeLength() const {
Expand Down Expand Up @@ -212,6 +216,7 @@ class GOAudioSection {
unsigned pcm_data_sample_rate,
unsigned pcm_data_nb_samples,
const std::vector<GOWaveLoop> *loop_points,
int8_t sampleGroup,
bool compress,
unsigned loopCrossfadeLength,
unsigned releaseCrossfadeLength);
Expand Down Expand Up @@ -246,10 +251,8 @@ class GOAudioSection {
GOSampleStatistic GetStatistic();
};

inline unsigned GOAudioSection::GetChannels() const { return m_Channels; }

inline unsigned GOAudioSection::GetBytesPerSample() const {
return (m_Compressed) ? 0 : (m_BitsPerSample / 8);
return (m_IsCompressed) ? 0 : (m_BitsPerSample / 8);
}

inline unsigned GOAudioSection::GetLength() const { return m_SampleCount; }
Expand Down Expand Up @@ -283,7 +286,7 @@ inline int GOAudioSection::GetSampleData(

inline int GOAudioSection::GetSample(
unsigned position, unsigned channel, DecompressionCache *cache) const {
if (!m_Compressed) {
if (!m_IsCompressed) {
return GetSampleData(
position, channel, m_BitsPerSample, m_Channels, m_Data);
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/grandorgue/sound/GOSoundEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ void GOSoundEngine::NextPeriod() {

GOSoundSampler *GOSoundEngine::StartSample(
const GOSoundProvider *pipe,
int sampler_group_id,
int8_t sampler_group_id,
unsigned audio_group,
unsigned velocity,
unsigned delay,
Expand Down Expand Up @@ -471,7 +471,8 @@ void GOSoundEngine::CreateReleaseSampler(GOSoundSampler *handle) {
* zero. */
const GOSoundProvider *this_pipe = handle->pipe;
const GOAudioSection *release_section = this_pipe->GetRelease(
&handle->stream, ((double)(m_CurrentTime - handle->time)) / m_SampleRate);
handle->stream.audio_section->GetSampleGroup(),
((double)(m_CurrentTime - handle->time)) / m_SampleRate);
unsigned cross_fade_len = release_section
? release_section->GetReleaseCrossfadeLength()
: this_pipe->GetAttackSwitchCrossfadeLength();
Expand Down
2 changes: 1 addition & 1 deletion src/grandorgue/sound/GOSoundEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class GOSoundEngine {

GOSoundSampler *StartSample(
const GOSoundProvider *pipe,
int sampler_group_id,
int8_t sampler_group_id,
unsigned audio_group,
unsigned velocity,
unsigned delay,
Expand Down
20 changes: 10 additions & 10 deletions src/grandorgue/sound/GOSoundProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "loader/cache/GOCache.h"
#include "loader/cache/GOCacheWriter.h"

#include "GOHash.h"
#include "GOMemoryPool.h"
#include "GOSampleStatistic.h"
#include "GOSoundAudioSection.h"
Expand All @@ -25,6 +26,11 @@
} \
} while (0)

void GOSoundProvider::UpdateCacheHash(GOHash &hash) {
hash.Update(sizeof(AttackSelector));
hash.Update(sizeof(ReleaseSelector));
}

GOSoundProvider::GOSoundProvider()
: m_MidiKeyNumber(0),
m_MidiPitchFract(0),
Expand Down Expand Up @@ -63,7 +69,7 @@ bool GOSoundProvider::LoadCache(GOMemoryPool &pool, GOCache &cache) {
if (!cache.Read(&attacks, sizeof(attacks)))
return false;
for (unsigned i = 0; i < attacks; i++) {
attack_section_info info;
AttackSelector info;
if (!cache.Read(&info, sizeof(info)))
return false;
m_AttackInfo.push_back(info);
Expand All @@ -76,7 +82,7 @@ bool GOSoundProvider::LoadCache(GOMemoryPool &pool, GOCache &cache) {
if (!cache.Read(&releases, sizeof(releases)))
return false;
for (unsigned i = 0; i < releases; i++) {
release_section_info info;
ReleaseSelector info;
if (!cache.Read(&info, sizeof(info)))
return false;
m_ReleaseInfo.push_back(info);
Expand Down Expand Up @@ -213,20 +219,14 @@ const GOAudioSection *GOSoundProvider::GetAttack(
}

const GOAudioSection *GOSoundProvider::GetRelease(
const audio_section_stream *handle, double playback_time) const {
unsigned attack_idx = 0;
uint8_t sampleGroup, double playback_time) const {
unsigned time = std::min(playback_time, 3600.0) * 1000;
for (unsigned i = 0; i < m_Attack.size(); i++) {
if (handle->audio_section == m_Attack[i])
attack_idx = i;
}

const unsigned x = abs(rand());
int best_match = -1;
for (unsigned i = 0; i < m_Release.size(); i++) {
const unsigned idx = (i + x) % m_Release.size();
if (
m_ReleaseInfo[idx].sample_group != m_AttackInfo[attack_idx].sample_group)
if (m_ReleaseInfo[idx].sample_group != sampleGroup)
continue;
if (m_ReleaseInfo[idx].max_playback_time < time)
continue;
Expand Down
Loading

0 comments on commit 2ac71d4

Please sign in to comment.