def trimwavfile(wavfilename, wavoutfilename, start, duration=None, gain=None): with file(wavfilename, 'rb') as f: # read in the headers headers = f.read(20) subchunk1size = common.LSBUint32(headers[16:20]) headers += f.read(subchunk1size) subchunk2id = f.read(4) subchunk2size = common.LSBUint32(f.read(4)) # check for a PCM file format if headers[:4]!='RIFF' or headers[8:12]!='WAVE' or \ headers[12:16]!='fmt ' or common.LSBUint16(headers[20:22])!=1: # not a PCM file raise TypeError subchunk2start = 20 + subchunk1size subchunk2datastart = subchunk2start + 8 samplerate = common.LSBUint32(headers[24:28]) blockalign = common.LSBUint16(headers[32:34]) # compute new start & duration new_start = int(start * samplerate) * blockalign new_size = subchunk2size - new_start if duration is not None: i = int(duration * samplerate) * blockalign if i < new_size: new_size = i # go get it f.seek(new_start, 1) open(wavoutfilename, 'wb').write("".join([ 'RIFF', common.LSBstr32(4 + 8 + subchunk1size + 8 + new_size), headers[8:], 'data', common.LSBstr32(new_size), f.read(new_size) ])) if gain is not None: adjustwavfilevolume(wavoutfilename, gain)
def trimwavdata(wavedatain, start, duration=None): # check for a PCM file format if wavedatain[:4]!='RIFF' or wavedatain[8:12]!='WAVE' or \ wavedatain[12:16]!='fmt ' or common.LSBUint16(wavedatain[20:22])!=1: raise ValueError, 'not a PCM file' subchunk1size = common.LSBUint32(wavedatain[16:20]) subchunk2start = 20 + subchunk1size subchunk2size = common.LSBUint32(wavedatain[subchunk2start + 4:subchunk2start + 8]) subchunk2datastart = subchunk2start + 8 samplerate = common.LSBUint32(wavedatain[24:28]) blockalign = common.LSBUint16(wavedatain[32:34]) # compute new start & duration new_start = int(start * samplerate) * blockalign newsubchunk2datastart = subchunk2datastart + new_start new_size = subchunk2size - new_start if duration is not None: i = int(duration * samplerate) * blockalign if i < new_size: new_size = i # return new data return 'RIFF'+common.LSBstr32(4+8+subchunk1size+8+new_size)+\ wavedatain[8:subchunk2start]+\ 'data'+common.LSBstr32(new_size)+\ wavedatain[newsubchunk2datastart:newsubchunk2datastart+new_size]
def adjustwavfilevolume(wavfilename, gain): """ Ajdust the volume of a wav file. """ with file(wavfilename, 'rb') as f: # read in the headers headers = f.read(20) subchunk1size = common.LSBUint32(headers[16:20]) headers += f.read(subchunk1size) headers += f.read(8) # 4 byte ID and 4 byte length subchunk2size = common.LSBUint32(headers[-4:]) bitspersample = common.LSBUint16(headers[34:36]) if bitspersample != 16: print 'Volume adjustment only works with 16-bit wav file', bitspersample return sample_num = subchunk2size / 2 # always 16-bit per channel per sample temp_name = common.gettempfilename("wav") with file(temp_name, 'wb') as f_temp: f_temp.write(headers) delta = pow(10.0, (gain / 10.0)) for i in range(sample_num): d = int(struct.unpack('<h', f.read(2))[0] * delta) if d > 32767: d = 32767 elif d < -32768: d = -32768 f_temp.write(struct.pack('<h', d)) os.remove(wavfilename) os.rename(temp_name, wavfilename)
def convertbmptolgbit(bmp_data): """Takes a BMP image file and returns BIT image file (LG proprietary) @param bit_data: 8BPP or 24BPP BMP image file data @return: 16BPP LGBIT image file data """ # This function only exists for the LG proprietary images (wallpaper, etc.) # on the LG-VX3200. if bmp_data[0:2] != 'BM': return None width = common.LSBUint32(bmp_data[18:22]) height = common.LSBUint32(bmp_data[22:26]) offset = common.LSBUint32(bmp_data[10:14]) bpp = common.LSBUint16(bmp_data[28:30]) img = common.LSBstr16(width) img += common.LSBstr16(height) # Now on to the char data if bpp == 8: # 8BPP (paletted) BMP data palette = bmp_data[54:54 + 256 * 4] for h in range(height): for w in range(width): bitind = (height - h - 1) * width + w + offset palind = ord(bmp_data[bitind:bitind + 1]) * 4 blue = ord(palette[palind:palind + 1]) green = ord(palette[palind + 1:palind + 2]) red = ord(palette[palind + 2:palind + 3]) bitval = ((red & 0xf8) << 8) | ((green & 0xfc) << 3) | ( (blue & 0xf8) >> 3) img += common.LSBstr16(bitval) elif bpp == 24: # 24BPP (non-paletted) BMP data for h in range(height): for w in range(width): bitind = (height - h - 1) * width * 3 + (w * 3) + offset blue = ord(bmp_data[bitind:bitind + 1]) green = ord(bmp_data[bitind + 1:bitind + 2]) red = ord(bmp_data[bitind + 2:bitind + 3]) bitval = ((red & 0xf8) << 8) | ((green & 0xfc) << 3) | ( (blue & 0xf8) >> 3) img += common.LSBstr16(bitval) else: return None return img