c++ - RtAudio - Playing samples from wav file -
i trying learn audio programming. goal open wav file, extract , play samples rtaudio.
i made waveloader class let's me extract samples , meta data. used this guide , checked correct 010 editor. here snapshot of 010 editor showing structure , data.
and how store raw samples inside waveloader class:
data = new short[wave_data.payloadsize]; // - allocates memory size of chunk size if (!fread(data, 1, wave_data.payloadsize, sound_file)) { throw ("could not read wav data"); }
if print out each sample : 1, -3, 4, -5 ... seems ok.
the problem not sure how can play them. i've done:
/* * using portaudio play samples */ bool player::play() { showdevices(); rt.showwarnings(true); rtaudio::streamparameters oparameters; //, iparameters; oparameters.deviceid = rt.getdefaultoutputdevice(); oparameters.firstchannel = 0; oparameters.nchannels = maudio.channels; //iparameters.deviceid = rt.getdefaultinputdevice(); //iparameters.nchannels = 2; unsigned int samplerate = maudio.samplerate; // use buffer of 512, need feed callback 512 bytes everytime! unsigned int nbufferframes = 512; rtaudio::streamoptions options; options.flags = rtaudio_schedule_realtime; options.flags = rtaudio_noninterleaved; //¶meters, null, rtaudio_float64,samplerate, &bufferframes, &mcallback, (void *)&rawdata try { rt.openstream(&oparameters, null, rtaudio_sint16, samplerate, &nbufferframes, &mcallback, (void*) &maudio); rt.startstream(); } catch (rtaudioerror& e) { std::cout << e.getmessage() << std::endl; return false; } return true; } /* * rtaudio callback * */ int mcallback(void * outputbuffer, void * inputbuffer, unsigned int nbufferframes, double streamtime, rtaudiostreamstatus status, void * userdata) { unsigned int = 0; short *out = static_cast<short*>(outputbuffer); auto *data = static_cast<player::audio_data*>(userdata); // if more our data size, done! if (i > data->datasize) return 1; // first time callback called data->ptr 0, means offset 0 // second time data->ptr 1, means offset = nbufferframes (512) * 1 = 512 unsigned int offset = nbufferframes * data->ptr++; printf("offset: %i\n", offset); // first time callback called offset 0, starting 0 , looping nbufferframes (512) times, gives 512 bytes // second time, offset 1, starting 512 bytes , looping 512 + 512 = 1024 (i = offset; < offset + nbufferframes; ++i) { short sample = data->rawdata[i]; // raw sample our struct *out++ = sample; // pass output buffer playback printf("current sample value: %i\n", sample); // showing 1, -3, 4, -5 check 010 editor } printf("current time: %f\n", streamtime); return 0; }
inside callback function, when print out sample values 010 editor? why isnt rtaudio playing them. wrong here? need normalize sample values between -1 , 1?
edit: wav file trying play:
- chunksize: 16
- format: 1
- channel: 1
- samplerate: 48000
- byterate: 96000
- blockalign: 2
- bitpersample: 16
- size of raw samples total: 2217044 bytes
for reason works when pass input parameters openstream()
rtaudio::streamparameters oparameters, iparameters; oparameters.deviceid = rt.getdefaultoutputdevice(); oparameters.firstchannel = 0; //oparameters.nchannels = maudio.channels; oparameters.nchannels = maudio.channels; iparameters.deviceid = rt.getdefaultinputdevice(); iparameters.nchannels = 1; unsigned int samplerate = maudio.samplerate; // use buffer of 512, need feed callback 512 bytes everytime! unsigned int nbufferframes = 512; rtaudio::streamoptions options; options.flags = rtaudio_schedule_realtime; options.flags = rtaudio_noninterleaved; //¶meters, null, rtaudio_float64,samplerate, &bufferframes, &mcallback, (void *)&rawdata try { rt.openstream(&oparameters, &iparameters, rtaudio_sint16, samplerate, &nbufferframes, &mcallback, (void*) &maudio); rt.startstream(); } catch (rtaudioerror& e) { std::cout << e.getmessage() << std::endl; return false; } return true;
it random when trying playback mic. left input parameters , wav file playing. bug?
Comments
Post a Comment