2015-12-30 14 views
0

我目前使用與OpenAL的這些設置和記錄從麥克風:使用OpenAL記錄最低質量可能嗎?

BUFFERSIZE 4410 
FREQ 22050 // Sample rate 
CAP_SIZE 10000 // How much to capture at a time (affects latency) 
AL_FORMAT_MONO16 

是否有可能去錄音質量低?我試過降低採樣率,但最終的結果是播放速度更快。

回答

0

好了,所以這是一些我曾經寫過的最哈克代碼,我真的希望沒有一個心智正常的人曾經使用它在生產.. 。sooooo許多壞事。

但是要回答你的問題,我已經能夠將質量下降到11025的8bitMono錄音。但是,我從麥克風錄製的所有內容都帶有大量的靜態數據,我並不完全確定我知道爲什麼。我已經生成了8bit karplus強勁的絃樂器,聽起來很棒,所以它可能只是我的錄音設備。

#include <AL/al.h> 
#include <AL/alc.h> 
#include <conio.h> 
#include <stdio.h> 
#include <vector> 
#include <time.h> 

void sleep(clock_t wait) 
{ 
    clock_t goal; 
    goal = wait + clock(); 
    while(goal > clock()) 
     ; 
} 

#define BUFFERSIZE 4410 
const int SRATE = 11025; 

