Guest User

Attempt using lame encoder from Python

a guest
Dec 17th, 2011
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.13 KB | None | 0 0
  1. from ctypes import *
  2. import sys
  3. import os
  4.  
  5.  
  6. BE_CONFIG_MP3, BE_CONFIG_LAME = (0, 256)
  7.  
  8. (BE_ERR_SUCCESSFUL, BE_ERR_INVALID_FORMAT, BE_ERR_INVALID_FORMAT_PARAMETERS,
  9.       BE_ERR_NO_MORE_HANDLES, BE_ERR_INVALID_HANDLE, BE_ERR_BUFFER_TOO_SMALL) = range(6)
  10.  
  11. BE_MAX_HOMEPAGE = 128
  12. BE_MP3_MODE_STEREO, BE_MP3_MODE_JSTEREO, BE_MP3_MODE_DUALCHANNEL, BE_MP3_MODE_MONO = range(4)
  13. MPEG2, MPEG1 = range(2)
  14.  
  15. CURRENT_STRUCT_VERSION = 1
  16. #CURRENT_STRUCT_SIZE sizeof(BE_CONFIG)    # is currently 331 bytes
  17.  
  18. VBR_METHOD_NONE,  VBR_METHOD_DEFAULT, VBR_METHOD_OLD, VBR_METHOD_NEW, VBR_METHOD_MTRH, VBR_METHOD_ABR = range(-1, 5)
  19. 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)]
  20.  
  21. class BE_VERSION(Structure):
  22.     _fields_ = [
  23.         ('byDLLMajorVersion', c_byte),  # BladeEnc DLL Version number
  24.         ('byDLLMinorVersion', c_byte),
  25.        
  26.         ('byMajorVersion', c_byte),    # BladeEnc Engine Version Number
  27.         ('byMinorVersion', c_byte),
  28.        
  29.         ('byDay', c_byte),             # DLL Release date
  30.         ('byMonth', c_byte),
  31.         ('wYear', c_short),
  32.         ('zHomepage', c_char * 129),    # BladeEnc Homepage URL
  33.         ('byAlphaLevel', c_byte),
  34.         ('byBetaLevel', c_byte),
  35.         ('byMMXEnabled', c_byte),
  36.         ('btReserved', c_byte * 125)                
  37.     ]
  38.     _pack_ = 1
  39.  
  40.  
  41. class BE_CONFIG(Structure):
  42.     _fields_ = [
  43.         ('dwConfig', c_ulong),          # BE_CONFIG_XXXXX, Currently only BE_CONFIG_MP3 is supported
  44.         ('dwStructVersion', c_ulong),  
  45.         ('dwStructSize', c_ulong),
  46.         ('dwSampleRate', c_ulong),      # SAMPLERATE OF INPUT FILE
  47.         ('dwReSampleRate', c_ulong),    # DOWNSAMPLERATE, 0=ENCODER DECIDES
  48.         ('nMode', c_long),              # BE_MP3_MODE_STEREO, BE_MP3_MODE_DUALCHANNEL, BE_MP3_MODE_MONO
  49.         ('dwBitrate', c_ulong),         # CBR bitrate, VBR min bitrate
  50.         ('dwMaxBitrate', c_ulong),      # CBR ignored, VBR Max bitrate
  51.         ('nPreset', c_long),            # Quality preset, use one of the settings of the LAME_QUALITY_PRESET enum
  52.         ('dwMpegVersion', c_ulong),     # FUTURE USE, MPEG-1 OR MPEG-2
  53.         ('dwPsyModel', c_ulong),        # FUTURE USE, SET TO 0
  54.         ('dwEmphasis', c_ulong),        # FUTURE USE, SET TO 0
  55.    
  56.         # BIT STREAM SETTINGS
  57.         ('bPrivate', c_long),    #bool Set Private Bit (True/FALSE)
  58.         ('bCRC', c_long), # bool Insert CRC (True/FALSE)
  59.         ('bCopyright', c_long), # bool Set Copyright Bit (True/FALSE)
  60.         ('bOriginal', c_long), # bool  Set Original Bit (True/FALSE)
  61.        
  62.         # VBR STUFF
  63.        
  64.         ('bWriteVBRHeader', c_long), # bool WRITE XING VBR HEADER (True/FALSE)
  65.         ('bEnableVBR', c_long),      #bool USE VBR ENCODING (True/FALSE)
  66.         ('nVBRQuality', c_int),     # VBR QUALITY 0..9
  67.         ('dwVbrAbr_bps', c_ulong), # Use ABR instead of nVBRQuality
  68.        
  69.         # CHECK THIS!!!!
  70.         ('nVbrMethod', c_int),      
  71.        
  72.         ('bNoRes', c_long), # bool Disable Bit reservoir (True/FALSE)
  73.    
  74.         # MISC SETTINGS
  75.         ('bStrictIso', c_long), # Use strict ISO encoding rules (True/FALSE)
  76.         ('nQuality', c_ushort), # Quality Setting, HIGH BYTE should be NOT LOW byte, otherwhise quality=5
  77.    
  78.         # FUTURE USE, SET TO 0, align strucutre to 331 bytes
  79.         ('btReserved', c_byte * (255 - 4*4 - 2 ) )
  80.     ]
  81.  
  82.     _packed_ = 1
  83.  
  84.  
  85. lame = cdll.lame_enc
  86.  
  87.  
  88. #version = BE_VERSION()
  89. #print version.zHomepage
  90. #lame.beVersion(byref(version))  
  91. #print version.byDay, version.byMonth
  92.  
  93. beConfig = BE_CONFIG()
  94.  
  95. print sizeof(c_int)
  96. print sizeof(beConfig)
  97.  
  98. beConfig.dwConfig = BE_CONFIG_LAME;
  99.  
  100. beConfig.dwStructVersion    = 1
  101. beConfig.dwStructSize       = sizeof(beConfig)    
  102. beConfig.dwSampleRate       = 22050                # INPUT FREQUENCY
  103. beConfig.dwReSampleRate     = 0                    # LET ENCODER DECIDE
  104. beConfig.nMode              = BE_MP3_MODE_MONO  # OUTPUT IN STREO
  105. beConfig.dwBitrate          = 128                  # MINIMUM BIT RATE
  106. beConfig.nPreset            = LQP_FM               # QUALITY PRESET SETTING
  107. beConfig.dwMpegVersion      = MPEG1                # MPEG VERSION (I or II)
  108. beConfig.dwPsyModel         = 0                    # USE DEFAULT PSYCHOACOUSTIC MODEL
  109. beConfig.dwEmphasis         = 0                    # NO EMPHASIS TURNED ON
  110. beConfig.bOriginal          = True                 # SET ORIGINAL FLAG
  111. beConfig.dwMaxBitrate       = 320                  # MAXIMUM BIT RATE
  112. beConfig.bCRC               = True                 # INSERT CRC
  113. beConfig.bCopyright         = True                 # SET COPYRIGHT FLAG  
  114. beConfig.bPrivate           = True                 # SET PRIVATE FLAG
  115. beConfig.bWriteVBRHeader    = False                 # YES, WRITE THE XING VBR HEADER
  116. beConfig.bEnableVBR         = False                 # USE VBR
  117. beConfig.nVBRQuality        = 5                    # SET VBR QUALITY
  118. beConfig.bNoRes             = True                 # No Bit reservoir
  119. beConfig.nVBRMethod = VBR_METHOD_NONE
  120.  
  121.  
  122. inBufSize = c_ulong()
  123. outBufSize = c_ulong()
  124. stream = c_int()
  125.  
  126. result = lame.beInitStream(byref(beConfig), byref(inBufSize), byref(outBufSize), byref(stream))
  127.  
  128. if result != BE_ERR_SUCCESSFUL:
  129.     print 'Error', result
  130.     sys.exit(0)
  131.  
  132. print inBufSize, outBufSize, stream
  133.  
  134. outBuf = create_string_buffer(outBufSize.value)
  135.  
  136. # test.wav encoded at 22050, mono
  137.  
  138. inFile = open('test.wav', 'rb')
  139. inFile.seek(0, os.SEEK_END)
  140. wavFileSize = inFile.tell()
  141. inFile.seek(44) # skip wav header
  142.  
  143.  
  144. outFile = open('test.mp3', 'wb')
  145.  
  146. while(1):
  147.     inBytes = inFile.read(inBufSize.value * 2)
  148.     if inBytes == '':
  149.         break
  150.     print len(inBytes)
  151.     inBuf = create_string_buffer(inBytes, inBufSize.value * 2)
  152.    
  153.     nBytesOut = c_ulong()
  154.     err = lame.beEncodeChunk(stream, len(inBytes), inBuf, outBuf, byref(nBytesOut) )
  155.     if err != 0:
  156.         break
  157.     outFile.write(outBuf[0:nBytesOut.value])
  158.    
  159.  
  160. err = lame.beDeinitStream(stream, outBuf, byref(nBytesOut));
  161.  
  162. if err == 0 and nBytesOut != 0:
  163.     outFile.write(outBuf[0:nBytesOut.value])
  164.    
  165. lame.beCloseStream(stream)
  166. outFile.close()
  167. inFile.close()
Advertisement
Add Comment
Please, Sign In to add comment