Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(using': 0.07; 'endian': 0.09; 'module)': 0.09; 'unpack': 0.09; 'unpacking': 0.09; 'unsigned': 0.09; 'python': 0.10; 'files.': 0.13; 'def': 0.13; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'numpy': 0.16; 'received:192.168.1.4': 0.16; 'subject:bit': 0.16; 'subject:sample': 0.16; 'wrote:': 0.16; 'later': 0.16; 'bytes': 0.18; 'do.': 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; 'values': 0.28; 'follows': 0.29; 'array': 0.29; 'code': 0.30; 'e.g.': 0.30; 'good.': 0.32; 'options': 0.33; 'extract': 0.33; 'machine.': 0.33; 'skip:h 40': 0.33; 'running': 0.34; 'gives': 0.35; 'could': 0.35; 'something': 0.35; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'thought': 0.37; 'format': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'save': 0.60; 'more': 0.63; 'sample': 0.63; 'subject:Fast': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=JOrGyJ+b c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=JAI3OqB5mnwA:10 a=IkcTkHD0fZMA:10 a=YAw_5V1dDyJwF2YwCuoA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Subject: Re: Fast 12 bit to 16 bit sample conversion? To: python-list@python.org References: From: MRAB Date: Mon, 20 Jul 2015 15:43:16 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437403400 news.xs4all.nl 2931 [2001:888:2000:d::a6]:58356 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94235 On 2015-07-20 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): If the 2 12-bit values are [0xABC, 0xDEF], the bytes will be [0xAB, 0xCF, 0xDE]. > h0=struct.unpack_from('>h',diskBuffer,offset) This gives 0xABCF, which is ANDed to give 0xABC0. Good. > h1=struct.unpack_from(' 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 ' Can I map the '& 0xfff0' to the whole array? > That's something the numpy could do.