home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD103231012000.psc / morphyx.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-26  |  5.3 KB  |  267 lines

  1. /*********************************************************
  2.  * FILE: morphyx.cpp                                     *
  3.  * AUTHOR: Ultimatum                                     *
  4.  * DESCRIPTION: API "wrapper" for the various MP3        *
  5.  *                encoding engines.                        *
  6.  *********************************************************/
  7.  
  8.  
  9. // Note to programmers: this file I wrote myself. However,
  10. // I did NOT write the file bladedll.h, nor did I create
  11. // the Blade Encoder DLL or the Lame Encoder DLL
  12.  
  13. #include <windows.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <io.h>
  17. #include <fcntl.h>
  18. #include <sys/stat.h>
  19.  
  20. // Lame and Blade have the same API prototypes, so only one header is needed.
  21. // I chose the blade header.
  22. #include "bladedll.h"
  23.  
  24. #define WINEXPORT __declspec(dllexport) WINAPI
  25.  
  26. // MP3 encoding prototypes
  27. BEINITSTREAM    beInitStream;
  28. BEENCODECHUNK    beEncodeChunk;
  29. BEDEINITSTREAM    beDeinitStream;
  30. BECLOSESTREAM    beCloseStream;
  31.  
  32. // enumerated encoder engine
  33. #define ENC_LAME 0
  34. #define ENC_BLADE 1
  35.  
  36. int                encoding_still;
  37. DWORD            percent_done = 0;
  38. LONG            thisEncoder;
  39.  
  40. HINSTANCE hBladeDLL;
  41. HINSTANCE hLameDLL;
  42.  
  43. // enum to MP3 encoding status
  44. typedef BOOL (CALLBACK* ENUMENC) (int);
  45.  
  46. // DLL startup
  47. extern "C" int APIENTRY
  48. DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  49. {
  50.     if (dwReason == DLL_PROCESS_ATTACH)
  51.     {
  52.         // startup processing
  53.         hBladeDLL = LoadLibrary("BLADEENC.DLL");
  54.         hLameDLL  = LoadLibrary("LAME_ENC.DLL");
  55.         thisEncoder = ENC_BLADE;
  56.     }
  57.     else if (dwReason == DLL_PROCESS_DETACH)
  58.     {
  59.         // cleanup processing
  60.         if (hBladeDLL)
  61.             FreeLibrary(hBladeDLL);
  62.         if (hLameDLL)
  63.             FreeLibrary(hLameDLL);
  64.  
  65.     }
  66.     return 1;
  67. }
  68.  
  69. LONG WINEXPORT SetEncoder(LONG enc)
  70. {
  71.     thisEncoder = enc;
  72.     return 0;
  73.  
  74. }
  75.  
  76. LONG WINEXPORT EncodeMp3(LPCSTR lpszWavFile, ENUMENC &EnumEncoding)
  77. {    
  78.     if(lpszWavFile == "")
  79.     {
  80.         return -1;
  81.     }
  82.  
  83.     // Load Encoding DLL
  84.  
  85.     if (thisEncoder == ENC_BLADE)
  86.     {
  87.         if(!hBladeDLL)
  88.         {
  89.             return -1;
  90.         }
  91.     }
  92.     else
  93.     {
  94.         if(!hLameDLL)
  95.         {
  96.             return -1;
  97.         }
  98.     }
  99.  
  100.     // Get Interface
  101.  
  102.     if (thisEncoder == ENC_BLADE)
  103.     {
  104.  
  105.         beInitStream    = (BEINITSTREAM) GetProcAddress(hBladeDLL, TEXT_BEINITSTREAM);
  106.         beEncodeChunk    = (BEENCODECHUNK) GetProcAddress(hBladeDLL, TEXT_BEENCODECHUNK);
  107.         beDeinitStream    = (BEDEINITSTREAM) GetProcAddress(hBladeDLL, TEXT_BEDEINITSTREAM);
  108.         beCloseStream    = (BECLOSESTREAM) GetProcAddress(hBladeDLL, TEXT_BECLOSESTREAM);
  109.     }
  110.     else
  111.     {
  112.         beInitStream    = (BEINITSTREAM) GetProcAddress(hLameDLL, TEXT_BEINITSTREAM);
  113.         beEncodeChunk    = (BEENCODECHUNK) GetProcAddress(hLameDLL, TEXT_BEENCODECHUNK);
  114.         beDeinitStream    = (BEDEINITSTREAM) GetProcAddress(hLameDLL, TEXT_BEDEINITSTREAM);
  115.         beCloseStream    = (BECLOSESTREAM) GetProcAddress(hLameDLL, TEXT_BECLOSESTREAM);
  116.     }
  117.  
  118.  
  119.     if(!beInitStream || !beEncodeChunk || !beDeinitStream || !beCloseStream)
  120.     {
  121.         return -1;
  122.     }
  123.  
  124.     int hIn = open(lpszWavFile, O_RDONLY | O_BINARY);
  125.  
  126.     if(hIn == -1)
  127.     {
  128.         return -1;
  129.     }
  130.  
  131.  
  132.     char zOutputFilename[MAX_PATH + 1];    
  133.     lstrcpy(zOutputFilename, lpszWavFile);
  134.     int l = lstrlen(zOutputFilename);
  135.     while(l && zOutputFilename[l] != '.')    {
  136.  
  137.         l--;
  138.     }
  139.  
  140.     if(!l)    {
  141.  
  142.         l = lstrlen(zOutputFilename) - 1;
  143.     }
  144.  
  145.     zOutputFilename[l] = '\0';
  146.  
  147.     lstrcat(zOutputFilename, ".mp3");
  148.  
  149.     int hOut = open(zOutputFilename, O_WRONLY | O_BINARY | O_TRUNC | O_CREAT, S_IWRITE);
  150.  
  151.     if(hOut == -1)    
  152.     {
  153.         return -1;
  154.     }
  155.  
  156.     BE_CONFIG    beConfig;
  157.  
  158.     beConfig.dwConfig = BE_CONFIG_MP3;
  159.  
  160.     beConfig.format.mp3.dwSampleRate    = 44100;
  161.     beConfig.format.mp3.byMode            = BE_MP3_MODE_STEREO;
  162.     beConfig.format.mp3.wBitrate        = 128;
  163.     beConfig.format.mp3.bCopyright        = FALSE;
  164.     beConfig.format.mp3.bCRC            = FALSE;
  165.     beConfig.format.mp3.bOriginal        = FALSE;
  166.     beConfig.format.mp3.bPrivate        = FALSE;
  167.  
  168.     DWORD        dwSamples, dwMP3Buffer;
  169.     HBE_STREAM    hbeStream;
  170.     BE_ERR        err;
  171.  
  172.     err = beInitStream(&beConfig, &dwSamples, &dwMP3Buffer, &hbeStream);
  173.  
  174.     if(err != BE_ERR_SUCCESSFUL)
  175.     {
  176.         return -1;
  177.     }
  178.  
  179.     PBYTE pMP3Buffer = new BYTE[dwMP3Buffer];
  180.  
  181.     PSHORT pBuffer = new SHORT[dwSamples];
  182.  
  183.     if(!pMP3Buffer || !pBuffer)
  184.     {
  185.         return -1;
  186.     }
  187.  
  188.     DWORD    length = filelength(hIn);
  189.     DWORD    done = 0;
  190.     DWORD    dwWrite;
  191.     DWORD    toread;
  192.     DWORD    towrite;
  193.     
  194.     setbuf(stdout,NULL);
  195.  
  196.     while(done < length)
  197.     {
  198.         encoding_still = 1;
  199.         if(done + dwSamples * 2 < length)
  200.         {
  201.  
  202.             toread = dwSamples * 2;
  203.         }
  204.         else
  205.         {
  206.  
  207.             toread = length - done;
  208.         }
  209.  
  210.         if(read(hIn, pBuffer, toread) == -1)
  211.         {
  212.             encoding_still = 0;
  213.             return -1;
  214.         }         
  215.  
  216.         err = beEncodeChunk(hbeStream, toread/2, pBuffer, pMP3Buffer, &towrite);
  217.  
  218.         if(err != BE_ERR_SUCCESSFUL)
  219.         {
  220.  
  221.             beCloseStream(hbeStream);
  222.             encoding_still = 0;
  223.             return -1;
  224.         }
  225.         
  226.         if(write(hOut, pMP3Buffer, towrite) == -1)
  227.         {
  228.             encoding_still = 0;
  229.             return -1;
  230.         }
  231.  
  232.         done += toread;
  233.  
  234.         percent_done = 100 * (float)done/(float)length;
  235.  
  236.         // call the enumerated function to display
  237.         // completion of encoding process
  238.         if ((EnumEncoding((int)percent_done)) == FALSE)
  239.             return -2;    //indicate that encoding was stopped by user
  240.     }
  241.     encoding_still = 0;
  242.     err = beDeinitStream(hbeStream, pMP3Buffer, &dwWrite);
  243.  
  244.     if(err != BE_ERR_SUCCESSFUL)
  245.     {
  246.         beCloseStream(hbeStream);
  247.         return -1;
  248.     }
  249.  
  250.     if(dwWrite)
  251.     {
  252.  
  253.         if(write(hOut, pMP3Buffer, dwWrite) == -1)
  254.         {
  255.             return -1;
  256.         }
  257.     }
  258.  
  259.     beCloseStream(hbeStream);
  260.  
  261.     close(hIn);
  262.     close(hOut);
  263.  
  264.     percent_done = 0;
  265.  
  266.     return 0;
  267. }