Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AiffFileWriter.WriteSample() fails to write 32-bit samples correctly #1179

Open
karip opened this issue Aug 15, 2024 · 0 comments
Open

AiffFileWriter.WriteSample() fails to write 32-bit samples correctly #1179

karip opened this issue Aug 15, 2024 · 0 comments

Comments

@karip
Copy link

karip commented Aug 15, 2024

I'm trying to write 32-bit samples using AiffFileWriter. Here's the code for it:

WaveFormat format = WaveFormat.CreateCustomFormat(WaveFormatEncoding.Extensible, 44100, 1, 0, 0, 32);
AiffFileWriter writer = new AiffFileWriter("output.aiff", format);
for (int i = -100; i <= 100; i++)
{
    writer.WriteSample(((float)i) / 100.0f);
}
writer.Dispose();

The resulting file doesn't have correct samples. It's just filled with zeros.

If I change the code to write 16-bit samples: WaveFormat format = new WaveFormat(44100, 16, 1);, then output.aiff looks correct.

I had a look at the code for AiffFileWriter and I don't understand how it could work for 32-bit output. It is converting a floating point value in the range [-1.0, 1.0] to Int32 by casting it: (Int32)sample. It means that the values become integer values -1, 0 or 1. Shouldn't the conversion be something like this: (Int32)(Int32.MaxValue * sample)?

Also, should the error message in AiffFileWriter.WriteSample() be changed from "Only 16, 24 or 32 bit PCM or IEEE float audio data supported" to "Only 16, 24 or 32 bit PCM audio data supported"? I thought it supports "IEEE float audio data", so I was trying to use WaveFormat format = WaveFormat.CreateIeeeFloatWaveFormat(44100, 1) but it just gave an error message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant