Ringtone.cpp 3.92 KB
Newer Older
atraczyk's avatar
atraczyk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/**************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux                                *
* Author: Jger Nicolas <nicolas.jager@savoirfairelinux.com>              *
* Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com>          *
*                                                                         *
* This program 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.                                     *
*                                                                         *
* This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.   *
**************************************************************************/
#include "pch.h"

using namespace Windows::UI::Core;

using namespace RingClientUWP;

Ringtone::Ringtone(String^ fileName)
{
    fileName_ = fileName;
    CreateGraph()
        .then([this](task<void> t) {
        t.get();
        CreateDefaultDeviceOutputNode()
            .then([this](task<void> t) {
            t.get();
            CreateFileInputNode()
                .then([](task<void> t) {
                t.get();
            });
        });
    });
}

void
Ringtone::Start()
{
45 46
    _deviceOutputNode->OutgoingGain = startingGain;
    _graph->ResetAllNodes();
atraczyk's avatar
atraczyk committed
47 48 49 50 51
}

void
Ringtone::Stop()
{
52
    _deviceOutputNode->OutgoingGain = 0.0;
atraczyk's avatar
atraczyk committed
53 54 55 56 57
}

task<void>
Ringtone::CreateGraph()
{
58
    AudioGraphSettings^ settings = ref new AudioGraphSettings(Windows::Media::Render::AudioRenderCategory::GameChat);
atraczyk's avatar
atraczyk committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    return create_task(AudioGraph::CreateAsync(settings))
        .then([=](CreateAudioGraphResult^ result){
        if (result->Status != AudioGraphCreationStatus::Success) {
            MSG_("CreateGraph failed");
        }
        _graph = result->Graph;
    });
}

task<void>
Ringtone::CreateDefaultDeviceOutputNode()
{
    return create_task(_graph->CreateDeviceOutputNodeAsync())
        .then([=](CreateAudioDeviceOutputNodeResult^ result){
        if (result->Status != AudioDeviceNodeCreationStatus::Success) {
            MSG_("CreateDefaultDeviceOutputNode failed");
        }
        _deviceOutputNode = result->DeviceOutputNode;
77 78 79
        startingGain = _deviceOutputNode->OutgoingGain;
        _deviceOutputNode->OutgoingGain = 0.0;
        _graph->Start();
atraczyk's avatar
atraczyk committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    });
}

task<void>
Ringtone::CreateFileInputNode()
{
    Windows::ApplicationModel::Package^ package = Windows::ApplicationModel::Package::Current;
    Windows::Storage::StorageFolder^ installedLocation = package->InstalledLocation;
    return create_task(installedLocation->GetFolderAsync("Assets"))
        .then([=](StorageFolder^ assetsFolder){
        create_task(assetsFolder->GetFileAsync(fileName_))
            .then([=](StorageFile^ ringtoneFile){
            create_task(_graph->CreateFileInputNodeAsync(ringtoneFile))
                .then([=](CreateAudioFileInputNodeResult^ result){
                if (result->Status != AudioFileNodeCreationStatus::Success) {
                    MSG_("CreateFileInputNode failed");
                }
                _fileInputNode = result->FileInputNode;
                _fileInputNode->LoopCount = nullptr;
                _fileInputNode->AddOutgoingConnection(_deviceOutputNode);
            });
        });
    });
}