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


Groups > comp.lang.python > #94250

Re: Fast 12 bit to 16 bit sample conversion?

Path csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.05; 'that?': 0.05; '(using': 0.07; 'endian': 0.09; 'module)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'throw': 0.09; 'unpack': 0.09; 'unpacking': 0.09; 'unsigned': 0.09; 'python': 0.10; 'files.': 0.13; 'def': 0.13; 'itertools': 0.16; 'lookups': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'recipes': 0.16; 'stdlib.': 0.16; 'subject:bit': 0.16; 'subject:sample': 0.16; 'xrange': 0.16; 'wrote:': 0.16; 'later': 0.16; 'bytes': 0.18; 'odd': 0.18; 'language': 0.19; 'assuming': 0.22; 'lawrence': 0.22; 'python"': 0.22; 'referring': 0.22; 'struct': 0.22; 'bit': 0.23; 'import': 0.24; 'words': 0.24; 'header:In-Reply-To:1': 0.24; 'script': 0.25; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'url:moin': 0.27; 'function': 0.28; 'follows': 0.29; 'array': 0.29; "i'm": 0.30; 'url:wiki': 0.30; 'code': 0.30; 'e.g.': 0.30; "i'd": 0.31; 'guess': 0.31; 'probably': 0.31; "can't": 0.32; 'language.': 0.32; 'options': 0.33; 'url:python': 0.33; 'extract': 0.33; 'machine.': 0.33; 'skip:h 40': 0.33; 'running': 0.34; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'url:org': 0.36; 'to:addr:python- list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'method': 0.37; 'received:org': 0.37; 'suggestion': 0.37; 'thought': 0.37; 'mean': 0.38; 'end': 0.39; 'why': 0.39; 'data': 0.39; 'format': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'save': 0.60; 'charset:windows-1252': 0.62; 'more': 0.63; 'sample': 0.63; 'times': 0.63; 'our': 0.64; 'grab': 0.64; 'pythonistas,': 0.84; 'subject:Fast': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Fast 12 bit to 16 bit sample conversion?
Date Tue, 21 Jul 2015 03:45:56 +0100
References <d14a9iF9nfgU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host-78-147-190-201.as13285.net
X-Mozilla-News-Host news://news.gmane.org
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0
In-Reply-To <d14a9iF9nfgU1@mid.individual.net>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.795.1437446777.3674.python-list@python.org> (permalink)
Lines 75
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1437446777 news.xs4all.nl 2821 [2001:888:2000:d::a6]:44266
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:94250

Show key headers only | View raw


On 20/07/2015 14:10, Peter Heitzer wrote:
> 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?

By "pure python" I'm assuming you mean part of the stdlib.

Referring to https://wiki.python.org/moin/PythonSpeed/PerformanceTips 
you could end with something like this (untested).

def getWaveData(diskBuffer):
     offset = 0
     words = array('H')
     wx = words.extend #saves two lookups and a function call
     su = struct.unpack_from #saves two lookups
     # 'i' not used in the loop so throw it away
     for _ in range(len(diskBuffer)/3): # use xrange on Python 2
         h0 = su('>h',diskBuffer,offset)
         h1 = su('<h',diskBuffer,offset+1)
         wx((h0[0] & 0xfff0), (h1[0] & 0xfff0)) # MRAB pointed out a 
problem with the masking in the second section???
         offset += 3
     return words

> 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.

If that reduces the number of times around the loop why not?  Combine it 
with MRAB's suggestion of lookups and I'd guess you'd get a speedup, but 
knowing Python I'm probably way out on that?  There's only one way to 
find out.

I'm also thinking that you could user one of the itertools functions or 
recipes to grab the data and hence simplify the loop even more, but it's 
now 3:45 BST, so I can't think straight, hence bed.

> Can I map the '& 0xfff0' to the whole array?

If it works :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

Back to comp.lang.python | Previous | NextPrevious 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