سؤال

يبدو أن Octave يفترض أن أداة مساعدة تشغيل صوتية محددة ستكون متاحة على نظام ولكن لا يبدو أنها توفر القدرة على تحديد بديل. في الخطأ أدناه، فإن اوكتاف يبحث عنه ofsndplay, ، والتي ليست فائدة متاحة على جميع الأنظمة.

اوكتاف: 38> الصوت (Beamformed_20)

sh: ofsndplay: الأمر غير موجود

هل هناك إعداد تكوين أوكتاف أو جزء رمز يمكنني استخدامه لتحديد الأداة المساعدة المناسبة للنظام؟

هل كانت مفيدة؟

المحلول 3

على أحد آلات Linux الخاصة بي، قمت بإنشاء البرنامج النصي التالي ofsndplay للعمل حول التبعية السلكية:

$ cat /usr/bin/ofsndplay

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

هذا البرنامج النصي الخاص يستخدم SOX play جدوى.

من المسلم به أن التعليق غير ضروري للوظائف ولكنه بالتأكيد جعلني أشعر بتحسن ....

نصائح أخرى

لقد تجاوزت وظيفة PlayAudio من Octave مع الوظيفة التالية. هذا سيعمل فقط بعد التثبيت سوكس.

sudo apt-get install sox

(في أوبونتو)

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

ثبت alsa-utils. أو pulseaudio-utils. ووضع ما يلي في ~ / .octaverc:

global sound_play_utility = 'aplay';

أو

global sound_play_utility = 'paplay';

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. ال paplay دعوة لن تعمل أبدا بسبب paplay الافتراضي إلى s16ne (ربما خطأ مطبعي. أعتقد أنها تعني s16be - وقعت 16 بت كبتة)، و playaudio يكتب غير موقعة 8 بت!
  4. يستدعي ذلك باستخدام system(). وبعد دائما فكرة سيئة.
  5. يكتب الصوت خارج إلى ملف بدلا من بثه. قد تسبب مشاكل للملفات الكبيرة.
  6. على عكس matlab، فإنه لا يتعامل مع الصوت العائم. في الواقع يدعم فقط الصوت 8 بت! نوع من الغباء لأن هذه هي النتيجة التي تعادها Wavread!
  7. على عكس matlab، فإنه يدعم فقط معدل عينة واحد (44100 هرتز).

هذه الوظيفة هي المخربقة للغاية وغير آمنة وغير موثوق بها. إذا تمثل ذلك بأي حال من الأحوال جودة التعليمات البرمجية في مكان آخر في اوكتاف ... حسنا، هذا مثير للقلق. يجب أن تكون فعلا حقا إعادة استخدامها كدالة مناسبة في Octave باستخدام Portaudio.

نسخة أفضل قليلا

ليس لدي وقت أو دافع للقيام بالكثير من القرصنة على اوكتاف، لذلك في الوقت نفسه أقترح عليك استخدام هذه الوظيفة الأفضل قليلا:

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 كممارسة للقارئ.

أنا على جهاز Mac (Yosemite)، واكتشف حل أبسط مما اقترحه الآخرون. فقط في حالة ما زال هذا ذات صلة لأي شخص:

أولا تثبيت Sox: http://sox.sourceforge.net/

(عبر البيرة)

brew install sox

الآن في سطر الأوامر الطرفية يمكنك استخدامها:

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

... وسوف تسمع الموسيقى الجميلة.

لكن هذا الأمر لا يعمل من داخل أوكتاف. هذا يعمل:

system(‘play “/path/to/sound file.wav”’);

على 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";

هاهو!

في اوكتاف 4.2.1. يمكنك تشغيل ملف WAV كما يلي

احفظ التعليمات البرمجية التالية في ملف Playwav.m

function playWav(inputFilePath)
  [y, fs] = audioread(inputFilePath);
  player  = audioplayer(y, fs);
  playblocking(player)
end

ثم يمكنك استدعاء الوظيفة playWav('/path/to/wavfile'); من Octave Commandline.

اختبارها على ويندوز 7.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top