1. linux pc のサウンドシステム linux pc sound systemnagata/tokuronii/...1-1 alsa (advanced...

21
1. Linux PC のサウンドシステム Linux PC Sound System 1-1 ALSA (Advanced Linux Sound Architecture) Linux には音響信号入出力のため、ハード(サウンドカード) 動かすためのソフトウェア一式が用意されているLinux has a set of programs to drive hardware (sound cards) for sound input and output・サウンドカードのドライバ(多くのカードのドライバの 集合体) a set of sound card driver ・プログラミング IF(関数)/関数群を動かすためのデー タ構造 programming interface data structures to support PC 用サウンドカード: 数多くのベンダーと製品 Hardware = PC sound cards : lots of benders and products CreativeLabM-AudioRolandOnkyoNative Instruments/・・・ ソフトウェアプラットフォームの種類(OS native) Sound software platforms (OS native) Linux : ALSAOSS Windows : WMMEDirectSoundASIOWASAPI /・・・ Mac : CoreAudio Linux PC での基本は ALSA (ALSA is a base system for linux) OSSOpen Sound System)は古いシステム ・カードのアクセス方法(デバイスドライバの使い方)は カードごとに異なり、普通公開されていないので、一般 ユーザのプログラミングは困難 Because card driver interface is usually closed for ordinary users, it is

Upload: others

