summaryrefslogtreecommitdiffstats
path: root/src/detection/sound/sound_haiku.cpp
blob: d69602e02e104b5da2b5dcc576a9ab82dbf8c768 (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
extern "C" {
#include "sound.h"
#include "common/strutil.h"
}
#include <MediaAddOn.h>
#include <MediaNode.h>
#include <MediaRoster.h>
#include <ParameterWeb.h>

const char* ffDetectSound(FF_A_UNUSED FFSoundOptions* options, FFlist* devices /* List of FFSoundDevice */) {
    BMediaRoster* roster = BMediaRoster::Roster();
    media_node mediaNode;
    live_node_info liveInfo;
    dormant_node_info dormantInfo;

    if (roster->GetAudioOutput(&mediaNode) != B_OK) {
        return NULL;
    }

    FFSoundDevice* device = FF_LIST_ADD(FFSoundDevice, *devices);
    ffStrbufInit(&device->identifier);
    if (roster->GetDormantNodeFor(mediaNode, &dormantInfo) == B_OK) {
        ffStrbufAppendS(&device->identifier, dormantInfo.name);
    }
    ffStrbufInit(&device->name);
    if (roster->GetLiveNodeInfo(mediaNode, &liveInfo) == B_OK) {
        ffStrbufAppendS(&device->name, liveInfo.name);
        ffStrbufTrimRightSpace(&device->name);
    }
    ffStrbufInitStatic(&device->platformApi, "MediaKit");
    // We'll check the Mixer actually
    device->volume = 0;
    device->type = (FFSoundType) (FF_SOUND_TYPE_ACTIVE | FF_SOUND_TYPE_MAIN);

    roster->ReleaseNode(mediaNode);

    media_node mixer;
    if (roster->GetAudioMixer(&mixer) != B_OK) {
        return NULL;
    }

    BParameterWeb* web;
    status_t status = roster->GetParameterWebFor(mixer, &web);
    roster->ReleaseNode(mixer); // the web is all we need :-)
    if (status != B_OK) {
        return NULL;
    }

    BContinuousParameter* gain = NULL;
    BParameter* mute = NULL;
    BParameter* parameter;
    for (int32 index = 0; (parameter = web->ParameterAt(index)) != NULL; index++) {
        // assume the mute preceding master gain control
        if (ffStrEquals(parameter->Kind(), B_MUTE)) {
            mute = parameter;
        }

        if (ffStrEquals(parameter->Kind(), B_MASTER_GAIN)) {
            // Can not use dynamic_cast due to fno-rtti
            // gain = dynamic_cast<BContinuousParameter *>(parameter);
            gain = (BContinuousParameter*) (parameter);
            break;
        }
    }

    if (gain == NULL) {
        return NULL;
    }

    bigtime_t when;
    size_t size;

    if (mute) {
        int32 isMute = false;
        size = sizeof(isMute);
        if (mute->GetValue(&isMute, &size, &when) == B_OK && isMute) {
            return NULL;
        }
    }

    float volume = 0.0;
    size = sizeof(volume);
    if (gain->GetValue(&volume, &size, &when) == B_OK) {
        device->volume = (uint8_t) (100 * (volume - gain->MinValue()) / (gain->MaxValue() - gain->MinValue()));
    }

    return NULL;
}