clc;
clear all;
close all;
rng('shuffle');

% this loads a default sample audio file in MATLAB, a piece of music by Handel.
load handel;
% once loaded, you will note it creates two variables, Fs and y. Fs is the sampling frequency of the music. Y % is a vector of the amplitudes of the signal.

% PYSCHTOOLBOX needs the music to be in a row vector so we have to transpose the matrix y
y = y';

% figure out how many audio channels there are
nrchannels = size(y,1);

% perform basic initialization of the sound driver:
InitializePsychSound;

% try to open an audio port for playing
audio_port = PsychPortAudio('Open', [], [], 0, Fs, nrchannels);

% put the music into an audio buffer - a special place in memory where it will be stored
PsychPortAudio('FillBuffer', audio_port, y);

% start playing what is in the audio buffer
% start_time is simply when the music starts playing
start_time = PsychPortAudio('Start', audio_port);

% wait some time so the music can play
WaitSecs(5);

% Stop playback
PsychPortAudio('Stop', audio_port);

% Close the audio port
PsychPortAudio('Close', audio_port);