Post on 09-Feb-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

  • 1. Linux PC のサウンドシステム ( Linux PC Sound System )

    1-1 ALSA (Advanced Linux Sound Architecture) Linux には音響信号入出力のため、ハード(サウンドカード) を動かすためのソフトウェア一式が用意されている(Linux has a set of programs to drive hardware (sound cards) for sound input and output):・サウンドカードのドライバ(多くのカードのドライバの集合体)

    a set of sound card driver・プログラミング IF(関数)/関数群を動かすためのデータ構造

    programming interface / data structures to support PC用サウンドカード :数多くのベンダーと製品

    Hardware = PC sound cards : lots of benders and products CreativeLab/M-Audio/Roland/Onkyo/Native Instruments/・・・

    ソフトウェアプラットフォームの種類(OS native)Sound software platforms (OS native)

    Linux : ALSA/OSSWindows : WMME/DirectSound/ASIO/WASAPI/・・・Mac : CoreAudio

    Linux PCでの基本は ALSA (ALSA is a base system for linux) ・ OSS(Open Sound System)は古いシステム・カードのアクセス方法(デバイスドライバの使い方)はカードごとに異なり、普通公開されていないので、一般ユーザのプログラミングは困難

    Because card driver interface is usually closed for ordinary users, it is

  • difficult to write programs. ・ALSAは、カードのデバイスドライバを統一的に扱うプログラミング IF(C言語)を備える

    ALSA has unified programing interface in C language to handle many device drivers.・どんなカードも共通の関数呼び出しで処理

    Every card can be handled using the common function calls.・パラメータ 設定(チャネル数、サンプリング周波数、サンプルデータ形式(語長/並び(BE、LE)等)

    Parameter setting (number of channels, sampling frequency, data format(word length/byte order(BE,LE), etc)

    ・入出力、制御(音量調節、初期化、起動、停止など)Data input/output, control(volume, initialization, start, stop, etc)

    ■ ALSAを使える状態にする:Preparation for use

    ・ALSAドライバ:linuxのカーネルに含まれるので、インストール不要Because ALSA driver is included in linux kernel, instration is not needed.・ユーザライブラリ(プログラミング I/F):主要なディストリビューション(Ubuntu、Fedora、etc.)ならライブラリ(libasound2)はインストール済だが、通常、

    development tool(libasound2-dev:コンパイルに必要なヘッダ類)は追加インストールが必要User library (programming interface) : library “libasound2” is already installed in case of major linux distributions(Ubuntu, Fedra, etc). But development tools(“libasound2-dev”) must be installed by users.

    パッケージマネージャで見つからなければ、if you can’t find libasound2 through package manager,

    http://www.alsa-project.org/main/index.php/Download

    にある最新版:the latest version

    http://www.alsa-project.org/main/index.php/Download

  • alsa-*-1.0.29をダウンロードしてインストールする。can be downloaded and installed instead.

    ■ ALSAの基本知識(プログラミングに必要)Fundamental knowledge of ALSA (required to write the program)ALSA project wiki http://www.alsa-project.org に書かれているwritten in ALSA project wiki

    ALSA project wiki Main page の IntroductionThe Advanced Linux Sound Architecture (ALSA) providesaudio and MIDI functionality to the Linux operating system. ・・・・・・・・・・

    「SoundCards 」・・・ ALSA対応カードの頁ALSA supported cards

    「Sound Card List」 http://www.alsa-project.org/main/index.php/Matrix:Main

    カードのサポート状況を調べるWe can check whether the card is spported or not on this page.

    例 ONKYO WAVIONI Audio4DJM-Audio FastTrackUltraRoland FA101

    「ALSA Developers 」・・・開発者(プログラマー)用の起点Start page for Programmers

    いずれ、この頁の情報は、プログラミングの際に必要になる。Solong, information on the home page will be necessary for programming.

    http://www.alsa-project.org/main/index.php/Matrix:Mainhttp://www.alsa-project.org/

  • 1-2 サウンドサーバー(Sound Server) PCオーディオデバイスへの直接アクセスを避け、サウンド入

    出力を仮想化して便利にするためのソフトウェアSound server enables indirect access to sound devices so that the sound input/output is vertualized.

    サウンドサーバーの働きEffects of sound server・複数プログラムの音響入出力統合

    Unification of sound input/outpt by multiple programs.・複数デバイスの管理、選択

    Management and selection of multiple sound devices. ・プログラムごとの設定保存(音量)

    Reservation of sound parameter depending on the program (mainly soundvolume) .

    サーバ(既存ソフト)の種類exsisting server・PulseAudio(Ubuntu/Fedora)・JACK

    PulseAudio(Official site : https://www.freedesktop.org/wiki/Software/PulseAudio/)日本語wiki : http://ja.wikipedia.org/wiki/PulseAudioEnglish wiki : https://en.wikipedia.org/wiki/PulseAudioread wiki to get information特徴(Features)* アプリケーションごとの音量制御

    Per-application volume controls.* ローダブル・モジュール対応を伴った拡張プラグイン構造

    An extensible plugin architecture with support for loadable modules.* 多くの有名なオーディオアプリケーションとの互換性

    https://en.wikipedia.org/wiki/PulseAudiohttp://ja.wikipedia.org/wiki/PulseAudiohttps://www.freedesktop.org/wiki/Software/PulseAudio/

  • Compatibility with many popular audio applications.* 複数のオーディオソース/シンクに対応

    Support for multiple audio sources and sinks.* 低遅延な動作と遅延量の計測に対するサポート

    Low latency operation and latency measurement.* A zero-copy memory architecture for processor resource efficiency.* Ability to change which output device an application plays sound throughwhile the application is playing sound* スクリプト処理に対応したコマンドライン・インターフェイス

    A command-line interface with scripting capabilities.* コマンドラインから設定変更できるサウンドデーモンとして動作

    A sound daemon with command line reconfiguration capabilities.* サンプリングレート変換、再サンプリングにビルトインで対応

    Built-in sample conversion and resampling capabilities.* 複数のサウンドカードをひとつにまとめられる

    The ability to combine multiple sound cards into one.* 複数の再生ストリームを同期させることができる

    The ability to combine multiple sound cards into one.* Bluetoothオーディオデバイスを動的に検出

    Bluetooth audio device support with dynamic detection capabilities.・・・アプリケーションとの関係Relation to application programs「PulseAudio はバックグラウンドで動作するサウンドサーバであり、一つまたは複数の音声入力(プロセスや録音デバイスなど)からデータを受け取って一つまたは複数の音声出力(サウンドカードやネットワーク上の PulseAudioサーバ、他プロセスなど)に送る。PulseAudio acts as a sound server, where a background process accepting sound input from one or more sources (processes, capture devices, etc) is created. The background process then redirects mentioned sound sources to one or more sinks (sound cards, remote network PulseAudio servers, or other processes)

    PulseAudioの目標の一つは、旧来のOSS を利用したアプリケーショ

    https://ja.wikipedia.org/wiki/Open_Sound_Systemhttps://en.wikipedia.org/wiki/Sound_cardhttps://en.wikipedia.org/wiki/Sound_cardhttps://en.wikipedia.org/wiki/Zero-copyhttps://en.wikipedia.org/wiki/Latency_(audio)

  • ンなどのようにハードウェアに直接アクセスする代わりに、すべての音声ストリームを PulseAudio 経由で扱うようにすることである。そのために、aRts や ESDなど他のオーディオシステムの利用するアプリケーションに対してアダプターを提供する。One of the goals of PulseAudio is to reroute all sound streams through it, including those from processes that attempt to directly access the hardware (like legacy OSS applications). PulseAudio achieves this by providing adapters to applications using other audio systems, like aRts and ESD.Linux環境では通常、ALSA が PulseAudio が提供している仮想デバイ

    スを使うように設定する。こうすることで、ALSAを利用するアプリ ケーションは PulseAudio にデータを送るようになり、PulseAudio からは ALSAを利用してサウンドカードにデータが送られる。

    In a typical installation scenario under Linux, the user configures ALSA touse a virtual device provided by PulseAudio. Thus, applications using ALSA will output sound to PulseAudio, which then uses ALSA itself to access the real sound card. PulseAudio also provides its own native interface to applications that want to support PulseAudio directly,・・・」サーバを含むサウンドシステム全体は複雑

    図: Pulseaudio-diagram.svghttp://upload.wikimedia.org/wikipedia/commons/0/00/Pulseaudio-diagram.svg

    例:ALSA ライブラリでプログラムを作成して動かす場合の処理経路

    [ALSA app] (ALSA native のアプリケーションとして作成)→ [libalsa Pulse] (Library layer)→ [UNIX/TCP → Pulse Server core → ALSA sink/source] (ALSA driver pulse engine layer)→ [ALSA/OSS hardware driver] (Linux Kernel)

    http://upload.wikimedia.org/wikipedia/commons/0/00/Pulseaudio-diagram.svghttp://upload.wikimedia.org/wikipedia/commons/0/00/Pulseaudio-diagram.svghttps://en.wikipedia.org/wiki/Advanced_Linux_Sound_Architecturehttps://en.wikipedia.org/wiki/Enlightened_Sound_Daemonhttps://en.wikipedia.org/wiki/ARtshttps://en.wikipedia.org/wiki/Open_Sound_Systemhttps://ja.wikipedia.org/wiki/ARts

  • プログラムは pulse engine layer が間に入って pulse経由で動作するため、サウンドデータと制御の経路が複雑化。(プログラマは考えなくてよい)The sound data path and controls are complicated by the pluse engine layer. But programers don’t have to care the complexity.

    PulseAudio の停止ALSAを使うプログラムは、デフォルトでPulseAudio経由でデバイスにアクセスする設定となっているため、他のサーバを使用したい場合や遅延を小さくしたい場合に外したいことがある。その場合、Because programs using ALSA access sound device bia PulseAudio, we have to eject PulseAudio to use other sound server or to reduce time delay induced by PulseAudio server.Then, in the file

    /usr/share/alsa/ alsa-conf.d/ pulse.conf の中のthe line

    /usr/share/alsa/pulse-alsa.confの行をコメントアウトする。must be comment outed.

    ../../../../usr/share/alsa/pulse.conf../../../../usr/share/alsa/pulse.conf../../../../usr/share/alsa/pulse.conf

  • 2. サウンドプログラミング ( Sound Programming ) 2-1 プログラミングのための基本情報

    Basic information for sound programming

    ■ 概要 linuxでのサウンドプログラミングは、ALSA か PortAudioSound programming on linux usually utilizes ALSA or PortAudio.ALSA ライブラリは Linux 限定だが、シンプルに書けて安定動作ALSA can be utilized on linux only, but we can write simple program and the program is stable. PortAudioは種々のOSに対応 :便利。一部未対応機能あり(異なったオーディオカードを入力と出力に使うときに不具合)PortAudio can work across multiple OS, so, is useful.プログラミングに必要な情報は、プログラミング IF(API)(関数呼び出しの定義、データ構造、定数、サンプルコード等)Programming requires programming interface(API) (data structure, constant, function call definition, sample code, etc).

    ■ ALSAの場合(in case of ALSA)(Main Pageis: http://www.alsa-project.org/main/index.php/Main_Page )◆ALSA Library API の頁を見る(Go to “Library API” page)

    ALSA Library API:→ (メイン ALSA Developers [ALSA Library API])

    http://www.alsa-project.org/main/index.php/ALSA_Library_API

    を見ると、The ALSA library API is the interface to the ALSA drivers. ・・・・・

    ALSA library API reference The currently designed interfaces are listed below:

    http://www.alsa-project.org/main/index.php/ALSA_Library_APIhttp://www.alsa-project.org/main/index.php/ALSA_Library_APIhttp://www.alsa-project.org/main/index.php/Main_Page

  • ・・・・・3. Mixer Interface (/dev/snd/mixerCXDX)4. PCM Interface (/dev/snd/pcmCXDX)

  • 2 read') and write (see 'man 2 write') C functions. ・・・・・日本語訳の頁(永田訳)/home/nagata/TokuronII/ALSA 翻訳 /ALSA project - the C library reference_PCM (digital audio) interface.html

    ・関数定義の例:sound_pcm_open( ) ( http://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m.html )Functionsの一行目

    int snd_pcm_open ( snd_pcm_t ** pcmp,const char * name,snd_pcm_stream_t stream,int mode

    ) Opens a PCM. Parameters:

    pcmp Returned PCM handle name ASCII identifier of the PCM handle stream Wanted streammode Open mode (see SND_PCM_NONBLOCK,

    SND_PCM_ASYNC) Returns:

    0 on success otherwise a negative error code Examples: /test/latency.c, /test/pcm.c, and /test/pcm_min.c.

    手っ取り早くプログラムを作りたいとき◆Tutorial: A Tutorial on Using the ALSA Audio API

    ( http://equalarea.com/paul/alsa-audio.html )Understanding Audio Interfaces基礎概念の説明( A/D, D/A, リングバッファ ,Latency)

    http://equalarea.com/paul/alsa-audio.htmlhttp://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m.html./ALSA%E7%BF%BB%E8%A8%B3/ALSA%20project%20-%20the%20C%20library%20reference_PCM%20(digital%20audio)%20interface.html./ALSA%E7%BF%BB%E8%A8%B3/ALSA%20project%20-%20the%20C%20library%20reference_PCM%20(digital%20audio)%20interface.html../ALSA%E7%BF%BB%E8%A8%B3/ALSA%20project%20-%20the%20C%20library%20reference_PCM%20(digital%20audio)%20interface.html

  • For this process to work correctly, there are a number of variables that need to be configured. They include:

    • what format should the interface use when converting between the bitstream used by the computer and the signal used in the outside world?

    • at what rate should samples be moved between the interface and the computer?

    • how much data (and/or space) should there be before the device interrupts thecomputer?

    • how big should the hardware buffer be?

    The first two questions are fundamental in governing the quality of the audio data. The second two questions affect the "latency" of the audio signal. This term refers tothe delay between

    1. data arriving at the audio interface from the outside world, and it being available to the computer ("input latency")

    2. data being delivered by the computer, and it being delivered to the outside world ("output latency")

    Both of these are very important for many kinds of audio software, though some programs do not need be concerned with such matters.

    What a typical audio application doesA typical audio application has this rough structure: open_the_device(); set_the_parameters_of_the_device(); while (!done) { /* one or both of these */ receive_audio_data_from_the_device(); deliver_audio_data_to_the_device(); } close the device

    プログラム例・A Minimal Playback Program,・A Minimal Capture Program・A Minimal Interrupt-Driven Program・A Minimal Full-Duplex Programinterrupt-driven(CPU パワー節約)のプログラムはPortAudio で(の方が)簡単に書ける。

    ・簡単な ALSAプログラムhttp://wiki.cis.iwate-u.ac.jp/~nagata/TokuronII/ (あとで参照)

    http://wiki.cis.iwate-u.ac.jp/~nagata/TokuronII/

  • ■ PortAudioの基本情報(Basic information of PortAudio) 特徴:・種々のプラットフォームに亘る互換性(詳細は後で)Compatibility across multiple platforms(http://portaudio.com/docs/v19-doxydocs/api_overview.html)・イベント処理的コーディングが簡単(デバイスアクセス頻度自動化:プロセッサの空回り削減)

    easy to program event driven processing情報(Main page: http://www.portaudio.com/ )

    PortAudio is a free, cross-platform, open-source, audio I/O library. It lets you write simple audio programs in 'C' or C++that will compile and run on many platforms including Windows, Macintosh OS X, and Unix (OSS/ALSA).…The Portaudio Wiki…

    ◆PortAudio Documentation ( http://www.portaudio.com/docs.html )ー V19 Tutorial - How to Compile and Use PortAudioー V19 API Reference generated using Doxygen including:

    • portaudio.h - primary header • API Overview

    ◆PortAudio Tutorialshttp://portaudio.com/docs/v19-doxydocs/tutorial_start.html

    Downloading(インストールするソースを選択)First thing you need to do is download the PortAudio source code either as a tarball from the website, or from the Subversion Repository.

    上のリンクに行くと、安定版は、pa_stable_v190600_20161030.tgz

  • ubuntu 16.04 のデフォルトもこれ。若干古いので、Daily Snapshots from the SVN Repository の

    pa_snapshot.tgz created Wednesday, 19-Apr-2017 01:23:08 UTC

    を使ってもよい。 Compiling

    Once you've downloaded PortAudio you'll need to compile it, which of course, depends on your environment:

    • Windows• Building Portaudio for Windows using Microsoft Visual Studio • Building Portaudio for Windows with MinGW • Building Portaudio for Windows with ASIO support using MSVC • Creating MSVC Build Files via CMake

    • Mac OS X• Building Portaudio for Mac OS X

    • POSIX• Building Portaudio for Linux ※

    ※Linuxの場合のダウンロード、インストール、コンパイルを説明してある。 Programming with PortAudio

    Below are the steps to writing a PortAudio application: • Write a callback function that will be called by PortAudio when

    audio processing is needed. • Initialize the PA library and open a stream for audio I/O. • Start the stream. Your callback function will be now be called

    repeatedly by PA in the background. • In your callback you can read audio data from the inputBuffer

    and/or write data to the outputBuffer. • Stop the stream by returning 1 from your callback, or by calling a

    stop function. • Close the stream and terminate the library.

    In addition to this "Callback" architecture ・・・

    http://portaudio.com/docs/v19-doxydocs/compile_linux.htmlhttp://portaudio.com/docs/v19-doxydocs/compile_mac_coreaudio.htmlhttp://portaudio.com/docs/v19-doxydocs/compile_cmake.htmlhttp://portaudio.com/docs/v19-doxydocs/compile_windows_asio_msvc.htmlhttp://portaudio.com/docs/v19-doxydocs/compile_windows_mingw.htmlhttp://portaudio.com/docs/v19-doxydocs/compile_windows.htmlhttp://www.portaudio.com/archives/pa_snapshot.tgz

  • Programming Tutorial Contents重要な関数の説明• Writing a Callback Function • Initializing PortAudio • Opening a Stream Using Defaults • Starting, Stopping and Aborting a Stream • Closing a Stream and Terminating PortAudio • Utility Functions • Enumerating and Querying PortAudio Devices

  • 2-2 プログラミング (Programming)まず、自分の PCで使うデータ形式とデバイスの情報が必要At first, you have to know the data format and the device information in your computer.■データ形式(data format)

    サンプリング周波数(sampling frequency)(in kHz)8、11.025、16 、 22.05 、 24 、 32 、 44.1 、 48 、 96 、 192

    オーディオサンプルの符号形式(sample format)・整数/浮動小数点(サウンドデバイスは通常整数のみを扱える)

    integer/floating point(ordinary sound device can handle only integer data)・Signed/Unsigined ( 整数の場合(for integer))・サンプルデータの語長(word length of sample data)浮動小数点(floating point format) : 32/64 bit整数(integer) : 8/16/20/24/32 bit

    24 bit :通常の 3 byteデータ/4 byte データの下位 3 bytenormal 3 bytes data / lower 3 bytes in 4bytes data

    20 bit : 3 byte データの下位 20 bitlower 20 bits in 3 bytes data

    ・Endian(LE/BE)データ形式の説明図 サンプル形式の定義(format definition)

    ALSA/PortAudioで共通のヘッダファイル(common header file) /usr/include/alsa/pcm.h に定義されている。

    typedef enum _snd_pcm_format snd_pcm_format_t16 bit signed int LE → SND_PCM_FORMAT_S16_LE24 bit signed int 4 byte LE → SND_PCM_FORMAT_S24_LE24 bit signed int 3 byte BE → SND_PCM_FORMAT_S24_3BE

    ./%E3%83%87%E3%83%BC%E3%82%BF%E5%BD%A2%E5%BC%8F%E3%81%AE%E8%AA%AC%E6%98%8E%E5%9B%B3.pdfhttp://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m.html#g5ae31ebbea7972ce97a498040df1839ehttp://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m.html#g3df0b888477ce2dc3817d9095db859b1../../../../usr/include/alsa/pcm.h#/usr/include/alsa/pcm.h

  • データ並びの型式(sample alignment)インターリーブ(interleave) LRLRLR・・・ノンインターリーブ(noninterleave)LLL・・・RRR・・・LLL・・・RRR・・・一般にはインターリーブが使われる。

    Interleave is often used.デバイスやファイル入出力時に指定が必要ことあり。指定がなければほぼインターリーブSample alignment is often required for device/file access. If not required, the device/file is accessed using interleave mode.

    処理プログラム内では、例えば、(example of format conversion) → デバイス(入力) S16_LE→ float / double (計算処理)→ S16_LE → デバイス(出力)のように型を変えて扱う。

    サウンドファイルのヘッダ(file header of sound file)一般的な識別子(必ずしも必要なわけではないが、あった方がわかりやすい)commonly used suffix (not necessarry, but easy to handle)

    .raw ヘッダなし(headerless raw data)

    .wav 圧縮なし(without compression)

    .wma Windows Media Audio

    .mp3 mpeg3・・・

  • ■オーディオデバイスを調べる(デバイス名の内部定義)Checking sound device name on Linux PC ALSAコマンドを使う(using ALSA command) : aplay -l / arec -l

    $ aplay -l / arecord -l**** ハードウェアデバイス PLAYBACK のリスト **** ・・・・・

    カード 1: Audio4DJ [Audio 4 DJ], デバイス 0: Audio 4 DJ [Audio 4 DJ] サブデバイス: 2/2 サブデバイス #0: subdevice #0 サブデバイス #1: subdevice #1 ・・・・・ALSA の内部的なハードウェア名は、The device name in ALSA system is defined as「hw:カード番号, デバイス番号, サブデバイス番号」

    デバイス名を陽に指定した場合の例$aplay -D hw:1,0,1 data.raw(カード1(Audio 4 DJ)のデバイス0番、サブデバイス1番でデータ再生)$aplay -D default data.raw -r 11025 -f S16_LE -v(デフォルトデバイスでサンプリング周波数とデータ型を指定してデータ再生)

    PortAudio の関数を使う(using portaudio function):プログラム:http://wiki.cis.iwate-u.ac.jp/~nagata/TokuronII/adev-sjis. c または、portaudio のサンプルプログラム: pa_devs.c を使う

    コンパイル:(事前に PortAudioのインストール必要)$gcc pa_devs.c -lportaudio -o pa_devs$gcc adev.c -lportaudio -o adev

    (adev → を実行 結果をメールで)再生(Playback)/録音(Capture)、BlockingIO/Callback

    http://wiki.cis.iwate-u.ac.jp/~nagata/TokuronII/adev-sjis.chttp://wiki.cis.iwate-u.ac.jp/~nagata/TokuronII/adev.chttp://portaudio.com/docs/v19-doxydocs/pa__devs_8c.html

  • ALSA 版 http://wiki.cis.iwate-u.ac.jp/~nagata/TokuronII/ コンパイル: $make ply %make reco

    PortAudio 版http://portaudio.com/docs/v19-doxydocs/group__examples__src.htmlコンパイル(compile): $gcc paex_sine.c -lportaudio

    ALSA 設定ファイル・通常は不要(デフォルトデバイス・ステレオ/モノラル)・多チャネル/特殊なデバイスでは 設定が必要

    ~/.asoundrc http://wiki.cis.iwate-u.ac.jp/~nagata/TokuronII/ asoundrc ・比較的わかりやすい説明; http://wikiwiki.jp/tetsuya/?asoundrc :

    hw:0,0,0 (カード番号、デバイス番号、サブデバイス番号)■ サウンドファイルライブラリ(libsndfile)ヘッダ付きのサウンドファイルの読み書きには、サウンドファイルライブラリ「libsndfile」が便利

    “libsndfile” is useful to read and/or write sound file with header. ・サウンドファイル全体の説明: http://www.mega-nerd.com/libsndfile/

    ・プログラミング IFの説明:http://www.mega-nerd.com/libsndfile/api.htmllibsndfileLibsndfile is a library designed to allow the reading and writing of many different sampled sound file formats (such as MS Windows WAV and the Apple/SGI AIFF format) through one standard library interface. During read and write operations, formats are seamlessly converted between the format the application program has requested or supplied and the file's data format. The application programmer can remain blissfully unaware of issues such as file endian-ness and data format.

    Synopsis#include #include

    SNDFILE* sf_open (const char *path, int mode, SF_INFO *sfinfo);

    sf_count_t sf_read_short (SNDFILE *sndfile, short *ptr, sf_count_t items);

    http://www.mega-nerd.com/libsndfile/api.html#openhttp://www.mega-nerd.com/libsndfile/api.htmlhttp://www.mega-nerd.com/libsndfile/http://wikiwiki.jp/tetsuya/?asoundrchttp://wiki.cis.iwate-u.ac.jp/~nagata/TokuronII/asoundrchttp://wiki.cis.iwate-u.ac.jp/~nagata/TokuronII/asoundrchttp://portaudio.com/docs/v19-doxydocs/group__examples__src.htmlhttp://portaudio.com/docs/v19-doxydocs/group__examples__src.htmlhttp://wiki.cis.iwate-u.ac.jp/~nagata/TokuronII/

  • ・・・sf_count_sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames);

    ・read、write、各々について、下の場合分けにより 4×2=8 種類の関数8 functions for read and write respectively are defined depending on the data type in a program and the way to count number of samples.

    プログラム中のデータ型(data format in a program) : short / int / float / double

    入出力サンプルの数え方(way to count number of samples) : 総サンプル数かフレーム数か(total number of samples / number of frames)「フレーム」とは、1ch当りの入出力サンプル数“frame” indicates number of samples per channel.例:モノラル 100 サンプルとステレオ 200 サンプルはいずれも 100フレームexample: Both 100 sample monoral data and 200 sample stereo data are expressed as 100 frame data.慣れてくるとフレーム単位で扱う方が便利なことが多い。Unit of frame will be useful as you are used to handle sound data.

    File Open FunctionSNDFILE* sf_open (const char *path, int mode, SF_INFO *sfinfo);The sf_open() function opens the sound file at the specified path.・・・・SF_INFO 構造体typedef struct{ sf_count_t frames ; フレーム数 int samplerate ; サンプリング周波数 int channels ; チャネル数 int format ; サンプルのデータ形式 int sections ; int seekable ;} SF_INFO ;

    ・modeThe mode parameter for this function can be any one of the following three values:

    SFM_READ - read only modeSFM_WRITE - write only modeSFM_RDWR - read/write mode

  • 注意When opening a file for read, the format field should be set to zero before calling sf_open(). The only exception to this is the case of RAW files where the caller has to set the samplerate, channels and format fields to valid values. All other fields of the structure are filled in by the library.When opening a file for write, the caller must fill in structure members samplerate, channels, and format.

    ・sample formatThe format field in the above SF_INFO structure is made up of the bit-wise OR of a major format type (values between 0x10000 and 0x08000000), a minor format type (with values less than 0x10000) and an optional endian-ness value. The currently understood formats are listed in sndfile.h as follows and also include bitmasks for separating major and minor file types. Not allcombinations of endian-ness and major and minor file types are valid.

    SF_INFO の 「format」の型は int(=4バイト)major format 16 進で5〜7桁 : 0x00010000 〜 0x08000000minor foramt(Subtype) 16 進で4〜1桁 : 0x00000000 〜 0x0000FFFFoptional 16進で8桁目が 0〜3の 4種類

    0x00000000、0x10000000、0x20000000、0x30000000定義されているデータ型enum(列挙型) {

    /* Major formats. */ 主にヘッダ型の識別 SF_FORMAT_WAV = 0x010000, /* Microsoft WAV format (little endian). */ SF_FORMAT_AIFF = 0x020000, /* Apple/SGI AIFF format (big endian). */ SF_FORMAT_AU = 0x030000, /* Sun/NeXT AU format (big endian). */ SF_FORMAT_RAW = 0x040000, /* RAW PCM data. */... ...SF_FORMAT_RF64 = 0x220000, /* RF64 WAV file */

    /* Subtypes from here on. */ サンプルの形式 SF_FORMAT_PCM_S8 = 0x0001, /* Signed 8 bit data */ SF_FORMAT_PCM_16 = 0x0002, /* Signed 16 bit data */ SF_FORMAT_PCM_24 = 0x0003, /* Signed 24 bit data */ SF_FORMAT_PCM_32 = 0x0004, /* Signed 32 bit data */ SF_FORMAT_PCM_U8 = 0x0005, /* Unsigned 8 bit data (WAV and RAW only) */ SF_FORMAT_FLOAT = 0x0006, /* 32 bit float data */ SF_FORMAT_DOUBLE = 0x0007, /* 64 bit float data */...

    /* Endian-ness options. */ SF_ENDIAN_FILE = 0x00000000, /* Default file endian-ness. */ SF_ENDIAN_LITTLE = 0x10000000, /* Force little endian-ness. */ SF_ENDIAN_BIG = 0x20000000, /* Force big endian-ness. */ SF_ENDIAN_CPU = 0x30000000, /* Force CPU endian-ness. */format と 以下3種類の MASK のビット ANDで major,minor,optionalの各型の値が取り出せる。 SF_FORMAT_SUBMASK = 0x0000FFFF, SF_FORMAT_TYPEMASK = 0x0FFF0000, SF_FORMAT_ENDMASK = 0x30000000

    }

  • File Close Function int sf_close (SNDFILE *sndfile) ;

    File Read Functions・・・Note: The data type used by the calling program and the data format of the file do not need to be the same. For instance, it is possible to open a 16 bit PCM encoded WAV file and read the data using sf_read_float(). The library seamlessly converts between the two formats on-the-fly. See Note 1. ・・・File Write Functions・・・As with the read functions above, the only difference in the items and frames version of each write function is the units in which the buffer size is specified. Again, the data type used by the calling program and the data format of the file do not need to be the same (Note 1). ・・・例sf_count_t sf_readf_double(SNDFILE *sndfile, double *ptr, sf_count_t frames);

    配列 ptrに doubleでファイルから読み込む。配列は double型、データ量はフレーム数でカウント。ファイル内のデータの型が何であっても doubleに型変換されて読まれる。注意型変換の規則は、Note 1 、Note 2 に記されている。浮動小数点型(float/double)のデータは-1.0〜1.0の範囲。Note1:整数型と浮動小数点型の間の変換の際は、指定した整数型で表現できる最大値が

    1になるように正規化される。Note2:浮動小数点データを整数型の read 関数で読むときは、通常だとデータが0になってしまうので、これを避ける設定もある。

    http://www.mega-nerd.com/libsndfile/api.html#note1http://www.mega-nerd.com/libsndfile/api.html#note1http://www.mega-nerd.com/libsndfile/api.html#note1http://www.mega-nerd.com/libsndfile/api.html#readhttp://www.mega-nerd.com/libsndfile/api.html#note1

    「Sound Card List」Understanding Audio Interfaces基礎概念の説明(A/D, D/A, リングバッファ, Latency)What a typical audio application does・A Minimal Interrupt-Driven Program・A Minimal Full-Duplex Program◆PortAudio Tutorialshttp://portaudio.com/docs/v19-doxydocs/tutorial_start.htmlubuntu 16.04 のデフォルトもこれ。Compiling

    Programming with PortAudioProgramming Tutorial Contents◆PortAudio API OverviewMain→ V19 API Reference → PortAudio API Overview(http://portaudio.com/docs/v19-doxydocs/api_overview.html )Introduction

    Key abstractions: Host APIs, Devices and Streams◆Example programs demonstrating PortAudio usageMain→ V19 API Reference → PortAudio API OverviewlibsndfileSynopsisFile Open FunctionFile Close FunctionFile Read FunctionsFile Write Functions