4  Mixerインターフェース

The Mixer Interface allows applications to change the volume level of a sound card's input/output channels in both the linear range percentage range (0-100) and in decibels. It also supports features like hardware mute, input sound source, stereo signal routing etc.

Mixerインターフェースはアプリケーションに直線変化のパーセンテージのはんい(0-100)とデシベルの両方でサウンドカードの入出力チャネルのボリュームレベルを変更することを許可します。将来はハードウェアミュートや入力soundソース、stereoシグナルルーティングなどのようなものもサポートします。

4.1  Low-Levelレイヤ

Mixer devices aren't opened exclusively. This allows applications to open a device multiple times with one or more processes.

Mixerデバイスは排他的オープンができません。これはアプリケーションが1つでも多数でも時間が重なっても開くことを許しています。

int snd_mixer_open(void **handle, int card, int device)

Creates new handle and opens a connection to the kernel sound mixer interface for sound card number card (0-N) and mixer device number device. Also checks if protocol is compatible to prevent use of old programs with new kernel API. Function returns zero if successful, otherwise it returns an error code.

サウンドカード番号card(0-N)とMixerデバイス番号deviceに対して新しいハンドルを作成してkernelのサウンドMixerインターフェースで通信をopenします。さらに新しいkernel APIにも関わらず古いプログラムが使われるのを防止するためにプロトコルに互換があるかチェックしています。関数は成功すれば0を返し、そうでなければエラーコードを返します。

int snd_mixer_close(void *handle)

Frees all resources allocated to the mixer handle and closes its connection to the kernel sound mixer interface. Function returns zero if successful, otherwise it returns an error code.

割り当てられたcontrolハンドルのすべてのリソースを解放しkernelのサウンドMixerインターフェースを閉じます。関数は成功すれば0を返し、そうでなければエラーコードを返します。

int snd_mixer_file_descriptor(void *handle)

Returns the file descriptor for the connection to the kernel sound mixer interface. This function should be used only in very special cases. Function returns a negative error code if an error was encountered.

kernelのサウンドMixerインターフェースのためのファイルディスクリプタを返します。この関数は普通は相当特殊なケースで使うのみです。エラーに遭遇した場合は負の数のエラーコードを返します。

The file descriptor should be used for the select(2) synchronous multiplexer function for determining read direction. Applications should call snd_mixer_read() function if some data is waiting to be read. It is recommended that you do this, since it leaves place for this function to handle some new kernel API specifications.

ファイルディスクリプタは読み込み方向を決めるためのselect(2)の同期多重通信関数が使われていなければなりません。アプリケーションは何らかのデータを読み込むのを待つ場合はsnd_mixer_read()関数を使うべきです。この関数を使用するのを勧めるのは、この関数の役割を何らかの新しいkernel API仕様が取り扱うように任せてからです。

int snd_mixer_channels(void *handle)

Returns the count of mixer channels for appropriate mixer device, otherwise the return value is negative, and signifies an error code. Never returns zero.

適したmixerデバイスのためのmixerチャネルの数を返します。そうでなければ負の、重大なエラーコードを返します。ゼロが返ることはありません。

int snd_mixer_info(void *handle, snd_mixer_info_t *info)

Fills the *info structure with information about the mixer associated with *handle. Returns zero if successful, otherwise it returns an error code.

*info構造体に*handleに関連するミキサについての情報を与えます。成功すれば0を返し、そうでなければエラーコードを返します。

/* mixerは排他的記録だけすることができる */

#define SND_MIXER_INFO_CAP_EXCL_RECORD   0x00000001

 

struct snd_mixer_info {

        /* サウンドカードのタイプ - SND_CARD_TYPE_XXXX */

        unsigned int type;

        /* Mixerデバイスの数 */

        unsigned int channels;

        /* デバイスについての何らかのフラグ (SND_MIXER_INFO_CAP_XXXX) */

        unsigned int caps;

        /* MixerのID */

        unsigned char id[32];

        /* デバイスの名前 */

        unsigned char name[80];

        /* 将来の拡張用の予約 */

        char reserved[ 32 ];

};

