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 __mms_header(self, img_name, img_type, img_data_len): if len(img_name) >= self.__mms_max_file_name_len: name = img_name[:self.__mms_max_file_name_len - 1] else: name = img_name return str('\x06'+str(name)+\ '\x00'*(self.__mms_max_file_name_len-len(name))+\ common.LSBstr32(img_data_len)+chr(img_type)+\ '\x00'*11)
def convertlgbittobmp(bit_data): """Takes a BIT image file (LG proprietary) and returns BMP @param bit_data: 16BPP BIT image file data @return: 24BPP BMP image file data """ width = common.LSBUint16(bit_data[0:2]) height = common.LSBUint16(bit_data[2:4]) img = 'BM' img += common.LSBstr32(width * height * 3 + 54) # file size img += common.LSBstr16(0) # unused img += common.LSBstr16(0) # unused img += common.LSBstr32(54) # offset to pixel data (from byte 0) img += common.LSBstr32(40) # info section size img += common.LSBstr32(width) # image width img += common.LSBstr32(height) # image height img += common.LSBstr16(1) # image planes img += common.LSBstr16(24) # bits-per-pixel img += common.LSBstr32(0) # compression type (0=uncompressed) img += common.LSBstr32(0) # image size (may be 0 for uncompressed images) img += common.LSBstr32(0) # (ignored) img += common.LSBstr32(0) # (ignored) img += common.LSBstr32(0) # (ignored) img += common.LSBstr32(0) # (ignored) # Now on to the char data for h in range(height): for w in range(width): # images can be zero len on phone if len(bit_data) == 0: bitdata = 0xffff else: bitind = (height - h - 1) * width * 2 + (w * 2) + 4 bitdata = common.LSBUint16(bit_data[bitind:bitind + 2]) red = (bitdata & 0xf800) >> 8 green = (bitdata & 0x07e0) >> 3 blue = (bitdata & 0x001f) << 3 if (red & 0x8) != 0: red = red | 0x7 if (green & 0x4) != 0: green = green | 0x3 if (blue & 0x8) != 0: blue = blue | 0x7 img += chr(blue) img += chr(green) img += chr(red) return img