943,865 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 950
  • Python RSS
Nov 27th, 2008
0

Translate C/C++ Code to python

Expand Post »
Hi All,
For long now have tried to get Python bindings and none is there.
jlgm suggested me to try ctypes and I dived in. But there are few examples on using ctypes for new bees like me.

I tried to take lame_enc.dll (The file made me to learn ctypes ) as sample to play with. The sad thing is, I find only C/C++ examples of the DLL and I failed to translate them.

Here is the example:
C++ Syntax (Toggle Plain Text)
  1. /*
  2.  * LAME DLL Sample Code.
  3.  *
  4.  * Copyright (c) 2000 A.L. Faber
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the
  18.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19.  * Boston, MA 02111-1307, USA.
  20.  */
  21.  
  22.  
  23. #include <windows.h>
  24. #include <stdio.h>
  25. #include <io.h>
  26. #include <fcntl.h>
  27. #include <sys/stat.h>
  28. #include "BladeMP3EncDLL.h"
  29.  
  30. BEINITSTREAM beInitStream=NULL;
  31. BEENCODECHUNK beEncodeChunk=NULL;
  32. BEDEINITSTREAM beDeinitStream=NULL;
  33. BECLOSESTREAM beCloseStream=NULL;
  34. BEVERSION beVersion=NULL;
  35. BEWRITEVBRHEADER beWriteVBRHeader=NULL;
  36.  
  37.  
  38. // Main program
  39. int main(int argc, char *argv[])
  40. {
  41. HINSTANCE hDLL =NULL;
  42. FILE* pFileIn =NULL;
  43. FILE* pFileOut =NULL;
  44. BE_VERSION Version ={0,};
  45. BE_CONFIG beConfig ={0,};
  46.  
  47. CHAR strFileIn[255] ={'0',};
  48. CHAR strFileOut[255] ={'0',};
  49.  
  50. DWORD dwSamples =0;
  51. DWORD dwMP3Buffer =0;
  52. HBE_STREAM hbeStream =0;
  53. BE_ERR err =0;
  54.  
  55. PBYTE pMP3Buffer =NULL;
  56. PSHORT pWAVBuffer =NULL;
  57.  
  58. // check number of arguments
  59. if(argc != 2)
  60. {
  61. fprintf(stderr,"Usage: %s <filename.wav>\n", argv[0]);
  62. fprintf(stderr,"Descr: Short demo to show how to use the lame_enc.dll library file\n");
  63. fprintf(stderr,"Note : WAV file is assumed to to have the following parameters\n");
  64. fprintf(stderr," : 44100 Hz, stereo, 16 Bits per sample\n");
  65. return -1;
  66. }
  67.  
  68. // Setup the file names
  69. strcpy(strFileIn ,argv[1]);
  70. strcpy(strFileOut,argv[1]);
  71.  
  72. // Add mp3 extention
  73. strcat(strFileOut,".mp3");
  74.  
  75. // Load lame_enc.dll library (Make sure though that you set the
  76. // project/settings/debug Working Directory correctly, otherwhise the DLL can't be loaded
  77.  
  78. #ifdef _MSC_VER
  79. hDLL = LoadLibrary(".\\Debug\\lame_enc.dll");
  80.  
  81. #ifdef _DEBUG
  82. hDLL = LoadLibrary(".\\Debug\\lame_enc.dll");
  83. #else
  84. hDLL = LoadLibrary(".\\Release\\lame_enc.dll");
  85.  
  86. if ( NULL == hDLL )
  87. {
  88. hDLL = LoadLibrary(".\\Release_NASM\\lame_enc.dll");
  89. }
  90. #endif /* _DEBUG */
  91. #else
  92. /*
  93.   Don't worry about dll location.
  94.   MSVC is the only compiler that creates
  95.   .\Release\ or .\Debug\ directories.
  96.   */
  97. hDLL = LoadLibrary("lame_enc.dll");
  98. #endif /* _MSC_VER */
  99.  
  100. if( NULL == hDLL )
  101. {
  102. fprintf(stderr,"Error loading lame_enc.DLL");
  103. return -1;
  104. }
  105.  
  106. // Get Interface functions from the DLL
  107. beInitStream = (BEINITSTREAM) GetProcAddress(hDLL, TEXT_BEINITSTREAM);
  108. beEncodeChunk = (BEENCODECHUNK) GetProcAddress(hDLL, TEXT_BEENCODECHUNK);
  109. beDeinitStream = (BEDEINITSTREAM) GetProcAddress(hDLL, TEXT_BEDEINITSTREAM);
  110. beCloseStream = (BECLOSESTREAM) GetProcAddress(hDLL, TEXT_BECLOSESTREAM);
  111. beVersion = (BEVERSION) GetProcAddress(hDLL, TEXT_BEVERSION);
  112. beWriteVBRHeader= (BEWRITEVBRHEADER) GetProcAddress(hDLL,TEXT_BEWRITEVBRHEADER);
  113.  
  114. // Check if all interfaces are present
  115. if(!beInitStream || !beEncodeChunk || !beDeinitStream || !beCloseStream || !beVersion || !beWriteVBRHeader)
  116. {
  117. printf("Unable to get LAME interfaces");
  118. return -1;
  119. }
  120.  
  121. // Get the version number
  122. beVersion( &Version );
  123.  
  124. printf(
  125. "lame_enc.dll version %u.%02u (%u/%u/%u)\n"
  126. "lame_enc Engine %u.%02u\n"
  127. "lame_enc homepage at %s\n\n",
  128. Version.byDLLMajorVersion, Version.byDLLMinorVersion,
  129. Version.byDay, Version.byMonth, Version.wYear,
  130. Version.byMajorVersion, Version.byMinorVersion,
  131. Version.zHomepage);
  132.  
  133. // Try to open the WAV file, be sure to open it as a binary file!
  134. pFileIn = fopen( strFileIn, "rb" );
  135.  
  136. // Check file open result
  137. if(pFileIn == NULL)
  138. {
  139. fprintf(stderr,"Error opening %s", argv[1]);
  140. return -1;
  141. }
  142.  
  143. // Open MP3 file
  144. pFileOut= fopen(strFileOut,"wb+");
  145.  
  146. // Check file open result
  147. if(pFileOut == NULL)
  148. {
  149. fprintf(stderr,"Error creating file %s", strFileOut);
  150. return -1;
  151. }
  152.  
  153. memset(&beConfig,0,sizeof(beConfig)); // clear all fields
  154.  
  155. // use the LAME config structure
  156. beConfig.dwConfig = BE_CONFIG_LAME;
  157.  
  158. // this are the default settings for testcase.wav
  159. beConfig.format.LHV1.dwStructVersion = 1;
  160. beConfig.format.LHV1.dwStructSize = sizeof(beConfig);
  161. beConfig.format.LHV1.dwSampleRate = 44100; // INPUT FREQUENCY
  162. beConfig.format.LHV1.dwReSampleRate = 0; // DON"T RESAMPLE
  163. beConfig.format.LHV1.nMode = BE_MP3_MODE_JSTEREO; // OUTPUT IN STREO
  164. beConfig.format.LHV1.dwBitrate = 128; // MINIMUM BIT RATE
  165. beConfig.format.LHV1.nPreset = LQP_HIGH_QUALITY; // QUALITY PRESET SETTING
  166. beConfig.format.LHV1.dwMpegVersion = MPEG1; // MPEG VERSION (I or II)
  167. beConfig.format.LHV1.dwPsyModel = 0; // USE DEFAULT PSYCHOACOUSTIC MODEL
  168. beConfig.format.LHV1.dwEmphasis = 0; // NO EMPHASIS TURNED ON
  169. beConfig.format.LHV1.bOriginal = TRUE; // SET ORIGINAL FLAG
  170.  
  171. // beConfig.format.LHV1.dwMaxBitrate = 320; // MAXIMUM BIT RATE
  172. // beConfig.format.LHV1.bCRC = TRUE; // INSERT CRC
  173. // beConfig.format.LHV1.bCopyright = TRUE; // SET COPYRIGHT FLAG
  174. // beConfig.format.LHV1.bPrivate = TRUE; // SET PRIVATE FLAG
  175. // beConfig.format.LHV1.bWriteVBRHeader = TRUE; // YES, WRITE THE XING VBR HEADER
  176. // beConfig.format.LHV1.bEnableVBR = TRUE; // USE VBR
  177. // beConfig.format.LHV1.nVBRQuality = 5; // SET VBR QUALITY
  178. beConfig.format.LHV1.bNoRes = TRUE; // No Bit resorvoir
  179.  
  180. // Preset Test
  181. // beConfig.format.LHV1.nPreset = LQP_PHONE;
  182.  
  183. // Init the MP3 Stream
  184. err = beInitStream(&beConfig, &dwSamples, &dwMP3Buffer, &hbeStream);
  185.  
  186. // Check result
  187. if(err != BE_ERR_SUCCESSFUL)
  188. {
  189. fprintf(stderr,"Error opening encoding stream (%lu)", err);
  190. return -1;
  191. }
  192.  
  193.  
  194. // Allocate MP3 buffer
  195. pMP3Buffer = new BYTE[dwMP3Buffer];
  196.  
  197. // Allocate WAV buffer
  198. pWAVBuffer = new SHORT[dwSamples];
  199.  
  200. // Check if Buffer are allocated properly
  201. if(!pMP3Buffer || !pWAVBuffer)
  202. {
  203. printf("Out of memory");
  204. return -1;
  205. }
  206.  
  207. DWORD dwRead=0;
  208. DWORD dwWrite=0;
  209. DWORD dwDone=0;
  210. DWORD dwFileSize=0;
  211.  
  212. // Seek to end of file
  213. fseek(pFileIn,0,SEEK_END);
  214.  
  215. // Get the file size
  216. dwFileSize=ftell(pFileIn);
  217.  
  218. // Seek back to start of WAV file,
  219. // but skip the first 44 bytes, since that's the WAV header
  220. fseek(pFileIn,44,SEEK_SET);
  221.  
  222.  
  223. // Convert All PCM samples
  224. while ( (dwRead=fread(pWAVBuffer,sizeof(SHORT),dwSamples,pFileIn)) >0 )
  225. {
  226. // Encode samples
  227. err = beEncodeChunk(hbeStream, dwRead, pWAVBuffer, pMP3Buffer, &dwWrite);
  228.  
  229. // Check result
  230. if(err != BE_ERR_SUCCESSFUL)
  231. {
  232. beCloseStream(hbeStream);
  233. fprintf(stderr,"beEncodeChunk() failed (%lu)", err);
  234. return -1;
  235. }
  236.  
  237. // write dwWrite bytes that are returned in tehe pMP3Buffer to disk
  238. if(fwrite(pMP3Buffer,1,dwWrite,pFileOut) != dwWrite)
  239. {
  240. fprintf(stderr,"Output file write error");
  241. return -1;
  242. }
  243.  
  244. dwDone += dwRead*sizeof(SHORT);
  245.  
  246. printf("Done: %0.2f%% \r", 100 * (float)dwDone/(float)(dwFileSize));
  247. }
  248.  
  249. // Deinit the stream
  250. err = beDeinitStream(hbeStream, pMP3Buffer, &dwWrite);
  251.  
  252. // Check result
  253. if(err != BE_ERR_SUCCESSFUL)
  254. {
  255.  
  256. beCloseStream(hbeStream);
  257. fprintf(stderr,"beExitStream failed (%lu)", err);
  258. return -1;
  259. }
  260.  
  261. // Are there any bytes returned from the DeInit call?
  262. // If so, write them to disk
  263. if(dwWrite)
  264. {
  265. if( fwrite( pMP3Buffer, 1, dwWrite, pFileOut ) != dwWrite )
  266. {
  267. fprintf(stderr,"Output file write error");
  268. return -1;
  269. }
  270. }
  271.  
  272. // close the MP3 Stream
  273. beCloseStream( hbeStream );
  274.  
  275. // Delete WAV buffer
  276. delete [] pWAVBuffer;
  277.  
  278. // Delete MP3 Buffer
  279. delete [] pMP3Buffer;
  280.  
  281. // Close input file
  282. fclose( pFileIn );
  283.  
  284. // Close output file
  285. fclose( pFileOut );
  286.  
  287. // Write the VBR Tag
  288. beWriteVBRHeader( strFileOut );
  289.  
  290. // Were done, return OK result
  291. return 0;
  292. }

