How to play a sound in the middle of the sound?

(I can't leave a project cause apparently a two-measure song split into 4 parts.)
I would like to manually cross-fade instruments together into a song. How do you start a sound in the middle of the sound? Also is there a way to track where you are in a sound you're playing so I can sync them up?

Edit: BTW I mean media, not MIDI.

image

Wow. That is interesting. Thanks!

No need for JavaScript here. Just get rid of the first (seconds * sample rate) samples of the sound.
play sound n seconds in

It didn't work. I'm getting
"Failed to execute 'createBuffer' on 'BaseAudioContext': the number of frames provided (0) is less than or equal to the minimum bound (0)."

That means you're trimming off all the samples in the sound. Either seconds is too high, or there's some other mistake.

Actually, I'm looking at the code. It doesn't make sense. Why do you multiply seconds by sample rate? That just gives 44100.

Sound is a wave. It has an amplitude, which is a positive or negative "height" that varies over time. Storing the exact shape of this wave is impractical, so digital sound is stored as a list of samples, which tell you the magnitude at specific points of time, separated by a fixed interval:


Usually, samples are recorded every 1/44100 seconds, and taking the inverse of this interval gives you the sample rate of 44100 hertz (samples per second). So when you want to get rid of the first N seconds of a sound, you multiply N by the sample rate, and that tells you the number of samples you need to remove from the beginning of the sample list.

So if I want to start 1 second in, I remove 44100 samples. That makes sense.
But why doesn't this work?


It should have 6 seconds length.

Nvm I guess it doesn't matter anyway. It'd be nice to know, but I can't track the position within the song anyway, so I'd have to use the JS. I got it to work with that. Thanks!
As a side note... I have a function borrowing code from parallel running scripts and used [launch] to start a loop in a function.

But... I can't use messages to [stop loops] in a different block since message retains the original broadcast. How else would I do this. (I'm doing it in here so I don't have a million topics for one project.)

Alright, I forgot to account for sounds with more than one channel. For sounds with one channel, the samples block gives you a list of samples, but for sounds with more than one, the block gives you a list of lists of samples.

This is getting too complicated just for screenshots so I've shared an example here: https://snap.berkeley.edu/snap/snap.html#cloud:Username=djdolphin&ProjectName=sound%20trimmer

Thanks!