Thank you very much, lithopsian, for your insightful clarification!
I corrected my code in the light of your information and was able to produce
the expected results instantly.
For anyone who may be unsure about this, I add a brief description:
I completely missed the crucial point that each unpacked int32_t sample contains
just one original 16 bit sample. I silently supposed (erroneously) a bijection between
original WAV-samples (==32bit) and unpacked samples (==32bit).
This misunderstanding resulted in me respecting only the first half of the unpacked
samples without noticing! (It seemed all innocent, since WavpackGetNumSamples() returned
exactly the same number as the total PCM sample count of the original WAV file. All seemed
fine.)
The following processing produced the correct results.
(Disclaimer: just demo code for the logic!)
uint32_t samples_per_block = // ... number of 32bit samples to read as a block
std::vector<uint32_t> u_samples(samples_per_block); // the samples I need
std::vector <int32_t> s_samples(samples_per_block * 2); // the samples I get
// The number of unpacked signed samples is samples_per_block * number of channels,
// where the multiplier in my case is 2 for stereo
samples_read = WavpackUnpackSamples(ctx, &s_samples[0], samples_per_block);
// The number of samples requested to be unpacked is the original value of
// samples_per_block, since "sample" in this context means 1 sample per channel
// for each channel. ctx is my WavpackContext pointer.
if (samples_read != current_block_size)
{
// (...) read not the expected amount, cope with situation or return
}
// (...) do some sanity checks
// The unpacked samples are now in s_samples. We convert them to their
// intended representation to u_samples.
for (int i = 0; i < u_samples->size(); ++i) // traverse destination samples
{
u_samples[i] = (s_samples[i*2+1] & 0x0000FFFF) << 16; // WAV-sample i, add left channel on upper bytes
u_samples[i] |= (s_samples[i*2 ] & 0x0000FFFF); // WAV-sample i, add right channel on lower bytes
}
As you pointed out, the left channel has to be shifted to the upper bytes of the
destination sample. That is probably, what the Wavpack 5 Library Documentation
meant, when hinting me to left-shifting (page 9, explaining WavpackGetBytesPerSample()).
Obviously, this was too subtle for me.