int snd_mixer_switches(void *handle)

Returns count of mixer switches. In this context 'switch' means universal control interface between kernel and application which allows various types of control. Function returns count if successful, otherwise it returns an error code. Return value will be zero if sound card doesn't have any mixer switch.

Mixerスイッチの数を返します。このスイッチとは一般的なControlインターフェースとkernelやアプリケーション間で与える多くのcontrolのタイプのことです。この関数は成功すれば0を返し、そうでなければエラーコードを返します。サウンドカードにMixerスイッチがなければ0が返るでしょう。

int snd_mixer_switch(void *handle, const char *switch_id)

Returns index for switch with name switch_id. Function returns switch index if successful, otherwise it returns an error code.

名称switch_idの示すスイッチのためにインデックスを返します。この関数は成功すれば0を返し、そうでなければエラーコードを返します。

int snd_mixer_switch_read(void *handle int switchn snd_mixer_switch_t *data)

Fills the *data structure with data about switch with index switchn. Function returns zero if successful, otherwise it returns an error code.

*data構造体にインデックスswitchnの示すスイッチに関するデータを与えます。この関数は成功すれば0を返し、そうでなければエラーコードを返します。

/* 0または1 (共用体のメンバを有効にする) */

#define SND_MIXER_SW_TYPE_BOOLEAN     0

/* 0から255 - 最小から最大 (data8[0] 共用体のメンバ) */

#define SND_MIXER_SW_TYPE_BYTE        1

/* 0から65535 - 最小から最大 (data16[0] 共用体のメンバ) */

#define SND_MIXER_SW_TYPE_WORD        2

/* 0から4294967296 ¯  最小から最大 (data32[0] 共用体のメンバ) */

#define SND_MIXER_SW_TYPE_DWORD       3

/* user type - タイプのコントロールではない */

#define SND_MIXER_SW_TYPE_USER        (~0)

 

/* 良く知られる(名付けられた)スイッチ */

#define SND_MIXER_SW_LOUDNESS         "Loudness"  /* bass boost */

#define SND_MIXER_SW_SIM_STEREO       "Simulated Stereo Enhancement"

#define SND_MIXER_SW_3D               "3D Stereo Enhancement"

/* MICの利得 */

#define SND_MIXER_SW_MIC_GAIN         "MIC Gain"

/* MICの自動利得制御 */

#define SND_MIXER_SW_MIC_GAIN         "MIC Auto-Gain-Control"

/* MICのインピーダンスの変更nbsp;*/

#define SND_MIXER_SW_MIC_GAIN         "Change MIC Impedance"

/* Line-in&を出力に変更nbsp;*/

#define SND_MIXER_SW_MIC_GAIN         "Line In to Output"

#define SND_MIXER_SW_IEC958OUT        "IEC-958 (S/PDIF) Output"

#define SND_MIXER_SW_IEC958IN         "IEC-958 (S/PDIF) Input"

 

struct snd_mixer_switch {

        /* スイッチインデックス (アプリケーションによって与えられる) */

        unsigned int switchn;

        /* スイッチの識別 (ドライバ用) */

        unsigned char name[32];

        /* type&スイッチの値のタイプ ¯  SND_CTL_SW_TYPE_XXXX参照 */

        unsigned int type;

        /* 最低値 */

        unsigned int low;

        /* 最高値 */

        unsigned int high;

        union {

                unsigned int enable;        /* 0 = off 1 = on */

                unsigned char data8[32];    /* 8-bit data */

                unsigned short data16[16];  /* 16-bit data */

                unsigned int data32[8];     /* 32-bit data */

        } value;

        /* 将来の拡張用の予約 ¯  ゼロで与えなければならない */

        unsigned char reserved[32];

}

int snd_mixer_switch_write(void *handle int switchn snd_mixer_switch_t *data)

Writes the *data structure with data about switch with index switchn to kernel. Function returns zero if successful, otherwise it returns an error code.

kernelへ*data構造体にインデックスswitchnの示すスイッチに関するデータを書き込みます。この関数は成功すれば0を返し、そうでなければエラーコードを返します。