and here is my trial; I failed to finish
python Syntax (Toggle Plain Text)
  1. import ctypes as ct
  2. import os, sys
  3. lame = ct.cdll.lame_enc
  4. #start playing around
  5. #Configure the LAME encoder the to do the conversion.
  6. #use lame configuration structure
  7. lame.beConfig.dwConfig = lame.BE_CONFIG_LAME
  8.  
  9.  
  10. # this are the default settings for testcase.wav
  11. lame.beConfig.format.LHV1.dwStructVersion = 1
  12. lame.beConfig.format.LHV1.dwStructSize = sizeof(lame.beConfig)
  13. lame.beConfig.format.LHV1.dwSampleRate = 44100 # INPUT FREQUENCY
  14. lame.beConfig.format.LHV1.dwReSampleRate = 0 #DON"T RESAMPLE
  15. lame.beConfig.format.LHV1.nMode = BE_MP3_MODE_JSTEREO #OUTPUT IN STREO
  16. lame.beConfig.format.LHV1.dwBitrate = 128 #MINIMUM BIT RATE
  17. lame.beConfig.format.LHV1.nPreset = LQP_HIGH_QUALITY #QUALITY PRESET SETTING
  18. lame.beConfig.format.LHV1.dwMpegVersion = MPEG1 # MPEG VERSION (I or II)
  19. lame.beConfig.format.LHV1.dwPsyModel = 0 #USE DEFAULT PSYCHOACOUSTIC MODEL
  20. lame.beConfig.format.LHV1.dwEmphasis = 0 #NO EMPHASIS TURNED ON
  21. lame.beConfig.format.LHV1.bOriginal = TRUE #SET ORIGINAL FLAG
  22.  
  23.  
  24. #init mp3 stream
  25. try:
  26. mp3 = beInitStream(beConfig, dwSamples, dwMP3Buffer, hbeStream)
  27.  
  28. except:
  29. print "Error: Cannot open encoding stream "
  30. sys.exit(True)
  31.  
  32.  
  33. #failed to go on due to lack of C/C++ knowledge

and here is TCL example:
http://wiki.tcl.tk/14289
and another CPP example:
http://www.creative-urge.com/ljpics/main.cpp

Please need help
Similar Threads
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Nov 27th, 2008
0

Re: Translate C/C++ Code to python

And another example here I found but didn't help
http://www.vbaccelerator.com/home/VB...ME/article.asp
and DLL manual here
http://www.fi.muni.cz/~qruzicka/Smid/man.htm
Pse help me
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Nov 29th, 2008
0

Re: Translate C/C++ Code to python

I found that Variables in Ctypes are different from python one. How do I interface the three variable; ctypes, C++ and python?

Thanks
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: wx.CallAfter()
Next Thread in Python Forum Timeline: List operation problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC