線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:14228
推到 Plurk!
推到 Facebook!

好用的錄音程式

 
leobxb
一般會員


發表:18
回覆:30
積分:14
註冊:2003-10-02

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-10-07 10:15:57 IP:220.229.xxx.xxx 未訂閱
 
//---------------------------------------------------------------------------    #include 
#include 
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;    HINSTANCE g_hInstance;
HANDLE m_hMCIWnd;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
  m_hMCIWnd=MCIWndCreate(Handle,
                         g_hInstance,
                         WS_CHILD | WS_OVERLAPPED | WS_CAPTION | WS_BORDER |
                         MCIWNDF_RECORD | MCIWNDF_SHOWALL,
                         NULL );
  if ( NULL==m_hMCIWnd )
  {
    MessageBox(Handle,"Error Creating MCIWnd Window!",NULL, MB_OK);
    return;
  }
  Button1->Enabled=true;
  Label1->Caption="IDLE";
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1(void)
{
  if ( Button2->Enabled )
    Button2Click(NULL);      MCIWndDestroy(m_hMCIWnd);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  MCIWndNew(m_hMCIWnd, "waveaudio");
  // change the default audio setting (which is 11khz 8bit mono)
  MCI_WAVE_SET_PARMS set_parms;
  set_parms.wFormatTag      = WAVE_FORMAT_PCM;
  set_parms.wBitsPerSample  = 16;
  set_parms.nChannels       = 1;
  set_parms.nBlockAlign     = (set_parms.nChannels*set_parms.wBitsPerSample)/8;
  set_parms.nSamplesPerSec  = 44100;
  set_parms.nAvgBytesPerSec = ((set_parms.wBitsPerSample) *
                                set_parms.nChannels *
                                set_parms.nSamplesPerSec)/8;      int deviceID=MCIWndGetDeviceID(m_hMCIWnd);
  int result = mciSendCommand( deviceID, MCI_SET,
                               MCI_WAIT
                             | MCI_WAVE_SET_FORMATTAG
                             | MCI_WAVE_SET_BITSPERSAMPLE
                             | MCI_WAVE_SET_CHANNELS
                             | MCI_WAVE_SET_SAMPLESPERSEC
                             | MCI_WAVE_SET_AVGBYTESPERSEC
                             | MCI_WAVE_SET_BLOCKALIGN,
                               (DWORD)(LPVOID)&set_parms);
  if ( result )
  {
    char buffer[100];
    mciGetErrorString(result, buffer, sizeof(buffer));
    MessageBox( NULL, buffer, "MCI_WAVE_SET_1", MB_OK);
    return;
  }      MCIWndRecord(m_hMCIWnd);      Button1->Enabled=false;
  Button2->Enabled=true;
  Label1->Caption="RECORDING";        
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  MCIWndStop(m_hMCIWnd);
  MCIWndSave(m_hMCIWnd,"test.wav");
  MCIWndClose(m_hMCIWnd);      Button1->Enabled=true;
  Button2->Enabled=false;
  Label1->Caption="IDLE";
}
//---------------------------------------------------------------------------
這個錄音程式很好用,但我用時原本要存成test.wav的檔案,會變成亂碼? 不知道為什麼?請大家試試吧!!!如有解決方式也分享一下吧...呵呵 有收穫記得回饋,讓我們台灣跨出程式的出頭天...
huei_brother
一般會員


發表:36
回覆:15
積分:10
註冊:2004-08-23

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-10-28 13:16:46 IP:140.127.xxx.xxx 未訂閱
我試的時候也是這樣耶 請大家幫忙看一下囉 謝謝
bugmans
高階會員


發表:95
回覆:322
積分:188
註冊:2003-04-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-11-12 12:47:18 IP:218.166.xxx.xxx 未訂閱
我一開始從vfw.h找出MCIWndSave的宣告方式 #define MCIWndSave(hwnd, szFile)    (LONG)MCIWndSM(hwnd, MCI_SAVE, 0, (LPARAM)(LPVOID)(szFile)) 而MCIWndSM在vfw.h定義為SendMessage #ifdef __cplusplus #define MCIWndSM ::SendMessage  /* SendMessage in C++*/ #else #define MCIWndSM SendMessage    /* SendMessage in C */ 所以將程式碼改為SendMessage(m_hMCIWnd,MCI_SAVE,0,(LPARAM)(LPVOID)(szFile)); 結果都失敗了 後來改用mciSendCommand來解決這個問題,修改後的程式碼如下
 
//---------------------------------------------------------------------------    #include 
#include 
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;    HINSTANCE g_hInstance;
HANDLE m_hMCIWnd;
int deviceID;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
  m_hMCIWnd=MCIWndCreate(Handle,
                         g_hInstance,
                         WS_CHILD | WS_OVERLAPPED | WS_CAPTION | WS_BORDER |
                         MCIWNDF_RECORD | MCIWNDF_SHOWALL,
                         NULL );
  if ( NULL==m_hMCIWnd )
  {
    MessageBox(Handle,"Error Creating MCIWnd Window!",NULL, MB_OK);
    return;
  }
  Button1->Enabled=true;
  Label1->Caption="IDLE";
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1(void)
{
  if ( Button2->Enabled )
    Button2Click(NULL);      MCIWndDestroy(m_hMCIWnd);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  MCIWndNew(m_hMCIWnd, "waveaudio");
  // change the default audio setting (which is 11khz 8bit mono)
  MCI_WAVE_SET_PARMS set_parms;
  set_parms.wFormatTag      = WAVE_FORMAT_PCM;
  set_parms.wBitsPerSample  = 16;
  set_parms.nChannels       = 1;
  set_parms.nBlockAlign     = (set_parms.nChannels*set_parms.wBitsPerSample)/8;
  set_parms.nSamplesPerSec  = 44100;
  set_parms.nAvgBytesPerSec = ((set_parms.wBitsPerSample) *
                                set_parms.nChannels *
                                set_parms.nSamplesPerSec)/8;      //int deviceID=MCIWndGetDeviceID(m_hMCIWnd);
  //mciSendCommand需要deviceID當作傳入的參數
  deviceID=MCIWndGetDeviceID(m_hMCIWnd);
  int result = mciSendCommand( deviceID, MCI_SET,
                               MCI_WAIT
                             | MCI_WAVE_SET_FORMATTAG
                             | MCI_WAVE_SET_BITSPERSAMPLE
                             | MCI_WAVE_SET_CHANNELS
                             | MCI_WAVE_SET_SAMPLESPERSEC
                             | MCI_WAVE_SET_AVGBYTESPERSEC
                             | MCI_WAVE_SET_BLOCKALIGN,
                               (DWORD)(LPVOID)&set_parms);
  if ( result )
  {
    char buffer[100];
    mciGetErrorString(result, buffer, sizeof(buffer));
    MessageBox( NULL, buffer, "MCI_WAVE_SET_1", MB_OK);
    return;
  }      MCIWndRecord(m_hMCIWnd);      Button1->Enabled=false;
  Button2->Enabled=true;
  Label1->Caption="RECORDING";        
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  MCIWndStop(m_hMCIWnd);
  //MCIWndSave(m_hMCIWnd,"test.wav");
  MCI_SAVE_PARMS mciSP;
  mciSp.lpfilename ="a.wav";
  mciSendCommand(deviceID,MCI_SAVE,MCI_SAVE_FILE | MCI_WAIT,(DWORD)(LPVOID)&mciSP);
  MCIWndClose(m_hMCIWnd);      Button1->Enabled=true;
  Button2->Enabled=false;
  Label1->Caption="IDLE";
}
//---------------------------------------------------------------------------
huei_brother
一般會員


發表:36
回覆:15
積分:10
註冊:2004-08-23

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-11-18 15:04:33 IP:140.127.xxx.xxx 未訂閱
bugmans您好 我RUN一下你修正後得程式結果出現 可否請您告訴我會什會這樣謝謝您的幫忙 Body has already been defined for function '_fastcall TForm1::~TForm()' Undefined symbol 'mciSp'
huei_brother
一般會員


發表:36
回覆:15
積分:10
註冊:2004-08-23

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-11-18 15:05:20 IP:140.127.xxx.xxx 未訂閱
bugmans您好 我RUN一下你修正後得程式結果出現 可否請您告訴我為什會這樣試我哪裡有錯嗎謝謝您的幫忙 Body has already been defined for function '_fastcall TForm1::~TForm()' Undefined symbol 'mciSp'
okeyla
一般會員


發表:51
回覆:20
積分:19
註冊:2003-06-12

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-11-21 11:30:26 IP:211.76.xxx.xxx 未訂閱
有無壓成mp3的版本呢? 相信會更實用的!
ddtddt55
一般會員


發表:7
回覆:3
積分:2
註冊:2004-05-20

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-12-16 02:09:43 IP:218.162.xxx.xxx 未訂閱
有棒的程式喔~~ 雖然會用...但不知道每個程式碼代表的意義 可以請你說明一下或是哪裡有這方面的教學說明 謝謝~~^0^
GGL
資深會員


發表:104
回覆:600
積分:335
註冊:2006-11-05

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-04-29 01:03:49 IP:211.76.xxx.xxx 未訂閱
請問一下,如果要把錄下來的改成mp3是要怎麼改? 是要改成wFormatTag=WAVE_FORMAT_MPEG嗎?我試過有錯誤訊息,而且我其他的參數不知道該怎麼設定。查了MSDN不知道是我不會查還是怎樣,找到相關的但是也沒寫出該怎麼用成MP3的格式,麻煩知道的大大告訴我。謝謝囉
雲中鵝
一般會員


發表:14
回覆:24
積分:7
註冊:2004-12-21

發送簡訊給我
#9 引用回覆 回覆 發表時間:2005-07-30 14:46:13 IP:140.125.xxx.xxx 未訂閱
請問一下大大.. 如何show出目前的的錄音的品質.即是目前由mci輸入的信號大小. 小弟我在msdn找到了相關的內容.但是經過測試後,它好像是要先錄好之後,才能去讀它的信號大小,有沒有方法去即時量測現在的信號大小.    ps:小弟剛好有轉mp3的資料.經過測試也可以了.隨便貼上.    msdn:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_recording.asp
 
        mcistateparms.dwItem =MCI_WAVE_STATUS_LEVEL;
        if(mciSendCommand(wdeviceID,MCI_STATUS,
                                    MCI_STATUS_ITEM
                                    ,(long)&mcistateparms))
        {
           char buffer[100];
            mciGetErrorString(dwReturn, buffer, sizeof(buffer));
           ShowMessage(buffer);
        }            if (double(mcistateparms.dwReturn) <100)
        {
                ShowMessage("to adjust the value");
        }
about mp3 file:轉貼. 本站主要面向Borland C++Builder, 网站名称: C++Builder 研究, C++Builder Study, 关键词: BCB, 致力于C++Builder的学习探讨和研究 编辑日期:2004-3-15 阅读次数:5939 文档标题:将Wav格式压缩成Mp3 作者: 佚名 关 键 字:Wav,mp3,compress,convert,压缩,格式转换 本文转自 C++Builder 研究 - http://www.ccrun.com/article/go.asp?i=599&d=0xie80 Compressing Wav file to MP3
● 1. Introduction
First, I don't meant to give you informations about how to understand the mp3 algorithm. My goal is to explain how to use an already existing encoder with BCB.    ● 2. Choosing the mp3 encoder
There are tons of mp3 encoders. Some of them are free others are not. Some are fast but produce an awful result. Others are slow but with excellent result and give a high audio quality. The ideal would be a free, reasonably fast encoder giving a high audio quality, all at the same time.
Enjoy! This pearl exists. But we have to look at it in the GNU world. There is a GNU project, called LAME, for Lame Ain't a Mp3 Encoder, under the GPL license. The official web site of the LAME project is http://www.mp3dev.org/mp3/
Moreover, as it is a GNU project, we have access to the source and there is a version compiled for Win32 in a DLL.
Among all the other encoders, I want to quote two of them. The first, FRAUNHOFER, because it is a fast and excellent encoder : http://www.iis.fhg.de/ but it's not  free though.
The second because it's a very fast encoder but the audio result is awful. So don't use it except if you are looking for a fast encoder. It's the encoder from Xing Tech : http://www.xingtech.com/
Note : The Lame encoder has a limitation. The sample rate must be 32000, 44100 or 48000.?    ● 3. Some informations about the WAV format
A wav file is just a collection of chunks. There is a format chunk wich contains all the informations about the samples. For instance, the bitrate, the number of channels, if it's stereo or mono... There is also a chunk containing the data. In other words, this chunk contains all the samples. In front of the file, there are 12 characters indicating that the file is a wav file.
The two chunks given above must be present in the file.
There could be other chunk but we just ignore them. They are not needed for our purpose. If you want to know more about wav file, take a look at http://www.wotsit.org/ for a complete description.
The format chunk :    struct FormatChunk
{
    char            chunkID[4];
    long            chunkSize;
    short           wFormatTag;
    unsigned short  wChannels;
    unsigned long   dwSamplesPerSec;
    unsigned long   dwAvgBytesPerSec;
    unsigned short  wBlockAlign;
    unsigned short  wBitsPerSample;
    // Note: there may be additional fields here, depending upon wFormatTag.
};    Above, you can see the struct representing the format chunk. The chunkID is always "fmt " with an ending space (4 characters). It's the identification of the chunk. All other chunk have such an ID. The chunkSize parameter contains the number of bytes of the chunk, the ID and chunkSize excluded.
The format chunk must be the first chunk in the file.    The data chunk :
struct Chunk
{
    char chunkID[4];
    long chunkSize;
};
In the case of the data chunk, the chunkID contains "data". The chunkSize parameters contains the size of the raw data (samples). The data begins just after chunkSize.    In the case of the data chunk, the chunkID contains "data". The chunkSize parameters contains the size of the raw data (samples). The data begins just after chunkSize.
Dans le cas du bloc de données, chunkID contient "data". Le paramètre chunkSize contient la taille du bloc de données proprement dites. Celles-ci commencent juste après chunkSize.
So, when we read a wav file, all we have to do is :
- read the first 12 characters to check if it's a real wav file.
- read the format chunk in a struct similar to the formatChunk struct.
- skip the extra parameters in the format chunk, if any.
- find the data chunk, read the raw data and carry out with the encoding.
-skip all other chunks.
Donc, ce que nous devons faire est :
- lire les 12 premiers caractères pour déterminer si on est bien en présence d'un fichier wav.
- lire le bloc de format dans une structure similaire à la structure formatChunk.
- ignorer les caractères supplémentaires dans le bloc de format, s'il y en a.
- ignorer tous les blocs qui ne sont pas le bloc de données.
- trouver le bloc de données, lire ces données et lancer l'encodage.    ● 4. Importing the DLL
The DLL used for the encoding  is called lame_enc.dll.
Unfortunately, this DLL was build with VC 6 from Microsoft. If we just create a lib file from the DLL and try to import the library in BCB, we'll get an 'Unresolved external error' at link time for each function we'll try to use. Due to the declaration type, BCB is expecting a function name with a leading underscore and the function names doesn't have such a leading underscore.
To resolve this issue, we must, first, create a def file from our DLL. Open a console windows and type :    IMPDEF lame_enc.def lame_enc.dll    Open the lame_enc.def file with an editor (Notepad for instance) and modify it like this. This will create aliases for the functions :
    LIBRARY     LAME_ENC.DLL
    EXPORTS
    _beCloseStream = beCloseStream
    _beDeinitStream = beDeinitStream
    _beEncodeChunk = beEncodeChunk
    _beInitStream = beInitStream
    _beVersion = beVersion
    _beWriteVBRHeader = beWriteVBRHeader
    beCloseStream                  @4
    beDeinitStream                 @3
    beEncodeChunk                  @2
    beInitStream                   @1
    beVersion                      @5
    beWriteVBRHeader               @6    Now, we can create the lib file from our def file. We'll import that lib file in our project. To create the lib file, type :
implib lame_enc.lib lame_enc.def    ● 5. The code
First, you have to import the libary in your project. Next, include the header file of the DLL into your unit. In the DLL header file, you have to add extern "C" in front of all exported function.
Here is the header with the moifications (lame_enc.h) :
/*  bladedll.h
    +++++++++++++++++++++++++++
    +   Blade's Encoder DLL   +
    +++++++++++++++++++++++++++        ------------------------------------------------------
  - Version 1.00 (7 November 1998) - Jukka Poikolainen -
  ------------------------------------------------------
*/
#ifndef ___BLADEDLL_H_INCLUDED___
#define ___BLADEDLL_H_INCLUDED___    #pragma pack(push)
#pragma pack(1)    /* encoding formats */    #define BE_CONFIG_MP3 0
#define BE_CONFIG_LAME 256    /* type definitions */    typedef    unsigned long      HBE_STREAM;
typedef    HBE_STREAM         *PHBE_STREAM;
typedef    unsigned long      BE_ERR;    /* error codes */    #define BE_ERR_SUCCESSFUL                0x00000000
#define BE_ERR_INVALID_FORMAT            0x00000001
#define BE_ERR_INVALID_FORMAT_PARAMETERS 0x00000002
#define BE_ERR_NO_MORE_HANDLES           0x00000003
#define BE_ERR_INVALID_HANDLE            0x00000004
#define BE_ERR_BUFFER_TOO_SMALL          0x00000005    /* other constants */    #define BE_MAX_HOMEPAGE 256    /* format specific variables */    #define BE_MP3_MODE_STEREO      0
#define BE_MP3_MODE_JSTEREO     1
#define BE_MP3_MODE_DUALCHANNEL 2
#define BE_MP3_MODE_MONO        3    #define MPEG1 1
#define MPEG2 0    #ifdef _BLADEDLL
#undef FLOAT
    #include 
#endif    enum  MPEG_QUALITY
{
    NORMAL_QUALITY = 0,
    LOW_QUALITY,
    HIGH_QUALITY,
    VOICE_QUALITY
};    typedef struct
{
    DWORD  dwConfig;      // BE_CONFIG_XXXXX
                          // Currently only BE_CONFIG_MP3 is supported
    union
    {
        struct
        {
            DWORD  dwSampleRate;  // 48000, 44100 and 32000 allowed
            BYTE   byMode;        // BE_MP3_MODE_STEREO, BE_MP3_MODE_DUALCHANNEL
                                  // BE_MP3_MODE_MONO
            WORD   wBitrate;      // 32, 40, 48, 56, 64, 80, 96, 112, 128,
                                  // 160, 192, 224, 256 and 320 allowed
            BOOL  bPrivate;
            BOOL  bCRC;
            BOOL  bCopyright;
            BOOL  bOriginal;
        }mp3;                     // BE_CONFIG_MP3
        struct
        {
            // STRUCTURE INFORMATION
            DWORD      dwStructVersion;
            DWORD      dwStructSize;
            // BASIC ENCODER SETTINGS
            DWORD      dwSampleRate;   // ALLOWED SAMPLERATE VALUES DEPENDS
                                       // ON dwMPEGVersion
            DWORD      dwReSampleRate; // DOWNSAMPLERATE, 0=ENCODER DECIDES
            INT        nMode;          // BE_MP3_MODE_STEREO, BE_MP3_MODE_DUALCHANNEL
                                       // BE_MP3_MODE_MONO
            DWORD      dwBitrate;      // CBR bitrate, VBR min bitrate
            DWORD      dwMaxBitrate;   // CBR ignored, VBR Max bitrate
            MPEG_QUALITY  nQuality;    // Quality setting (NORMAL,HIGH,LOW,VOICE)
            DWORD      dwMpegVersion;  // MPEG-1 OR MPEG-2
            DWORD      dwPsyModel;     // FUTURE USE, SET TO 0
            DWORD      dwEmphasis;     // FUTURE USE, SET TO 0                // BIT STREAM SETTINGS
            BOOL      bPrivate;        // Set Private Bit (TRUE/FALSE)
            BOOL      bCRC;            // Insert CRC (TRUE/FALSE)
            BOOL      bCopyright;      // Set Copyright Bit (TRUE/FALSE)
            BOOL      bOriginal;       // Set Original Bit (TRUE/FALSE)
       
            // VBR STUFF
            BOOL      bWriteVBRHeader; // WRITE XING VBR HEADER (TRUE/FALSE)
            BOOL      bEnableVBR;      // USE VBR ENCODING (TRUE/FALSE)
            INT        nVBRQuality;    // VBR QUALITY 0..9
            BYTE      btReserved[255]; // FUTURE USE, SET TO 0
        }LHV1;                         // LAME header version 1            struct
        {
            DWORD  dwSampleRate;
            BYTE  byMode;
            WORD  wBitrate;
            BYTE  byEncodingMethod;
        }aac;
    }format;  
}BE_CONFIG;    struct BE_VERSION
{
    // BladeEnc DLL Version number
    BYTE  byDLLMajorVersion;
    BYTE  byDLLMinorVersion;
    // BladeEnc Engine Version Number
    BYTE  byMajorVersion;
    BYTE  byMinorVersion;
    // DLL Release date
    BYTE  byDay;
    BYTE  byMonth;
    WORD  wYear;
    // BladeEnc Homepage URL
    CHAR  zHomepage[BE_MAX_HOMEPAGE + 1];
};    #ifndef _BLADEDLL    typedef BE_ERR  (*BEINITSTREAM)    (BE_CONFIG*, PDWORD, PDWORD, PHBE_STREAM);
typedef BE_ERR  (*BEENCODECHUNK)   (HBE_STREAM, DWORD, PSHORT, PBYTE, PDWORD);
typedef BE_ERR  (*BEDEINITSTREAM)  (HBE_STREAM, PBYTE, PDWORD);
typedef BE_ERR  (*BECLOSESTREAM)   (HBE_STREAM);
typedef VOID    (*BEVERSION)       (BE_VERSION*);    #define TEXT_BEINITSTREAM   "beInitStream"
#define TEXT_BEENCODECHUNK  "beEncodeChunk"
#define TEXT_BEDEINITSTREAM "beDeinitStream"
#define TEXT_BECLOSESTREAM  "beCloseStream"
#define TEXT_BEVERSION      "beVersion"    /*
BE_ERR beInitStream(BE_CONFIG *beConfig, PDWORD dwSamples, PDWORD dwBufferSize,
    PHBE_STREAM phbeStream);
BE_ERR beEncodeChunk(HBE_STREAM hbeStream, DWORD nSamples, PSHORT pSamples, PBYTE pOutput,
    PDWORD pdwOutput);
BE_ERR beDeinitStream(HBE_STREAM hbeStream, PBYTE pOutput, PDWORD pdwOutput);
BE_ERR beCloseStream(HBE_STREAM hbeStream);
VOID beVersion(BE_VERSION *beVersion);
*/    #else    extern "C" __declspec(dllexport) BE_ERR beInitStream(BE_CONFIG *beConfig,
    PDWORD dwSamples, PDWORD dwBufferSize, PHBE_STREAM phbeStream);
extern "C" __declspec(dllexport) BE_ERR  beEncodeChunk(HBE_STREAM hbeStream,
    DWORD nSamples, PSHORT pSamples, PBYTE pOutput, PDWORD pdwOutput);
extern "C" __declspec(dllexport) BE_ERR  beDeinitStream(HBE_STREAM hbeStream,
    PBYTE pOutput, PDWORD pdwOutput); extern "C" __declspec(dllexport) BE_ERR  beCloseStream(HBE_STREAM hbeStream);
extern "C" __declspec(dllexport) VOID    beVersion(BE_VERSION *beVersion);    #endif
#pragma pack(pop)
#endif    As you can see in the header above, you have to add  #define _BLADEDLL into your .cpp file before including the header.    Below, you'll find the code of a little application which takes a wav file in input and encode the file to mp3. I don't give more explanations because the code is very straightforward and commented. It is not very elegant but it's just to show how to use the DLL.    File Format.h
//---------------------------------------------------------------------------
#ifndef Format_H
#define Format_H
//---------------------------------------------------------------------------
struct FormatChunk
{
    char            chunkID[4];
    long            chunkSize;
    short           wFormatTag;
    unsigned short  wChannels;
    unsigned long   dwSamplesPerSec;
    unsigned long   dwAvgBytesPerSec;
    unsigned short  wBlockAlign;
    unsigned short  wBitsPerSample;
    // Note: there may be additional fields here, depending upon wFormatTag.
};    // This is the start ID of a Wave file
// must contains 'RIFF' and 'WAVE'
char startID[12];    // contains the chunk id ('data', 'cue ' ...) and the chunk size
struct Chunk
{
    char            chunkID[4];
    long            chunkSize;
};    // a pointer to the samples in the data chunk
unsigned char     *WaveformData;    //---------------------------------------------------------------------------
#endif    File Unit1_H.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include 
#include 
#include 
#include <Forms.hpp>
#include 
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:  // IDE-managed Components
    TOpenDialog *OpenDialog1;
    TEdit *FileEdit;
    TLabel *Label1;
    TButton *Browse;
    TButton *Encode;
    void __fastcall BrowseClick(TObject *Sender);
    void __fastcall EncodeClick(TObject *Sender);
private:  // User declarations
    AnsiString OutputFileName;
