Farbod Shahinfar
Farbod Shahinfar

Reputation: 804

Octave playing an audio file from a function

I would like to write a function which takes an audio file address as input, and then make some changes to that audio file and play it.

I am new to Octave and by looking the documentation I came up with the code below

function main
    % clear screen 
    clear;
    clc;
    audio_src_address = 'introduction.wav';
    NegateAudioPhase(audio_src_address);
endfunction;

function NegateAudioPhase (audio_src_address)
    % load audio source
    [y, fs] = audioread(audio_src_address);

    % use chanel 1 
    y = y(:,1);

    % audio frequency domain
    f = fft(y);
    m = abs(f);
    p = angle(f);

    % negate audio source phase
    p = -p;

    % calculate new fourier transform
    f = m .* exp(j*p);

    % create the new audio source
    y2 = ifft(f);

    % play audio sound
    player = audioplayer(y2, fs);
    play(player);
endfunction

I have put this code in main.m file. I use Octave CLI to run the code by typing main and pressing enter.

code will be run until the end but no audio is played. there is a warning at the command line after the execution:

warning: Octave:audio-interrupt
warning: called from
    main>NegateAudioPhase at line 33 column 1
    main at line 6 column 5

I have put all the code in an other file without defining any functions and it worked as expected.

Upvotes: 2

Views: 1262

Answers (2)

ceving
ceving

Reputation: 23824

Use the function playblocking instead of play in order to wait until the playback finishes. See the manual.

Upvotes: 1

Farbod Shahinfar
Farbod Shahinfar

Reputation: 804

Okay I have found a hack to get a round the problem.
As was mentioned by Andy, the problem looks like to be the player object gets destroyed when the function returns (reaches endfunction). so I though may be I could waite for audio file to finish.

So I added the code below after playing audio.

% wait to finish
    while(isplaying(player))
      % Waiting for sound to finish
    endwhile

the result became as I wanted at first.

Upvotes: 2

Related Questions