# Can I create an \`juce::AudioFormatReader\` from a buffer instead of a \`juce::File\`?

**URL:** <https://forum.juce.com/t/can-i-create-an-juce-audioformatreader-from-a-buffer-instead-of-a-juce-file/46277>\
**Category:** General JUCE discussion\
**Created:** [June 1, 2021, 12:59am UTC](https://forum.juce.com/t/can-i-create-an-juce-audioformatreader-from-a-buffer-instead-of-a-juce-file/46277 "2021-06-01T00:59:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![guerlando](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@guerlando](https://forum.juce.com/u/guerlando)\
**Post date:** [June 1, 2021, 12:59am UTC](https://forum.juce.com/t/can-i-create-an-juce-audioformatreader-from-a-buffer-instead-of-a-juce-file/46277/1 "2021-06-01T00:59:25Z")

</div>

I’m trying to modify some code that reads a .wav file from a file and then creates a `juce::SamplerSound` from this file.

On Android it’s not possible to directly read files from the APK, but it’s possible to get their buffer. Since it’s sample files, they are small enough to load entirely on a buffer.

Is there a way to create a `std::unique_ptr<juce::AudioFormatReader> reader` from a buffer instead of a file?

I know that I could write to a file and then pass this `file` to `mFormatManager.createReaderFor(file)` but this would be inneficient

```
void HelloSamplerAudioProcessor::loadBuffer (const juce::String& path, uint8_t* buffer, size_t size)
{
    //no juce::File file anymore, I must find another way to create the reader below
    std::unique_ptr<juce::AudioFormatReader> reader{ mFormatManager.createReaderFor(file) };
    if (reader)
    {
        juce::BigInteger range;
        range.setRange(0, 128, true);
        sampler.addSound(new juce::SamplerSound("Sample", *reader, range, 60, 0.1, 0.1, 10.0));
    } else {
        ALOGE(TAG, "reader error!");
    }
}
```

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [June 1, 2021, 7:07am UTC](https://forum.juce.com/t/can-i-create-an-juce-audioformatreader-from-a-buffer-instead-of-a-juce-file/46277/2 "2021-06-01T07:07:52Z")

</div>

there is a version that takes an InputStrean.  
Use a MemoryInputStream to create one from your buffer.

---

<div class="post-metadata">

**Author:** ![anoesisaudio](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anoesisaudio/32/1132_2.png) [@anoesisaudio](https://forum.juce.com/u/anoesisaudio)\
**Post date:** [June 1, 2021, 1:55pm UTC](https://forum.juce.com/t/can-i-create-an-juce-audioformatreader-from-a-buffer-instead-of-a-juce-file/46277/4 "2021-06-01T13:55:09Z")

</div>

Below is an excerpt from the SamplerPluginDemo demo.

```auto
//==============================================================================
inline std::unique_ptr<AudioFormatReader> makeAudioFormatReader (AudioFormatManager& manager,
                                                                 const void* sampleData,
                                                                 size_t dataSize)
{
    return std::unique_ptr<AudioFormatReader> (manager.createReaderFor (std::make_unique<MemoryInputStream> (sampleData, dataSize, false)));
}

```

You can find this (_plugin_ example) here -

> <https://github.com/juce-framework/JUCE/blob/master/examples/Plugins/SamplerPluginDemo.h>
