Samples and envelopes become associated with an instrument by creating an attachment item. You can use the same sample on multiple instruments, or you can use multiple samples on one instrument. What you really need to know is when a specific attachment has finished. There are two ways to do this:
The first is to call MonitorAttachment() to request a signal when the sample or envelope has finished. This method can be used with WaitSignal() which lets a task sleep until the attachment is finished.
The second way is to set the AF_ATTF_FATLADYSINGS flag in the attachment. When the attachment finishes, it stops the associated instrument. You can then poll the instrument by calling GetAudioItemInfo() with AF_TAG_STATUS.
Be careful when you are polling for an instrument to finish. Never loop continuously while polling. This is called busy waiting. Busy waiting takes up CPU cycles and can cause a drop in performance or a deadlock.