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

Resampling while streaming #79

Open
marvinh opened this issue Nov 22, 2024 · 1 comment
Open

Resampling while streaming #79

marvinh opened this issue Nov 22, 2024 · 1 comment

Comments

@marvinh
Copy link

marvinh commented Nov 22, 2024

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 .

@marvinh
Copy link
Author

marvinh commented Nov 23, 2024

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;
};

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

No branches or pull requests

1 participant