Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I get the wave infomation as an array after decode without play the music? #97

Closed
zeroxer opened this issue Oct 2, 2019 · 2 comments
Labels

Comments

@zeroxer
Copy link

zeroxer commented Oct 2, 2019

Hi~
I'm looking a way to extract the mucis/sound wave info into a int array.
I want to know, can I use miniaudio to get the music wave data?
I have try this which I don't it's right or not.

void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
    ma_decoder* pDecoder = (ma_decoder*)pDevice->pUserData;
    if (pDecoder == NULL) {
        return;
    }

    ma_uint64 framesRead = ma_decoder_read_pcm_frames(pDecoder, pOutput, frameCount);
    int index = 0;
    int* tempArray = (int *)pOutput;

    while (index < framesRead)
    {
        std::cout << "\t" << *(tempArray)/48000.0f << "\t";
        tempArray++;
        index++;
    }

    (void)pInput;
}

Can I decode the music without playing it?

@mackron
Copy link
Owner

mackron commented Oct 2, 2019

Can I decode the music without playing it?

Absolutely. Just use ma_decode_file() or ma_decode_memory():

ma_decoder_config audioConfig = ma_decoder_config_init(MY_FORMAT, MY_CHANNELS, MY_SAMPLE_RATE);
ma_uint64 frameCount;
void* pAudioData;
result = ma_decode_file("my_file.wav", &audioConfig, &frameCount, &pAudioData);
if (result != MA_SUCCESS) {
    /* Error */
}

/* At this point the audio data will be in the format you specified in audioConfig. */

You set audioConfig to the audio format you want - just replace MY_FORMAT, etc. to whatever you want. If you need more flexibility you need to use the lower level decoding APIs. Free the pAudioData pointer with ma_free()

You don't need to create a device if you just want to decode audio data from a file.

@zeroxer
Copy link
Author

zeroxer commented Oct 2, 2019

Very thanks! I'll try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants