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

scipy.io.wavfile should be able to read 24 bit signed wave (Trac #1405) #1930

Closed
scipy-gitbot opened this issue Apr 25, 2013 · 18 comments · Fixed by #12287
Closed

scipy.io.wavfile should be able to read 24 bit signed wave (Trac #1405) #1930

scipy-gitbot opened this issue Apr 25, 2013 · 18 comments · Fixed by #12287
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected Migrated from Trac scipy.io
Milestone

Comments

@scipy-gitbot
Copy link

Original ticket http://projects.scipy.org/scipy/ticket/1405 on 2011-03-10 by trac user tuxicoman, assigned to unknown.

So far the scipy.io.wavfile module cannot read 24 bit wave files whereas it's quite a popular format.

After looking to source code, it seems that because there i no numpy.int24 type; the code fails on 24bit wave file. The function crashes on following line :

data = numpy.fromfile(fid, dtype=dtype, count=size//bytes)

with dtype = "<i3" :-(

Because 24bit is stored on 3 bytes, scipy.io.wavfile needs additional code to slice and convert filebytes into an 32bit type array for example. Unfortunately, even after googling, I'm not enough talented in python/numpy/wavfile to code this. So i file this ticket for someone else to code it. Thanks.

@scipy-gitbot
Copy link
Author

@stefanv wrote on 2011-03-10

Could you also attach a (very short) .wav file of the required format? That would help with debugging. Thanks!

@scipy-gitbot
Copy link
Author

Attachment added by trac user tuxicoman on 2011-03-10: 24test.wav

@scipy-gitbot
Copy link
Author

trac user tuxicoman wrote on 2011-03-10

Sure, this is one i've done with audacity.
16bit and 32bit versions of this file (created through audacity also) work.

@scipy-gitbot
Copy link
Author

Milestone changed to Unscheduled by @WarrenWeckesser on 2011-12-29

@WarrenWeckesser
Copy link
Member

The gist https://gist.github.com/WarrenWeckesser/7461696 contains a couple functions for generating wav files with a 3 byte sample width. Something like this could be used for unit tests if support for 24 bit files is ever added.

@WarrenWeckesser
Copy link
Member

For anyone who needs to read 24 bit files: I created a gist containing the function readwav that can read (uncompressed) 24 bit wav files: https://gist.github.com/WarrenWeckesser/7461781

Update: The gist now includes a function called writewav24 for creating 24 bit wav files.

Update 2: I moved the gist to a regular github repository at https://github.com/WarrenWeckesser/wavio

@matthew-brett
Copy link
Contributor

Warren - OK to add this to scipy?

@WarrenWeckesser
Copy link
Member

@matthew-brett: Sure, that would be great.

@damnsavage
Copy link

Can we add this code from Warren? This is essentially what is required

data = numpy.fromfile(fid, dtype='u1', count=size) # first read byte per byte
a = numpy.empty((len(data)/3, 4), dtype=`u1`)
a[:, :3] = data.reshape((-1, 3))
a[:, 3:] = (a[:, 3 - 1:3] >> 7) * 255
data = a.view('<i4').reshape(a.shape[:-1])

24bit wav format is very common, unfortunately it's not possible to attach a sample to this post...

@rgommers
Copy link
Member

@Snotzer if that fixes the issue (I didn't check), then I guess the answer is yes. PR welcome I'd say. Would need to include a small .wav file for a unit test, similar to the ones that are already there now in scipy/io/tests/data/.

@gozzilli
Copy link

Including a 24bit wav file similar to the one in scipy/io/tests/data
test-44100-le-1ch-3bytes.wav

Input File     : 'test-44100-le-1ch-3bytes.wav'
Channels       : 1
Sample Rate    : 44100
Precision      : 24-bit
Duration       : 00:00:00.10 = 4418 samples = 7.51361 CDDA sectors
File Size      : 13.3k
Bit Rate       : 1.06M
Sample Encoding: 24-bit Signed Integer PCM

@endolith
Copy link
Member

3-bytes per sample is only possible for the case of mmap=False, correct? Since numpy has no 3-byte data type?

Note that WAV files can have any number of bits per sample. 1-8 bits are interpreted as unsigned, 9 or more are signed. Adobe Audition can output 20-bit files, for instance, starting like so:

52 49 46 46 2A 43 00 00 57 41 56 45 66 6D 74 20 
10 00 00 00 01 00 01 00 80 BB 00 00 80 32 02 00 
03 00 14 00 64 61 74 61
      ^^^^^

      0x0014 = 20-bit

It is still stored as 3 bytes per sample, though.

I suggested in #5990 that it be possible to convert all different formats into floating point normalized from -1 to +1, the way scikits.audiolab does, for when you just want to process the signal and don't care what format it was stored in. Also if not normalized, it should have an option to output the bit depth.

@rgommers
Copy link
Member

gh-6849 has a link to an implementation. The author doesn't want to make the time to do a PR though, so if someone wants to have a look at that that'd be great.

@patrickmmartin
Copy link

I encountered this for myself - my DAWS software records in 24-bit WAV files so it would be convenient for me just to reading

patrickmmartin/AxesXplained#7

@jeremycochoy
Copy link

Is the support of 24-bits wav available in a recent release of scipy, or is it still work in progress since 2014?

@endolith
Copy link
Member

endolith commented Aug 6, 2019

@jeremycochoy It's still a work in progress, in PR #6852. Can you use PySoundFile?

@WarrenWeckesser
Copy link
Member

FYI: The gist that I created back in 2013 has since been converted to a package called wavio that is available on PyPI: https://pypi.org/project/wavio/

@giacaglia
Copy link

giacaglia commented Dec 9, 2019

Another way of going around this is to change the input's bit depth using a tool like ffmpeg. The following should help:
fmpeg -i input.wav -sample_fmt s16 output.wav

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected Migrated from Trac scipy.io
Projects
None yet
Development

Successfully merging a pull request may close this issue.