int snd_mixer_exact_mode(void *handle, int enable)

Turns on = 1 or off = 0 (by default) exact mode. This mode allows application to set/get volume values in exact range which uses hardware. In non-exact mode range is always from percentage 0 to 100 and driver does conversion to hardware range. Function returns zero if successful, otherwise it returns an error code.

精密モードをオンなら1、オフなら0(初期値)に変えます。このモードはアプリケーションにハードウェアを使った精密な範囲でのボリュームの値をsetまたはgetすることを許します。非精密モードでは範囲は0から100パーセントで、ドライバがハードウェアレンジに変換します。この関数は成功すれば0を返し、そうでなければエラーコードを返します。

int snd_mixer_channel(void *handle, const char *channel_id)

Returns the channel number (index) associated with channel_id (channel name), or returns an error code. Note: Below mixer channel IDs are subject to change and will be extended if new hardware has support for other mixer input/output channels.

channel_id(チャネル名)と関連のあるチャネル番号(インデックス)を返すか、またはエラーコードを返します。

メモ:下のMixerチャネルIDはもし新しいハードウェアが他のMixer入出力チャネルをサポートしたときSubjectを変更するし拡張もするでしょう。

#define SND_MIXER_ID_MASTER        "Master"

#define SND_MIXER_ID_MASTER1       "Master 1"

/* digital master */

#define SND_MIXER_ID_MASTERD       "Master D"

/* second digital master */

#define SND_MIXER_ID_MASTERD1      "Master D1"

#define SND_MIXER_ID_HEADPHONE     "Headphone"

#define SND_MIXER_ID_MASTER_MONO   "Master M"

#define SND_MIXER_ID_3D            "3D Wide"

#define SND_MIXER_ID_3D_VOLUME     "3D Volume"

#define SND_MIXER_ID_3D_CENTER     "3D Center"

#define SND_MIXER_ID_3D_SPACE      "3D Space"

#define SND_MIXER_ID_3D_DEPTH      "3D Depth"

#define SND_MIXER_ID_BASS          "Bass"

#define SND_MIXER_ID_TREBLE        "Treble"

#define SND_MIXER_ID_FADER         "Fader"

#define SND_MIXER_ID_SYNTHESIZER   "Synth"

#define SND_MIXER_ID_SYNTHESIZER1  "Synth 1"

#define SND_MIXER_ID_FM            "FM"

#define SND_MIXER_ID_EFFECT        "Effect"

#define SND_MIXER_ID_DSP           "DSP"

#define SND_MIXER_ID_PCM           "PCM"

#define SND_MIXER_ID_PCM1          "PCM 1"

#define SND_MIXER_ID_LINE          "Line-In"

#define SND_MIXER_ID_MIC           "MIC"

#define SND_MIXER_ID_CD            "CD"

#define SND_MIXER_ID_VIDEO         "Video"

#define SND_MIXER_ID_PHONE         "Phone"

#define SND_MIXER_ID_GAIN          "Record-Gain"

#define SND_MIXER_ID_MIC_GAIN      "Mic-Gain"

#define SND_MIXER_ID_IGAIN         "In-Gain"

#define SND_MIXER_ID_OGAIN         "Out-Gain"

#define SND_MIXER_ID_LOOPBACK      "Loopback"

#define SND_MIXER_ID_SPEAKER       "PC Speaker"

#define SND_MIXER_ID_MONO          "Mono"

#define SND_MIXER_ID_MONO1         "Mono 1"

#define SND_MIXER_ID_MONO2         "Mono 2"

#define SND_MIXER_ID_AUXA          "Aux A"

#define SND_MIXER_ID_AUXB          "Aux B"

#define SND_MIXER_ID_AUXC          "Aux C"

int snd_mixer_channel_info(void *handle, int channel, snd_mixer_channel_info_t *info)

Fills the *info structure. The argument channel specifies channel (0 to N) for which is the info requested. Function returns zero if successful, otherwise it returns an error code.

*info構造体に与えます。引数channelはinfoを要求するためのチャネル(0からN)に特定します。この関数は成功すれば0を返し、そうでなければエラーコードを返します。

/* Mixerチャネルは録音ソース */

#define SND_MIXER_CINFO_CAP_RECORD         0x00000001

/* Mixerチャネルはステレオ */

#define SND_MIXER_CINFO_CAP_STEREO         0x00000002

/* 常に現在のドライバに置いてミュートをエミュレート */

#define SND_MIXER_CINFO_CAP_MUTE           0x00000004

/* チャネルはハードウェアミュートをサポート */

#define SND_MIXER_CINFO_CAP_HWMUTE         0x00000008

/* チャネルはデジタルミキシング(アナログではない)をする */

#define SND_MIXER_CINFO_CAP_DIGITAL        0x00000010

/* 外部入力チャネル */

#define SND_MIXER_CINFO_CAP_INPUT          0x00000020

/* 連動ミュートだけサポートされている。 */

/* 左右のチャネル別個のミュート制御はできない */

#define SND_MIXER_CINFO_CAP_JOINMUTE       0x00000040

/* 連動録音だけサポートされている *

/* 左右のチャネル別個の録音制御はできない */

#define SND_MIXER_CINFO_CAP_JOINRECORD     0x00000080

/* 左の入力から右の出力への経路がサポートされている */

#define SND_MIXER_CINFO_CAP_LTOR_OUT       0x00000100

/* 右の入力から左の出力への経路がサポートされている */

#define SND_MIXER_CINFO_CAP_RTOL_OUT       0x00000200

/* 左の入力から右のADCへの経路がサポートされている */

#define SND_MIXER_CINFO_CAP_LTOR_IN        0x00000400

/* 右の入力から左のADCへの経路がサポートされている */

#define SND_MIXER_CINFO_CAP_RTOL_IN        0x00000800

/* 出力経路はスイッチのみ(分けて使うことはできない) */

#define SND_MIXER_CINFO_CAP_SWITCH_OUT     0x00001000

/* 入力経路はスイッチのみ(分けて使うことはできない) */

#define SND_MIXER_CINFO_CAP_SWITCH_IN      0x00002000

/* たとえ出力方向がミュートでも録音することができる */

/* (loopbackを回避して) */

#define SND_MIXER_CINFO_CAP_RECORDBYMUTE   0x00004000

 

struct snd_mixer_channel_info {

        /* チャネルインデックス (アプリケーションによって与えられる) */

        unsigned int channel;

        /* 親チャネル番号またはSND_MIXER_PARENT */

        unsigned int parent;

        /* このデバイスの名前 */

        unsigned char name[12];

        /* このデバイスに関する何らかのフラグ(SND_MIXER_CINFO_XXXX) */

        unsigned int caps;

        /* 精密モードのときの最小値(または常に0) */

        int min; 

        /* 精密モードのときの最大値(または常に100) */ 

        int max;

        /* 最小デシベル値(*100倍) */

        int min_dB;

        /* 最大デシベル値(*100倍) */

        int max_dB;

        /* step decibel value (*100) */

        int step_dB;

        /* 将来拡張用の予約 */

        unsigned char reserved[16];

};

int snd_mixer_channel_read(void *handle, int channel, snd_mixer_channel_t *data)

Fills the *data structure. The argument channel specifies the channel (0 to N) for which is data requested. Function returns zero if successful, otherwise it returns an error code.

*data構造体に与えます。引数channelはdataを要求するためのチャネル(0からN)に特定します。この関数は成功すれば0を返し、そうでなければエラーコードを返します。

/* チャネル録音ソースフラグ */

#define SND_MIXER_FLG_RECORD_LEFT  0x00000001

#define SND_MIXER_FLG_RECORD_RIGHT 0x00000002

#define SND_MIXER_FLG_RECORD       0x00000003

/* ミュートチャネルフラグ */

#define SND_MIXER_FLG_MUTE_LEFT    0x00010000

#define SND_MIXER_FLG_MUTE_RIGHT   0x00020000

#define SND_MIXER_FLG_MUTE         0x00030000

/* 入力から出力への経路の用意 */

#define SND_MIXER_FLG_LTOR_OUT     0x00100000

#define SND_MIXER_FLG_RTOL_OUT     0x00200000

#define SND_MIXER_FLG_SWITCH_OUT   0x00300000

/* 入力からADCへの経路の用意 */

#define SND_MIXER_FLG_LTOR_IN      0x00400000

#define SND_MIXER_FLG_RTOL_IN      0x00800000

#define SND_MIXER_FLG_SWITCH_IN    0x00c00000

/* dB変数のデシベルでボリュームをセット */

#define SND_MIXER_FLG_DECIBEL      0x40000000

/* Kernel用の予約  アプリケーションが使ってはならない */

#define SND_MIXER_FLG_FORCE        0x80000000

 

struct snd_mixer_channel {

        /* チャネル番号(アプリケーションによって与えられる) */

        unsigned int channel;

        /* あるフラグを読み書きする(SND_MIXER_FLG_XXXX) */

        unsigned int flags;

        /* 精密モードのとき最小から最大(または0から100) */

        int left;

        /* 精密モードのとき最小から最大(または0から100) */ 

        int right;

        /* dB * 100 */

        int left_dB;

        /* dB * 100 */

        int right_dB;

        unsigned char reserved[16];

}; 

int snd_mixer_channel_write(void *handle, int channel, snd_mixer_channel_t *data)

Writes the *data structure to kernel. The channel argument specifies the channel (0 to N) for which is data is to be applied. Function returns zero if successful, otherwise it returns an error code. This functions is the opposite of snd_mixer_channel_read.

*data構造体をKernelに書き込みます。引数channelは適用されているデータための番号(0からN)にします。この関数は成功すれば0を返し、そうでなければエラーコードを返します。この関数はsnd_mixer_channel_readの対になる関数です。

int snd_mixer_read(void *handle, snd_mixer_callbacks_t *callbacks)

This function reads and parses data from driver. Parsed actions are returned back to the application using the callbacks structure. Applications should not parse data from the driver in standard cases. This function returns immediately after all data is read from driver. Does not block process.

この関数はドライバーからデータを読み込んで分析します。分析動作はcallbacks構造体を使ったアプリケーションに返されます。アプリケーションは標準のケースではドライバからのデータを分析してはいけません。プロセスはブロックしません。

typedef struct snd_mixer_callbacks {

        /* should be used by application */

        void *private_data;

        void (*channel_was_changed)(void *private_data, int channel);

        void (*switch_was_changed)(void *private_data, int switchn);

        /* reserved for future use - must be NULL!!! */ 

        void *reserved[14];

} snd_mixer_callbacks_t;

4.1.1  Examples

The following example shows installed mixer channels for sound card #0 and mixer device #0 in the system, and also sets the master volume (if present) to 50.

以下の例はシステム内のサウンドカード#0とミキサデバイス#0用のインストールされたミキサデバイスを見せて、(もしあれば)masterボリュームを50にセットします。

int card = 0, device = 0, err;

void *handle;

snd_mixer_info_t info;

snd_mixer_channel_t channel;

 

if ((err = snd_mixer_open(&handle, card, device)) < 0) {

        fprintf(stderr, "open failed: %s\n", snd_strerror(err));

        return;

}

if ((err = snd_mixer_info(handle, &info)) < 0) {

        fprintf(stderr, "info failed: %s\n", snd_strerror(err)); 

        snd_mixer_close(handle);

        return;

}

printf("Installed MIXER channels for card #i and device %i: %i\n",                                     card + 1, device, 

                                    info.channels);

master = snd_mixer_channel(handle, SND_MIXER_ID_MASTER);

if (master >= 0) {

        if ((err = snd_mixer_read(handle, master, &channel)) < 0) {                   fprintf(stderr, "master read failed: %s\n",                                                     snd_strerror( err )); 

                snd_mixer_close( handle );

                return;

        }

        channel.left = channel.right = 50;

        if ((err = snd_mixer_write(handle, master, &channel)) < 0 ) {                 fprintf(stderr, "master write failed: %s\n",                                                   snd_strerror( err )); 

                snd_mixer_close(handle);

                return;

        }

}

snd_mixer_close(handle);