Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94236

Re: Fast 12 bit to 16 bit sample conversion?

Newsgroups comp.lang.python
Date 2015-07-20 08:14 -0700
References <d14a9iF9nfgU1@mid.individual.net>
Message-ID <df0a58cc-0ba1-405a-a43d-12bcb190c74d@googlegroups.com> (permalink)
Subject Re: Fast 12 bit to 16 bit sample conversion?
From edmondo.giovannozzi@gmail.com

Show all headers | View raw


Il giorno lunedì 20 luglio 2015 15:10:22 UTC+2, Peter Heitzer ha scritto:
> I am currently writing a python script to extract samples from old Roland 12 bit sample
> disks and save them as 16 bit wav files.
> 
> The samples are layouted as follows
> 
> 0 [S0 bit 11..4] [S0 bit 3..0|S1 bit 3..0] [S1 bit 11..4]
> 3 [S2 bit 11..4] [S2 bit 3..0|S3 bit 3..0] [S3 bit 11..4]
> 
> In other words 
> sample0=(data[0]<<4)|(data[1]>>4)
> sample1=(data[2]<<4)|(data[1] & 0x0f)
> 
> I use this code for the conversion (using the struct module)
> 
> import struct
> from array import array
> 
> def getWaveData(diskBuffer):
>   offset=0
>   words=array('H')
>   for i in range(len(diskBuffer)/3):
>     h0=struct.unpack_from('>h',diskBuffer,offset)
>     h1=struct.unpack_from('<h',diskBuffer,offset+1)
>     words.append(h0[0] & 0xfff0)
>     words.append(h1[0] & 0xfff0)
>     offset+=3
>   return words
> 
> I unpack the samples in an array of unsigned shorts for I later can use the byteswap() method
> if the code is running on a big endian machine.
> 
> What options using pure python do I have to make the conversion faster?
> I thought of unpacking more bytes at once e.g. using a format '>hxhxhxhx' for 4 even samples
> and '<xhxhxhxh' for 4 odd samples vice versa.
> Can I map the '& 0xfff0' to the whole array?

I'll try to read the binary data with numpy.fromfile, reshape the array in [n,3] matrix, and then you can operate with the columns to get what you want.
:-)
 

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Fast 12 bit to 16 bit sample conversion? "Peter Heitzer" <peter.heitzer@rz.uni-regensburg.de> - 2015-07-20 13:10 +0000
  Re: Fast 12 bit to 16 bit sample conversion? MRAB <python@mrabarnett.plus.com> - 2015-07-20 15:43 +0100
    Re: Fast 12 bit to 16 bit sample conversion? "Peter Heitzer" <peter.heitzer@rz.uni-regensburg.de> - 2015-07-20 15:23 +0000
  Re: Fast 12 bit to 16 bit sample conversion? edmondo.giovannozzi@gmail.com - 2015-07-20 08:14 -0700
  Re: Fast 12 bit to 16 bit sample conversion? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-07-21 03:45 +0100

csiph-web