1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Alexa APL for Audio (APL-A) をスキルに実装する

Last updated at Posted at 2020-11-03

APL for Audio (APLA)

APL for Audioは、Alexaデバイスの新しいオーディオミキシング機能で、APLAドキュメント形式を使用してデバイスに音声レスポンスを送信するもの。
APLAは、APLコンポーネントを使用してオーディオを定義することができ、すべてのAlexaデバイスでAPLAを再生できる。

※2021.7.21のAlexa Liveで一般公開され、**.addDirectiveToRepromptが追加され、リプロンプト**でもAPLAが使えるようになった。さらにサンプルレートが、44.1kHzから48kHzに変更された。

APL for Audio (APLA) is Now Generally Available
Launched as a Public Beta at last year’s Alexa Live, APLA is now generally available (GA)! We want to thank everyone who participated in the beta and for the feedback you provided to make the service better. GA is an important milestone but, as Amazonians like to say, it’s still day one. We’re still working on a lot of new features and even have a couple to announce today!

オーディオファイルのフォーマット

以下のフォーマットをサポートしている。

No. フォーマット
1 aac
2 mp3
3 ogg
4 opus
5 wav

オーディオファイルの要件

以下の要件を満たしていないと再生されない。
満たしていない場合は、FFmpegやフリーのオーディオ変換サイトで簡単に変換することができる。

No. 項目 要件
1 ビットレート 1411.20 kbps 以下
2 サンプルレート 48kHz 以下
3 ファイルサイズ 10MB 以内
4 長さ 240秒 以内

オーディオファイル数

1回のリクエストで使用できるオーディオファイルは15ファイルまで。
1つのAPLA内に使用できるオーディオファイルの最大が15ファイルということ。

APLAコンポーネント

APLAは、次のプリミティブコンポーネントをサポートしている。

No. コンポーネント 説明
1 Audio オーディオを再生
2 Mixer オーディオの同時再生/テキストとオーディオの同時再生
3 Selector 設定した配列の中から1つを選択し再生
4 Sequencer テキストまたはオーディオを順次再生
5 Silence 音なし(一時停止)
6 Speech テキストまたはSSMLを再生

オーディオフィルター機能

オーディオに変化を加えることができる。

No. フィルター 説明
1 Trim filter 指定した位置でオーディオをトリムする
2 FadeIn filter オーディオのフェードイン
3 FadeOut filter オーディオのフェードアウト
4 Volume filter オーディオのボリュームを調整

APL for Audio をスキルに実装する

JSONファイルを3つ( directive.jsondocument.jsondatasources.json )用意する。ファイル名は任意のファイル名をつけて問題ない。そしてファイルを3つに分けなくても特に問題ない。昔からのクセで必ずわけるようにしている。

responseBuilder.addDirective に設定するJSONファイル。

directive.json
{
    "type": "Alexa.Presentation.APLA.RenderDocument",
    "token": "token",
    "document": apla_document,         ← ドキュメント
    "datasources": apla_datasources,   ← データソース
}

document.json は、document.jsonディレクティブのドキュメントを担う部分。
以下は、Mixer コンポーネントを使用した例。
mainTemplateitemtypeMixer を設定。

document.json(ドキュメント)
{
    "type": "APLA",
    "version": "0.8",
    "mainTemplate": {
        "parameters": [
            "payload"
        ],
        "item": {
            "type": "Mixer",
            "items": [
                {
                    "type": "Speech",
                    "contentType": "SSML",
                    "content": "<speak>${payload.myData.ssml}</speak>"
                },
                {
                    "type": "Audio",
                    "when": "${payload.myData.audio != ''}",
                    "source":"${payload.myData.audio}",
                    "filters": [
                        {
                            "type": "Volume",
                            "amount": "40%"
                        },
                        {
                            "type": "FadeIn",
                            "duration": 300
                        },
                        {
                            "type": "FadeOut",
                            "duration": 300
                        }
                    ]
                }
            ]
        }
    }
}

datasources.json は、ディレクティブのデータソースを担う部分。ドキュメント側でこの値が拾える。
データソースは、Mixer 用に、テキスト文字列と、オーディオのURLが設定できるようにしたもの。

datasources.json(データソース)
"myData": {
    "ssml": "",
    "audio": ""
}

speakの代わりにAPLAのdirective(addDirective)を追加する

sample
const apla_document = require('document.json');
const apla_datasources = require('datasources.json');
const apla_directive = require('directive.json');

const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle(handlerInput) {
        const ssml  = 'しゃべらせる文字列';
        const audio = 'https://..../..../audio.mp3';

        apla_datasources.myData.ssml = ssml;
        apla_datasources.myData.audio = audio;

        apla_directive.document = apla_document;
        apla_directive.datasources = apla_datasources;

        return handlerInput.responseBuilder
            .addDirective(apla_directive)
            .getResponse();
    }
}

repromptの代わりにAPLAのdirective(addDirectiveToReprompt)を追加する

2021年7月21日 Alexa Live にて、APL for Audioがリプロンプトにも対応したことが発表された。
リプロンプトにディレクティブを追加する時は、addDirectiveToReprompt を使用する。

reprompt
        return handlerInput.responseBuilder
            .addDirective(apla_directive)
            .addDirectiveToReprompt(apla_directive)
            .getResponse();
    }
}

実行順序

ディレクティブが上から順番に実行される。
APLよりAPL for Audioを先に実行したければ、APL for Audioを最初に記述する。
APLの AnimateItem を使ってる場合は、これを変更するだけでスキルの雰囲気が変わることがある。

sample
        return handlerInput.responseBuilder
            .addDirective( APLのdirective )             最初に事項される
            .addDirective( APL for Audioのdirective )   次に実行される
            .getResponse();
    }
}

※Alexa Developer Console でJSONを作成する

Alexa Developer ConsoleでもAPLAのJSONが作成できる。
左側のナビゲーションからマルチモーダル をクリックする。

image.png

Create Audio Response をクリックする。

image.png

そうするとJSONが表示されるので、これをコピーしてベースにするのも良い。

image.png

参考

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?