build a music keyboard, with sampled sound

I plan to build a music keyboard with sampled sound, recorded previously from piano, for example.

My initial idea is to use multi-thread lib like scoop or freeRtos.

One thread/task keep checking which key has been pressed. Once it monitors some key pressed, it change some global variable to inform the sound generator.
A sound generator thread/task keep checking the global variable and will play the sound wave file.

The problem is that. Playing sound wave file is blocking. if one release the pressed key, the sound play should stop immediately. I don't find a way to do this.

A discussion on the mutiplexing doesn't help:

A general approach is to read a few bytes from file (just enough as needed to control the sound generator), send it to the sound generator, test if a key is pressed/released, read a few bytes again and send to the sound generator etc. If the key is released, stop this loop.

You will need to post your code that plays back a file for us to be able to see how it can be stopped.

Your can read Robin's Demonstration code for several things at the same time for an alternative approach to multitasking.

You need hardware that can play pre-recorded sound. A simple Arduino cannot do that.

sterretje:
A general approach is to read a few bytes from file (just enough as needed to control the sound generator), send it to the sound generator, test if a key is pressed/released, read a few bytes again and send to the sound generator etc. If the key is released, stop this loop.

You will need to post your code that plays back a file for us to be able to see how it can be stopped.

Your can read Robin's Demonstration code for several things at the same time for an alternative approach to multitasking.

Thanks. Good to learn. Is this the typical way, other commercial musical instrument maker (yamaha, casio, roland, korg) do for their arranger keyboards?

I would say: 'no'.

My guess is that use much more powerful processors/chips with more memory and built in DACs, AMPs...etc..

You do NOT get these things with an Arduino.

I would look to use an external board with DAC, AMP....etc..etc.. instead of say using the Adafruit Wave Shield....

something that does the brunt of the audio playback and frees up the Arduino for other things..

So your basic idea is that let arduino focus on monitoring keys and inform the audio board to start/stop playing audio at key press/release moment, right? Sounds a feasible design and probably easier than reading a small piece of the audio every time one.

I have purchased some extension boards which could drive speakers and another board which could play mp3. I found some similar system called teensy. And it seems to have some ability to do other things while playing audio. I guess it is due to the fact that it has a much more powerful mcu?

The pdf manual for reference:
https://www.pjrc.com/store/audio_tutorial_kit.html

joedodo:
So your basic idea is that let arduino focus on monitoring keys and inform the audio board to start/stop playing audio at key press/release moment, right?

Indeed. Use the correct tool for the job. Arduinos are great at taking inputs and providing signals. They're not suited to play audio as such (Teensy's also not - they're much faster so can handle a lot more of course).

Using something like WAV player chips you may be able to tell it to start/stop playing sounds, including when it's halfway - depending on how that piece of hardware works.

Those musical instruments use dedicated hardware to produce the sounds.

sterretje:
A general approach is to read a few bytes from file (just enough as needed to control the sound generator), send it to the sound generator, test if a key is pressed/released, read a few bytes again and send to the sound generator etc. If the key is released, stop this loop.

You will need to post your code that plays back a file for us to be able to see how it can be stopped.

Your can read Robin's Demonstration code for several things at the same time for an alternative approach to multitasking.

Thanks. Will this cause jitter frequently? Do you think in practice, a mega 2560 is sufficient for this task?