int main() 
{ 
    std::vector<ALchar> vBuffer; 
    ALCdevice  *pDevice = NULL; 
    ALCcontext  *pContext = NULL; 
    ALCdevice  *pCaptureDevice; 
    const ALCchar *szDefaultCaptureDevice; 
    ALint   iSamplesAvailable; 
    ALchar   Buffer[BUFFERSIZE]; 
    ALint   iDataSize = 0; 
    ALint   iSize; 

    // NOTE : This code does NOT setup the Wave Device's Audio Mixer to select a recording input 
    // or a recording level. 

    pDevice = alcOpenDevice(NULL); 
    pContext = alcCreateContext(pDevice, NULL); 
    alcMakeContextCurrent(pContext); 

    printf("Capture Application\n"); 

    if (pDevice == NULL) 
    { 
     printf("Failed to initialize OpenAL\n"); 
     //Shutdown code goes here 
     return 0; 
    } 

    // Check for Capture Extension support 
    pContext = alcGetCurrentContext(); 
    pDevice = alcGetContextsDevice(pContext); 
    if (alcIsExtensionPresent(pDevice, "ALC_EXT_CAPTURE") == AL_FALSE){ 
     printf("Failed to detect Capture Extension\n"); 
     //Shutdown code goes here 
     return 0; 
    } 

    // Get list of available Capture Devices 
    const ALchar *pDeviceList = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER); 
    if (pDeviceList){ 
     printf("\nAvailable Capture Devices are:-\n"); 

     while (*pDeviceList) 
     { 
      printf("%s\n", pDeviceList); 
      pDeviceList += strlen(pDeviceList) + 1; 
     } 
    } 

    // Get the name of the 'default' capture device 
    szDefaultCaptureDevice = alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER); 
    printf("\nDefault Capture Device is '%s'\n\n", szDefaultCaptureDevice); 

    pCaptureDevice = alcCaptureOpenDevice(szDefaultCaptureDevice, SRATE, AL_FORMAT_MONO8, BUFFERSIZE); 
    if (pCaptureDevice) 
    { 
     printf("Opened '%s' Capture Device\n\n", alcGetString(pCaptureDevice, ALC_CAPTURE_DEVICE_SPECIFIER)); 

     // Start audio capture 
     alcCaptureStart(pCaptureDevice); 

     // Wait for any key to get pressed before exiting 
     while (!_kbhit()) 
     { 
      // Release some CPU time ... 
      sleep(1); 

      // Find out how many samples have been captured 
      alcGetIntegerv(pCaptureDevice, ALC_CAPTURE_SAMPLES, 1, &iSamplesAvailable); 

      printf("Samples available : %d\r", iSamplesAvailable); 

      // When we have enough data to fill our BUFFERSIZE byte buffer, grab the samples 
      if (iSamplesAvailable > (BUFFERSIZE/2)) 
      { 
       // Consume Samples 
       alcCaptureSamples(pCaptureDevice, Buffer, BUFFERSIZE/2); 

       // Write the audio data to a file 
       //fwrite(Buffer, BUFFERSIZE, 1, pFile); 
       for(int i = 0; i < BUFFERSIZE/2; i++){ 
        vBuffer.push_back(Buffer[i]); 
       } 

       // Record total amount of data recorded 
       iDataSize += BUFFERSIZE/2; 
      } 
     } 

     // Stop capture 
     alcCaptureStop(pCaptureDevice); 

     // Check if any Samples haven't been consumed yet 
     alcGetIntegerv(pCaptureDevice, ALC_CAPTURE_SAMPLES, 1, &iSamplesAvailable); 
     while (iSamplesAvailable) 
     { 
      if (iSamplesAvailable > (BUFFERSIZE/2)) 
      { 
       alcCaptureSamples(pCaptureDevice, Buffer, BUFFERSIZE/2); 
       for(int i = 0; i < BUFFERSIZE/2; i++){ 
        vBuffer.push_back(Buffer[i]); 
       } 
       iSamplesAvailable -= (BUFFERSIZE/2); 
       iDataSize += BUFFERSIZE; 
      } 
      else 
      { 
       //TODO::Fix 
       alcCaptureSamples(pCaptureDevice, Buffer, iSamplesAvailable); 
       for(int i = 0; i < BUFFERSIZE/2; i++){ 
        vBuffer.push_back(Buffer[i]); 
       } 
       iDataSize += iSamplesAvailable * 2; 
       iSamplesAvailable = 0; 
      } 
     } 

     alcCaptureCloseDevice(pCaptureDevice); 
    } 

    //TODO::Make less hacky 
    ALuint bufferID;      // The OpenAL sound buffer ID 
    ALuint sourceID;      // The OpenAL sound source 

    // Create sound buffer and source 
    alGenBuffers(1, &bufferID); 
    alGenSources(1, &sourceID); 

    alListener3f(AL_POSITION, 0.0f, 0.0f, 0.0f); 
    alSource3f(sourceID, AL_POSITION, 0.0f, 0.0f, 0.0f); 

    alBufferData(bufferID, AL_FORMAT_MONO8, &vBuffer[0], static_cast<ALsizei>(vBuffer.size()), SRATE); 

    // Attach sound buffer to source 
    alSourcei(sourceID, AL_BUFFER, bufferID); 

    // Finally, play the sound!!! 
    alSourcePlay(sourceID); 

    printf("Press any key to continue..."); 
    getchar(); 

    return 0; 
} 

正如你可以看到:

alBufferData(bufferID, AL_FORMAT_MONO8, &vBuffer[0], static_cast<ALsizei>(vBuffer.size()), SRATE); 

我驗證過這種情況。對於演示代碼,我可以將這個例子扔到那裏,但是我不會在生產中使用它。

+0

是的,我也得到靜態/雪。沒關係。 – Taurian

0

我不確定,但對我來說,FREQ是輸出頻率,但不是採樣率。 定義採樣率48000 看到此鏈接:http://supertux.lethargik.org/wiki/OpenAL_Configuration

+0

看到這個:http://stackoverflow.com/questions/4087727/openal-how-to-create-simple-microphone-echo-programm 我修改了這段代碼。 – Taurian

+0

還設置採樣率11020使音頻聲音像米奇老鼠。將其設置爲48000使其聽起來很慢。 – Taurian

+0

如果您是從文件/緩衝區播放,您仍然有相同數量的原始數據正在嘗試播放,所以此行爲是可以預料的。當你說「質量較低」時,你可以將你的文件/緩衝區轉換爲8bit單聲道或8khz,然後嘗試播放它。 –