10

Octave は、特定のサウンド再生ユーティリティがシステムで利用可能になると想定しているように見えますが、代替を指定する機能は提供していないようです。以下のエラーで、Octave は を探していますがofsndplay、これはすべてのシステムで利用できるユーティリティではありません。

オクターブ:38> 音(beamformed_20)

sh: ofsndplay: コマンドが見つかりません

システムに適したユーティリティを指定するために使用できる Octave 構成設定またはコード フラグメントはありますか?

4

7 に答える 7

8

次の関数を使用して、オクターブの playaudio 関数をオーバーライドしました。これは、 soxをインストールした後にのみ機能します。

sudo apt-get install sox

(ubuntuで)

function [ ] = playaudio (x, sampling_rate)

    if nargin == 1
        sampling_rate = 8000
    end
    file = tmpnam ();
    file= [file, '.wav'];
    wavwrite(x, sampling_rate, file);
    ['play ' file ]
    system(['play ' file ]);
    system(['rm ' file]);
end

同様のアプローチにより、記録することもできます。

% Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2003, 2004, 2005,
%               2006, 2007 John W. Eaton
%
% This file is part of Octave.
%
% Octave is free software; you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 3 of the License, or (at
% your option) any later version.
%
% Octave is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
% General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Octave; see the file COPYING.  If not, see
% <http://www.gnu.org/licenses/>.

% -*- texinfo -*-
% @deftypefn {Function File} {} record (@var{sec}, @var{sampling_rate})
% Records @var{sec} seconds of audio input into the vector @var{x}.  The
% default value for @var{sampling_rate} is 8000 samples per second, or
% 8kHz.  The program waits until the user types @key{RET} and then
% immediately starts to record.
% @seealso{lin2mu, mu2lin, loadaudio, saveaudio, playaudio, setaudio}
% @end deftypefn

% Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
% Created: 19 September 1994
% Adapted-By: jwe
% And adapted again 11/25/2010 by Rob Frohne    
function X = record (sec, sampling_rate)


  if (nargin == 1)
    sampling_rate = 8000;
  elseif (nargin != 2)
    print_usage ();
  endif

  file = tmpnam ();
  file= [file,".wav"];

  input ("Please hit ENTER and speak afterwards!\n", 1);

  cmd = sprintf ("rec -c1 -r%d %s trim 0 %d",
                   sampling_rate, file, sec)

  system (cmd);

  X = wavread(file);

end
于 2011-01-21T08:06:09.823 に答える
3

私の Linux マシンの 1 つで、次の ofsndplay スクリプトを作成して、ハードワイヤードな依存関係を回避しました。

$ cat /usr/bin/ofsndplay

#!/bin/sh
## Coping with stupid dependency on ofsndplay in octave
play -t au -

この特定のスクリプトは、SoXplayユーティリティを使用します。

確かに、コメントは機能上不要ですが、確かに気分が良くなりました....

于 2009-10-01T19:48:54.017 に答える
3

playaudioは壊れてます!

playsound(バージョン 3.6.2)のデフォルトの実装を読む価値があります。

function playaudio (name, ext)

  if (nargin < 1 || nargin > 2)
    print_usage ();
  endif

  if (nargin == 1 && isnumeric (name))
    ## play a vector
    if (! isvector (name))
      error ("playaudio: X must be a vector");
    endif
    X = name(:) + 127;
    unwind_protect
      file = tmpnam ();
      fid = fopen (file, "wb");
      fwrite (fid, X, "uchar");
      fclose (fid);
      [status, out] = system (sprintf ('cat "%s" > /dev/dsp', file));
      if (status != 0)
        system (sprintf ("paplay --raw \"%s\"", file))
      endif
    unwind_protect_cleanup
      unlink (file);
    end_unwind_protect
  elseif (nargin >= 1 && ischar (name))
    ## play a file
    if (nargin == 1)
      name = [name ".lin"];
    elseif (nargin == 2)
      name = [name "." ext];
    endif
    if (any (strcmp (ext, {"lin", "raw"})))
      [status, out] = system (sprintf ('cat "%s" > /dev/dsp', name));
      if (status != 0)
        system (sprintf ('paplay --raw "%s"', name))
      endif
    elseif (any (strcmp (ext, {"mu", "au" "snd", "ul"})))
      [status, out] = system (sprintf ('cat "%s" > /dev/audio', name));
      if (status != 0)
        system (sprintf ('paplay "%s"', name))
      endif
    else
      error ("playaudio: unsupported extension '%s'", ext);
    endif
  else
    print_usage ();
  endif

endfunction