public:    // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif    Fiel Unit1.cpp
//---------------------------------------------------------------------------
#include 
#pragma hdrstop    #include 
#include 
#include "Unit1.h"    #define _BLADEDLL           // Don't forget it
#include "lame_enc.h"
#include "format.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BrowseClick(TObject *Sender)
{
    OpenDialog1->InitialDir = ExtractFileDir(Application->ExeName);
    if(OpenDialog1->Execute())
    {
        FileEdit->Text = OpenDialog1->FileName;
        OutputFileName = ChangeFileExt(OpenDialog1->FileName, ".mp3");
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::EncodeClick(TObject *Sender)
{
    if(FileEdit->Text == "")
        return;
    std::ifstream fin(FileEdit->Text.c_str(), std::ios::binary);
    if(!fin)
        return;
    // read the 12 character in front of the file
    fin.read((char*)&startID, sizeof(startID));        // get the format chunk
    FormatChunk fc;
    fin.read((char*)&fc, sizeof(FormatChunk));
    // the first chunk MUST be the format chunk
    if(strncmp(fc.chunkID, "fmt ", 4) != 0)
    {
        Application->MessageBox("This is not a valid Wave file",
                            "Wav2Mp3 ERROR", MB_OK);
        return;
    }
    if(fc.wFormatTag!=1)
    {
        Application->MessageBox("Cannot handle compressed Wave file",
                            "Wav2Mp3 ERROR", MB_OK);
        return;
    }
    // initialization of Mp3 encoder
    BE_CONFIG bc;
    bc.dwConfig = BE_CONFIG_MP3;
    // 32000, 44100 and 48000 are the only sample rate authorized
    // due to encoding limitations
    if(fc.dwSamplesPerSec == 32000 || fc.dwSamplesPerSec == 44100 ||
            fc.dwSamplesPerSec == 48000)
        bc.format.mp3.dwSampleRate = fc.dwSamplesPerSec;
    else
    {
        Application->MessageBox("Unsuported sample rate",
                            "Wav2Mp3 ERROR", MB_OK);
        return;
    }
    if(fc.wChannels == 1)
        bc.format.mp3.byMode = BE_MP3_MODE_MONO;
    else
        bc.format.mp3.byMode = BE_MP3_MODE_STEREO;
    // the resulting file length depends on this parameter
    // higher the bitrate, better the result
    bc.format.mp3.wBitrate = 192;
    bc.format.mp3.bCopyright = false;
    bc.format.mp3.bCRC = false;
    bc.format.mp3.bOriginal = false;
    bc.format.mp3.bPrivate = false;
    // skip extra formatchunk parameter, if any
    if(sizeof(FormatChunk) < int(8 + fc.chunkSize))
    {
        char c;
        for(int i=0; i< int(8 + fc.chunkSize - sizeof(FormatChunk)); i++)
            fin.get(c);
    }
    // get next chunk
    Chunk chunk;
    fin.read((char*)&chunk, sizeof(Chunk));
    // check if it's the data chunk
    while(strncmp(chunk.chunkID, "data", 4) != 0)
    {
        char c;
        for(int i=0; iMessageBox("Cannot perform compression",
                            "Wav2Mp3 ERROR", MB_OK);
        return;
    }
    std::ofstream fout(OutputFileName.c_str(), std::ios::binary);
    char *Mp3Buffer = new char[dwOutputBufferLength];
    SHORT *InputBuffer = new SHORT[dwNumberOfSamples];      // SHORT = short = 16 bits        int nSamplesPerformed=0;
    DWORD dwNumberOfSamplesEncoded;
    while(nSamplesPerformed < chunk.chunkSize)
    {
        fin.read((char*)InputBuffer, dwNumberOfSamples * 2);
        nSamplesPerformed += dwNumberOfSamples * 2;
        if(beEncodeChunk(hStream, dwNumberOfSamples, InputBuffer,
                (BYTE*)Mp3Buffer, &dwNumberOfSamplesEncoded) != BE_ERR_SUCCESSFUL)
        {
            Application->MessageBox("Cannot perform compression",
                              "Wav2Mp3 ERROR", MB_OK);
            return;
        }
        fout.write(Mp3Buffer, dwNumberOfSamplesEncoded);
    }
    beDeinitStream(hStream, (BYTE*)Mp3Buffer, &dwNumberOfSamplesEncoded);
    beCloseStream(hStream);        delete Mp3Buffer;
    delete InputBuffer;
    return;
}    >>上篇文章: 通过修改VCL源码实现自定义输入对话框
>>下篇文章: 一个使用多媒体定时器的简单例子    C++Builder 研究 - http://www.ccrun.com 晋ICP备05000574号    版权所有 (C) 2001,2005 C++Builder 研究. 保留所有权利    Copyright © www.ccrun.com, Inc. All rights reserved.     謝謝回答    Try it!
------
Try it!
bugmans
高階會員


發表:95
回覆:322
積分:188
註冊:2003-04-12

發送簡訊給我
#10 引用回覆 回覆 發表時間:2005-08-09 05:00:29 IP:218.166.xxx.xxx 未訂閱
引言: 請問一下,如果要把錄下來的改成mp3是要怎麼改? 是要改成wFormatTag=WAVE_FORMAT_MPEG嗎?我試過有錯誤訊息,而且我其他的參數不知道該怎麼設定。查了MSDN不知道是我不會查還是怎樣,找到相關的但是也沒寫出該怎麼用成MP3的格式,麻煩知道的大大告訴我。謝謝囉
OGOLive MP3 Encoder 7/11/2003 http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=46825&lngWId=1 1/7/2004 http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=50842&lngWId=1 作者說可以即時將音源轉換成mp3檔案,但是我手邊沒有麥克風 我將原始碼編譯成執行檔附上gogo.dll請網友測試一下 http://delphi.ktop.com.tw/loadfile.php?TOPICID=23930428&CC=535192
雲中鵝
一般會員


發表:14
回覆:24
積分:7
註冊:2004-12-21

發送簡訊給我
#11 引用回覆 回覆 發表時間:2005-09-07 14:44:00 IP:140.125.xxx.xxx 未訂閱
引言: 有棒的程式喔~~ 雖然會用...但不知道每個程式碼代表的意義 可以請你說明一下或是哪裡有這方面的教學說明 謝謝~~^0^
還有一個方式可以實現出錄音功能... 但是還未成功...請大大能夠指教一下.... 要如何得到device identifier 因為我想以capGetAudioFormat函數得到.但是錄起來的內容好像是錯的 data: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_waveinopen.asp code
struct  tinlevelstreamdata
        {
        WAVEHDR        pwavehdr[3];
        WAVEFORMATEX    waveformat;
        HWAVEIN         hwi;
        int             buffersize;
        };
int check;
WAVEINCAPS      nil;
tinlevelstreamdata      fininfo;
static int SNDSIZEBUF        =320;
static int SNDNBUF        =4;
char hdrData[SNDSIZEBUF*SNDNBUF];
void __fastcall TForm1::Button1Click(TObject *Sender)
{
        if(waveInGetNumDevs()==0)
        {
        ShowMessage("無聲音輸入裝置!");
        exit(0);
        }
//format
fininfo.waveformat.wFormatTag =WAVE_FORMAT_PCM;
fininfo.waveformat.nChannels =1;
fininfo.waveformat.nSamplesPerSec =44100;
fininfo.waveformat.cbSize =0;
fininfo.waveformat.wBitsPerSample=8;
fininfo.waveformat.nBlockAlign =(fininfo.waveformat.wBitsPerSample*fininfo.waveformat.nChannels )/8;
fininfo.waveformat.nAvgBytesPerSec =fininfo.waveformat.nSamplesPerSec *fininfo.waveformat.nBlockAlign ;    //test open type
check=waveInOpen(NULL,WAVE_MAPPER,&fininfo.waveformat,0,0,WAVE_FORMAT_QUERY);
if(check!=MMSYSERR_NOERROR)
        {
       char error[100];
        waveInGetErrorText(check,error,100);
        ShowMessage(error);
        }
capSetAudioFormat(this,&fininfo.waveformat,sizeof (WAVEFORMATEX));
//capGetAudioFormat(this,&fininfo.waveformat,sizeof(WAVEFORMATEX));
//to    open input device type
fininfo.buffersize =fininfo.waveformat.nAvgBytesPerSec /20;
fininfo.hwi =0;
check=waveInOpen(&fininfo.hwi,WAVE_MAPPER,&fininfo.waveformat,
0,0,CALLBACK_EVENT);
/////////為了得到其輸入裝置
capGetAudioFormat(this,&fininfo.waveformat,sizeof(WAVEFORMATEX));
//TWAVEINCAPS     in;    if(check!=MMSYSERR_NOERROR)
        {
                        char error[100];
                        waveInGetErrorText(check,error,100);
                        ShowMessage(error);
        }
 //memset(fininfo.pwavehdr,0,sizeof(WAVEHDR)*SNDNBUF);
 for(int i=0;i原本是為了得到四個空間,但是//不知為何會出錯.故減一
 {
        fininfo.pwavehdr[i].lpData =hdrData   i*SNDSIZEBUF;
        fininfo.pwavehdr[i].dwBufferLength =SNDSIZEBUF;
        fininfo.pwavehdr[i].dwBytesRecorded =0;
        fininfo.pwavehdr[i].dwUser =0;
        fininfo.pwavehdr[i].dwFlags =0;
        fininfo.pwavehdr[i].dwLoops =0;
//      &fininfo.pwavehdr[i]=nil;
        check=waveInPrepareHeader(fininfo.hwi ,fininfo.pwavehdr  i,
                sizeof(WAVEHDR) );
                if(check!=MMSYSERR_NOERROR)
                {
                char error[100];
                waveInGetErrorText(check,error,100);
                exit(0);
                }
        check=waveInAddBuffer(fininfo.hwi ,
        &fininfo.pwavehdr[i],sizeof(WAVEHDR));
                if(check!=MMSYSERR_NOERROR)
                {
                char error[100];
                waveInGetErrorText(check,error,100);
                exit(0);
                }
 }
//開始錄音.
 check=waveInStart(fininfo.hwi);
                if(check!=MMSYSERR_NOERROR)
                {
                char error[100];
                waveInGetErrorText(check,error,100);
                exit(0);
                }
 
謝謝分享 Try it!
------
Try it!
nike906
一般會員


發表:0
回覆:1
積分:0
註冊:2013-03-11

發送簡訊給我
#12 引用回覆 回覆 發表時間:2013-03-11 16:44:24 IP:203.74.xxx.xxx 訂閱
 請問各位大大
完整的程式碼就是這些嗎
我開檔進去都有錯誤
好像說沒有include到標頭檔

------
hi!!!
系統時間:2024-11-22 14:47:28
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!