Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from ctypes import *
- import sys
- import os
- BE_CONFIG_MP3, BE_CONFIG_LAME = (0, 256)
- (BE_ERR_SUCCESSFUL, BE_ERR_INVALID_FORMAT, BE_ERR_INVALID_FORMAT_PARAMETERS,
- BE_ERR_NO_MORE_HANDLES, BE_ERR_INVALID_HANDLE, BE_ERR_BUFFER_TOO_SMALL) = range(6)
- BE_MAX_HOMEPAGE = 128
- BE_MP3_MODE_STEREO, BE_MP3_MODE_JSTEREO, BE_MP3_MODE_DUALCHANNEL, BE_MP3_MODE_MONO = range(4)
- MPEG2, MPEG1 = range(2)
- CURRENT_STRUCT_VERSION = 1
- #CURRENT_STRUCT_SIZE sizeof(BE_CONFIG) # is currently 331 bytes
- VBR_METHOD_NONE, VBR_METHOD_DEFAULT, VBR_METHOD_OLD, VBR_METHOD_NEW, VBR_METHOD_MTRH, VBR_METHOD_ABR = range(-1, 5)
- LQP_PHONE, LQP_SW, LQP_AM, LQP_FM, LQP_VOICE, LQP_RADIO, LQP_TAPE, LQP_HIFI, LQP_CD, LQP_STUDIO = [x * 1000 for x in range(1, 11)]
- class BE_VERSION(Structure):
- _fields_ = [
- ('byDLLMajorVersion', c_byte), # BladeEnc DLL Version number
- ('byDLLMinorVersion', c_byte),
- ('byMajorVersion', c_byte), # BladeEnc Engine Version Number
- ('byMinorVersion', c_byte),
- ('byDay', c_byte), # DLL Release date
- ('byMonth', c_byte),
- ('wYear', c_short),
- ('zHomepage', c_char * 129), # BladeEnc Homepage URL
- ('byAlphaLevel', c_byte),
- ('byBetaLevel', c_byte),
- ('byMMXEnabled', c_byte),
- ('btReserved', c_byte * 125)
- ]
- _pack_ = 1
- class BE_CONFIG(Structure):
- _fields_ = [
- ('dwConfig', c_ulong), # BE_CONFIG_XXXXX, Currently only BE_CONFIG_MP3 is supported
- ('dwStructVersion', c_ulong),
- ('dwStructSize', c_ulong),
- ('dwSampleRate', c_ulong), # SAMPLERATE OF INPUT FILE
- ('dwReSampleRate', c_ulong), # DOWNSAMPLERATE, 0=ENCODER DECIDES
- ('nMode', c_long), # BE_MP3_MODE_STEREO, BE_MP3_MODE_DUALCHANNEL, BE_MP3_MODE_MONO
- ('dwBitrate', c_ulong), # CBR bitrate, VBR min bitrate
- ('dwMaxBitrate', c_ulong), # CBR ignored, VBR Max bitrate
- ('nPreset', c_long), # Quality preset, use one of the settings of the LAME_QUALITY_PRESET enum
- ('dwMpegVersion', c_ulong), # FUTURE USE, MPEG-1 OR MPEG-2
- ('dwPsyModel', c_ulong), # FUTURE USE, SET TO 0
- ('dwEmphasis', c_ulong), # FUTURE USE, SET TO 0
- # BIT STREAM SETTINGS
- ('bPrivate', c_long), #bool Set Private Bit (True/FALSE)
- ('bCRC', c_long), # bool Insert CRC (True/FALSE)
- ('bCopyright', c_long), # bool Set Copyright Bit (True/FALSE)
- ('bOriginal', c_long), # bool Set Original Bit (True/FALSE)
- # VBR STUFF
- ('bWriteVBRHeader', c_long), # bool WRITE XING VBR HEADER (True/FALSE)
- ('bEnableVBR', c_long), #bool USE VBR ENCODING (True/FALSE)
- ('nVBRQuality', c_int), # VBR QUALITY 0..9
- ('dwVbrAbr_bps', c_ulong), # Use ABR instead of nVBRQuality
- # CHECK THIS!!!!
- ('nVbrMethod', c_int),
- ('bNoRes', c_long), # bool Disable Bit reservoir (True/FALSE)
- # MISC SETTINGS
- ('bStrictIso', c_long), # Use strict ISO encoding rules (True/FALSE)
- ('nQuality', c_ushort), # Quality Setting, HIGH BYTE should be NOT LOW byte, otherwhise quality=5
- # FUTURE USE, SET TO 0, align strucutre to 331 bytes
- ('btReserved', c_byte * (255 - 4*4 - 2 ) )
- ]
- _packed_ = 1
- lame = cdll.lame_enc
- #version = BE_VERSION()
- #print version.zHomepage
- #lame.beVersion(byref(version))
- #print version.byDay, version.byMonth
- beConfig = BE_CONFIG()
- print sizeof(c_int)
- print sizeof(beConfig)
- beConfig.dwConfig = BE_CONFIG_LAME;
- beConfig.dwStructVersion = 1
- beConfig.dwStructSize = sizeof(beConfig)
- beConfig.dwSampleRate = 22050 # INPUT FREQUENCY
- beConfig.dwReSampleRate = 0 # LET ENCODER DECIDE
- beConfig.nMode = BE_MP3_MODE_MONO # OUTPUT IN STREO
- beConfig.dwBitrate = 128 # MINIMUM BIT RATE
- beConfig.nPreset = LQP_FM # QUALITY PRESET SETTING
- beConfig.dwMpegVersion = MPEG1 # MPEG VERSION (I or II)
- beConfig.dwPsyModel = 0 # USE DEFAULT PSYCHOACOUSTIC MODEL
- beConfig.dwEmphasis = 0 # NO EMPHASIS TURNED ON
- beConfig.bOriginal = True # SET ORIGINAL FLAG
- beConfig.dwMaxBitrate = 320 # MAXIMUM BIT RATE
- beConfig.bCRC = True # INSERT CRC
- beConfig.bCopyright = True # SET COPYRIGHT FLAG
- beConfig.bPrivate = True # SET PRIVATE FLAG
- beConfig.bWriteVBRHeader = False # YES, WRITE THE XING VBR HEADER
- beConfig.bEnableVBR = False # USE VBR
- beConfig.nVBRQuality = 5 # SET VBR QUALITY
- beConfig.bNoRes = True # No Bit reservoir
- beConfig.nVBRMethod = VBR_METHOD_NONE
- inBufSize = c_ulong()
- outBufSize = c_ulong()
- stream = c_int()
- result = lame.beInitStream(byref(beConfig), byref(inBufSize), byref(outBufSize), byref(stream))
- if result != BE_ERR_SUCCESSFUL:
- print 'Error', result
- sys.exit(0)
- print inBufSize, outBufSize, stream
- outBuf = create_string_buffer(outBufSize.value)
- # test.wav encoded at 22050, mono
- inFile = open('test.wav', 'rb')
- inFile.seek(0, os.SEEK_END)
- wavFileSize = inFile.tell()
- inFile.seek(44) # skip wav header
- outFile = open('test.mp3', 'wb')
- while(1):
- inBytes = inFile.read(inBufSize.value * 2)
- if inBytes == '':
- break
- print len(inBytes)
- inBuf = create_string_buffer(inBytes, inBufSize.value * 2)
- nBytesOut = c_ulong()
- err = lame.beEncodeChunk(stream, len(inBytes), inBuf, outBuf, byref(nBytesOut) )
- if err != 0:
- break
- outFile.write(outBuf[0:nBytesOut.value])
- err = lame.beDeinitStream(stream, outBuf, byref(nBytesOut));
- if err == 0 and nBytesOut != 0:
- outFile.write(outBuf[0:nBytesOut.value])
- lame.beCloseStream(stream)
- outFile.close()
- inFile.close()
Advertisement
Add Comment
Please, Sign In to add comment