Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define MA_NO_DECODING
- #define MA_NO_ENCODING
- #define MINIAUDIO_IMPLEMENTATION
- #include "miniaudio.h"
- #define DEVICE_FORMAT ma_format_f32
- #define DEVICE_CHANNELS 1
- #define DEVICE_SAMPLE_RATE 48000
- #include "miniaudio.h"
- void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount){
- /*In playback mode copy data to pOutput. In capture mode read data from pInput. In full-duplex mode, both
- pOutput and pInput will be valid and you can move data from pInput into pOutput. Never process more than
- frameCount frames.*/
- static float saw = 0;
- float* Samples = pOutput; /* "Samples" is just a (castless in this case) cast of the "pOutput" float pointer */
- ma_uint32 SampleIndex;
- float freq = 50;
- float amp = 0.2;
- MA_ASSERT(pDevice->playback.channels == DEVICE_CHANNELS);
- for(SampleIndex = 0; SampleIndex < frameCount; SampleIndex++){
- saw=saw*(saw<(DEVICE_SAMPLE_RATE/freq)); /* zeroes out sq every (1/freq) */
- *Samples = saw/(DEVICE_SAMPLE_RATE/freq)*amp;
- saw++; /* increases sq >> so I have a rising sawthoot */
- Samples++;}
- (void)pInput;}
- int main(){
- ma_device_config deviceConfig;
- ma_device device;
- /* audio output device configuration */
- deviceConfig = ma_device_config_init(ma_device_type_playback); /* initialize for playback (not capture) */
- deviceConfig.playback.format = DEVICE_FORMAT;
- deviceConfig.playback.channels = DEVICE_CHANNELS;
- deviceConfig.sampleRate = DEVICE_SAMPLE_RATE;
- deviceConfig.dataCallback = data_callback; /* name of the callback function */
- /*deviceConfig.pUserData = &sineWave;*/ /* this data is fed to data_callback (Can be accessed from the device object (device.pUserData) inside the callback) */
- /*************************************/
- /* audio output device initialization */
- if(ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS){
- printf("Failed to open playback device.\n");
- return -4;}
- /**************************************/
- printf("== Device Name: %s\n", device.playback.name);
- printf("== Press Enter to start...");
- getchar();
- /* this is the actual sound start */
- if (ma_device_start(&device) != MA_SUCCESS) {
- printf("== Failed to start playback device.\n");
- ma_device_uninit(&device);
- return -5;}
- /* Do something here. Probably your program's main loop. */
- printf("~~~ You should hear sound now ~~~\n");
- printf("== Press Enter to quit...");
- getchar();
- ma_device_uninit(&device); /* turn off sound */
- return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement