ni-media
ni-media copied to clipboard
Resampling while streaming
https://gist.github.com/marvinh/f0f36dd176566f99ba2a252dbfe7005e
I have a gist here if you want to include in examples with juce. super thread safe I think I have no issues .
this is much better not streaming but loving this library edited: for caching sample position of course needs a little work correct bit depth based on the info and interleaving properly
class SampleReader {
public:
SampleReader(const std::string& filename){
stream = audio::ifstream(filename);
num_frames = stream.info().num_frames();
num_samples = stream.info().num_samples();
num_channels = stream.info().num_channels();
sample_rate = stream.info().sample_rate();
fm = boost::interprocess::file_mapping(filename.c_str(), boost::interprocess::read_only);
mr = boost::interprocess::mapped_region(fm, boost::interprocess::read_only);
mr.advise( boost::interprocess::mapped_region::advice_willneed);
mr.advise( boost::interprocess::mapped_region::advice_sequential);
mm = static_cast<char*>(mr.get_address())+44;
rt_fmt = pcm::make_format(pcm::signed_integer,pcm::_24bit, pcm::little_endian);
rt_it = pcm::make_iterator<float,char*>(mm, rt_fmt);
data.resize(96,0);
}
size_t lastPosition = 0;
float readSample(size_t position, int cache) {
if(position < 0 || position > num_frames) {
return 0.0f;
}
if(cache == 0 && position != lastPosition) {
pcm::iterator<float, char*> m = (rt_it+position);
for(int i = 0; i < 24; i++) {
data[i] = *m++;
}
lastPosition = position;
return data[0];
}else{
return data[cache];
}
}
std::vector<float> data;
boost::interprocess::file_mapping fm;
boost::interprocess::mapped_region mr;
pcm::format rt_fmt;
pcm::iterator<float, char*> rt_it;
char * mm;
audio::ifstream stream;
size_t num_frames;
size_t num_samples;
size_t num_channels;
double sample_rate;
};