2
votes

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.

2
Try to return 'player' from your function so it isn't deleted after returnAndy
So, you say player is a local variable and gets deatroyed when function is returned because player is in the stack memory of function. Make sense to me.Farbod Shahinfar
Exactly. Have you already tried it? And am I right that line 33 from your function it "endfunction"?. Btw, if you have warnings or errors wit line numbering you should include a comment in your code which annotates the lineAndy
Sorry for delay. I got busy. I just tried it and surprisingly it didn't worked. may be it is because main gets returned and object gets destroyed. I think I should find a way to wait for audio completion before returning. sorry about line numbering problem. yes your right line 33 is the endfunction which is at the end of the file.Farbod Shahinfar
I'm sure you made some error but without showing us the complete (modified) it's impossible to say.Andy

2 Answers

2
votes

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.

1
votes

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