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

Writing floating point samples to a wav file #46

Closed
adriano-vilela opened this issue Jun 3, 2017 · 4 comments
Closed

Writing floating point samples to a wav file #46

adriano-vilela opened this issue Jun 3, 2017 · 4 comments

Comments

@adriano-vilela
Copy link

This is actually a question, not an issue...

I'm a bit confused about the second table in the wavwrite section of the documentation (the table about floating point values). What does the "Output Format" column of this table mean? For example, based on the second row of that table, I thought that if I passed a vector of doubles (y data) to the wavwrite function with nbits=16, the function would convert the y data to 16 bit integers, mapping the y data range to the range -32768:32767 before writing the data to the output file. However, if I try to do that, I get the following error message:

ERROR: 16 bits is not supported for WAVE_FORMAT_IEEE_FLOAT.

Of course I can do the conversion to Int16 myself before calling wavwrite, but in that case I don't know what the last column of the table means. Right after the table, one can read:

"Floating point (single and double precision) values are written to the file unaltered. The library will not modify the data range or representation."

This suggests that I do have to do the conversion myself before calling wavwrite...

@dancasimiro
Copy link
Owner

The "Output Format" is the type of data saved in the file. To convert the input floating point data to 16 bit PCM, you need to specify a compression value of WAV_FORMAT_PCM (and set nbits to 16).

Something like:

wavwrite(y, filename, nbits=16, compression=WAVE_FORMAT_PCM)

@dancasimiro
Copy link
Owner

The error that you received:

ERROR: 16 bits is not supported for WAVE_FORMAT_IEEE_FLOAT.

is because no one has implemented support for half width (16 bit) floats. It should be possible now that Julia has support for the type.

@adriano-vilela
Copy link
Author

Thank you, that works.

Coming from Matlab, I just assumed that if I passed nbits=16 or nbits=8 the conversion would be done automatically, without the need to explicitly do compression=WAVE_FORMAT_PCM. Maybe this could be made clearer in the documentation.

Question: would it be interesting to automatically determine the compression scheme from nbits? This seems possible based on the tables in the wavwrite section of the documentation:

nbits = {8, 16, 24} -> compression=WAVE_FORMAT_PCM
nbits = 32 -> compression=WAVE_FORMAT_IEEE_FLOAT

Though I just checked and it is also possible to use compression=WAVE_FORMAT_PCM for nbits=32. In this case (nbits=32), the choice between WAVE_FORMAT_PCM and WAVE_FORMAT_IEEE_FLOAT could be made based on the y data type (integer vs single or double). Obviously, I'm talking about default values; the user should be able to override this by explicitly providing a value for the compression argument.

Am I missing something here?

@dancasimiro
Copy link
Owner

There is some code that implements part of your request. In practice, I find it a bit too subtle. Generally, I want to choose how the samples are encoded in the file.

Despite the questionable usefulness, do you have an suggestions to make the feature easier to find?

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

2 participants