注意すべき点がいくつかあります。

  1. /dev/dsp への直接書き込みは、最新の Linux ディストリビューションでは常に失敗するため、コマンドを実行するたびに (cat > /dev/dsp行で) エラーが発生します。
  2. paplayコマンドラインの pulseaudio プレーヤーであるを使用するようにハードコードされています。
  3. デフォルトは(おそらくタイプミスです。署名付きの16ビットビッグエンディアンを意味していると思います)、署名なしの8ビットを書き込むため、paplay呼び出しは決して機能しません!paplays16nes16beplayaudio
  4. を使って呼び出しますsystem()。常に悪い考えです。
  5. オーディオをストリーミングするのではなく、ファイルに書き出します。大きなファイルで問題が発生する可能性があります。
  6. matlab とは異なり、浮動小数点オーディオを処理しません。実際には 8 ビット オーディオのみをサポートしています。それが wavread によって返された結果なので、ちょっとばかげています!
  7. matlab とは異なり、1 つのサンプル レート (44100 Hz) のみをサポートします。

この関数は非常にハックで、安全でなく、信頼性がありません。それが何らかの方法で Octave の他の場所でのコード品質を表しているとしたら...まあ、それは心配です。portaudio を使用して、Octave で適切な機能として再実装する必要があります。

少し良いバージョン

私には、オクターブで多くのハッキングを行う時間も動機もありません。そのため、当面は、代わりに次の少し優れた関数を使用することをお勧めします。

function playsound(wav, samplerate)
  # Play a single-channel wave at a certain sample rate (defaults to 44100 Hz).
  # Input can be integer, in which case it is assumed to be signed 16-bit, or
  # float, in which case it is in the range -1:1.

  if (nargin < 1 || nargin > 2)
    print_usage();
  endif

  if (nargin < 2)
    samplerate = 44100;
  end

  if (!isvector(wav))
    error("playsound: X must be a vector");
  endif

  # Write it as a 16-bit signed, little endian (though the amaaazing docs don't say the endianness)

  # If it is integers we assume it is 16 bit signed. Otherwise we assume in the range -1:1
  if (isfloat(wav))
    X = min(max(wav(:), -1), 1) * 32767; # Why matlab & octave do not have a clip() function... I do not know.
  else
    X = min(max(wav(:), -32767), 32767) + 32767;
  endif
  unwind_protect
    file = tmpnam ();
    fid = fopen (file, "wb");
    fwrite (fid, X, "int16");
    fclose (fid);
    # Making aplay (alsa) the default, because let's be honest: it is still way more reliable than
    # the mess that is pulseaudio.
    if (exist("/usr/bin/aplay") == 2)
      system(sprintf("/usr/bin/aplay --format=S16_LE --channels=1 --rate=%d \"%s\"", samplerate, file))
    elseif (exist("/usr/bin/paplay") == 2)
      system(sprintf("/usr/bin/paplay --format=s16le --channels=1 --rate=%d --raw \"%s\"", samplerate, file))
    endif
  unwind_protect_cleanup
    unlink (file);
  end_unwind_protect

endfunction

これはまだ非常にハックな関数です。playaudioしかし、少なくとも!よりも信頼性が少し高くなるはずです。の実装はsoundsc、読者の演習として残しておきます。

于 2013-03-27T20:52:04.077 に答える
3

alsa-utilsまたはpulseaudio-utilsをインストールし、~/.octaverc に以下を記述します。

global sound_play_utility = 'aplay';

また

global sound_play_utility = 'paplay';
于 2009-10-01T18:31:05.733 に答える
2

私は Mac (Yosemite) を使用しており、他の人が提案したものよりも簡単な解決策を発見しました。これがまだ誰にとっても関連がある場合に備えて:

最初に SoX をインストールします: http://sox.sourceforge.net/

(自作経由)

brew install sox

端末のコマンドラインで次を使用できます。

play “/path/to/sound file.wav"

...そして美しい音楽が聞こえてきます。

しかし、そのコマンドは Octave 内からは機能しません。これは機能します:

system(‘play “/path/to/sound file.wav”’);
于 2015-01-02T05:00:43.843 に答える
1

OSXでは、これは私がサウンドを機能させるためにしたことです:

soundコマンドヘルプから:

This function writes the audio data through a pipe to the program "play" from the sox distribution. sox runs pretty much anywhere, but it only has audio drivers for OSS (primarily linux and freebsd) and SunOS. In case your local machine is not one of these, write a shell script such as ~/bin/octaveplay, substituting AUDIO_UTILITY with whatever audio utility you happen to have on your system: #!/bin/sh cat > ~/.octave_play.au SYSTEM_AUDIO_UTILITY ~/.octave_play.au rm -f ~/.octave_play.au and set the global variable (e.g., in .octaverc) global sound_play_utility="~/bin/octaveplay";

次のスクリプトに「octaveplay」という名前を付けて、~/bin に入れました。

cat > ~/.octave_play.aif
afplay ~/.octave_play.aif
rm -f ~/.octave_play.aif

次に、.octaverc を作成して追加しました。 global sound_play_utility="~/bin/octaveplay";

出来上がり!

于 2010-01-12T08:12:09.333 に答える