Skip to content

Non-contiguous memory layout in stereo input causes corrupted output #3

@joris-vaneyghen

Description

@joris-vaneyghen

When processing stereo audio input where the NumPy array has a non-C-contiguous memory layout (commonly occurring after loading with librosa), the processed output becomes corrupted or garbled. This appears to be a memory layout compatibility issue between the Python/NumPy interface and the underlying C++ processing code.

The C++ processing code expects arrays with C-contiguous memory layout, but librosa-loaded arrays may have different memory stride patterns or non-contiguous layouts that are incompatible.

Suggested Solutions:

Ideal: The process() method should automatically handle memory layout conversion internally
Alternative: Clearly document this requirement with examples in the documentation

Code to reproduce + FIX

import numpy as np
import librosa
import python_stretch as ps

# Load a stereo audio example from librosa (e.g., 'trumpet', 'brahms',...)
audio, sr = librosa.load(librosa.ex('trumpet', hq=True), sr=None, mono=False)

#fix memory layout issue
audio_fixed = np.ascontiguousarray(audio)

print("audio shape:", audio.shape, "dtype:", audio.dtype)
print("audio_fixed shape:", audio_fixed.shape, "dtype:", audio_fixed.dtype)
print("audio flags:", audio.flags)
print("audio_fixed flags:", audio_fixed.flags)
print("Arrays equal:", np.array_equal(audio, audio_fixed))
print("Memory layout same:", audio.flags['C_CONTIGUOUS'] == audio_fixed.flags['C_CONTIGUOUS'])


# Create a Stretch object
stretch = ps.Signalsmith.Stretch()
# Configure using a preset
stretch.preset(audio.shape[0], sr) # numChannels, sampleRate
# Shift up by one octave
stretch.setTransposeSemitones(12)
# Stretch time
stretch.timeFactor = 0.75

# Process
audio_processed = stretch.process(audio)
audio_fixed_processed = stretch.process(audio_fixed)

# Save and listen
import soundfile as sf
sf.write("audio_original.wav", audio.T, sr)
sf.write("audio_processed.wav", audio_processed.T, sr)
sf.write("audio_fixed_processed.wav", audio_fixed_processed.T, sr)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions