# How to make a synth voice play back a wav file on a sample-by-sample basis?

**URL:** <https://forum.juce.com/t/how-to-make-a-synth-voice-play-back-a-wav-file-on-a-sample-by-sample-basis/39144>\
**Category:** General JUCE discussion\
**Created:** [May 2, 2020, 9:00am UTC](https://forum.juce.com/t/how-to-make-a-synth-voice-play-back-a-wav-file-on-a-sample-by-sample-basis/39144 "2020-05-02T09:00:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikej](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@mikej](https://forum.juce.com/u/mikej)\
**Post date:** [May 2, 2020, 9:00am UTC](https://forum.juce.com/t/how-to-make-a-synth-voice-play-back-a-wav-file-on-a-sample-by-sample-basis/39144/1 "2020-05-02T09:00:22Z")

</div>

I want to try to trigger a WAV file to play back in sample-by-sample processing within my MPESynthVoice under `renderNextBlock`. I am okay with the file location being hardcoded for simple testing.

eg. file is `C:\testwav.wav`

I would like a simple way of having it do something like this:

```
//To trigger playback:
playWav = true;
sampleIndex = 0;

//Playback:
if (playWav == true) {
double wavSampleOutput = wavSample[sampleIndex];
sampleIndex = sampleIndex +1;
}

```

I don’t want to manually put it all the wav file’s values into an array/vector or anything though. I just mean I want to step through the wav file sample-by-sample on command/trigger and get the values so I can use them elsewhere in my synth voice.

The tutorial I see on playing back audio samples is all about block/buffer based processing:

[https://docs.juce.com/master/tutorial\_playing\_sound\_files.html](https://docs.juce.com/master/tutorial_playing_sound_files.html)

Is there any simple way to do what I’m talking about? Could you share a bit of example code for how it would work? Thanks.

---

<div class="post-metadata">

**Author:** ![mikej](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@mikej](https://forum.juce.com/u/mikej)\
**Post date:** [May 2, 2020, 5:30pm UTC](https://forum.juce.com/t/how-to-make-a-synth-voice-play-back-a-wav-file-on-a-sample-by-sample-basis/39144/2 "2020-05-02T17:30:28Z")

</div>

If the only way to do this is to iterate through the samples of the wav file and add them to a vector to create a wave table on initialization of the voice/synth, I’m okay with that too. I just don’t want to have to manually create the vector of all the per sample wav values myself. Is it possible to make JUCE do that?

---

<div class="post-metadata">

**Author:** ![WilliamkWusik](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/williamkwusik/32/3821_2.png) [@WilliamkWusik](https://forum.juce.com/u/WilliamkWusik)\
**Post date:** [May 2, 2020, 6:50pm UTC](https://forum.juce.com/t/how-to-make-a-synth-voice-play-back-a-wav-file-on-a-sample-by-sample-basis/39144/3 "2020-05-02T18:50:41Z")

</div>

You need to load the whole WAV file into memory. You can stream, but takes a bit more work. If I recall JUCE does have a code for that, but I don’t remember what it was.
