summaryrefslogtreecommitdiff
path: root/src/lib/ecore_audio/ecore_audio_alsa.c
blob: 2496426d809c2e7394922c665c24b5742ac358a4 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_ALSA

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifdef HAVE_FEATURES_H
#include <features.h>
#endif
#include <ctype.h>
#include <errno.h>

#include <alsa/asoundlib.h>

#include "ecore_audio_private.h"

static Ecore_Audio_Module *alsa_module = NULL;

static Ecore_Audio_Object *
_alsa_output_new(Ecore_Audio_Object *output)
{
   int ret;
   Ecore_Audio_Output *out = (Ecore_Audio_Output *)output;
   struct _Ecore_Audio_Alsa *alsa;

   alsa = calloc(1, sizeof(struct _Ecore_Audio_Alsa));
   if (!alsa)
     {
        ERR("Could not allocate memory for private structure.");
        free(out);
        return NULL;
     }

   out->module_data = alsa;

   ret = snd_pcm_open(&alsa->handle, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
   if (ret < 0)
     {
        ERR("Could not open playback: %s", snd_strerror(ret));
        goto err;
     }

   /* XXX: Make configurable */
   ret = snd_pcm_set_params(alsa->handle, SND_PCM_FORMAT_FLOAT, SND_PCM_ACCESS_RW_INTERLEAVED, 2, 44100, 1, 500000);
   if (ret < 0)
     {
        ERR("Could not set parameters: %s", snd_strerror(ret));
        goto err;
     }

   return output;

err:
   free(out->module_data);
   return NULL;
}

static void
_alsa_output_del(Ecore_Audio_Object *output)
{
   free(output->module_data);
}

static Eina_Bool
_alsa_output_async_cb(Ecore_Audio_Object *output)
{
   unsigned char *data;
   int size, ret;
   snd_pcm_sframes_t avail;
   Ecore_Audio_Output *out = output;
   Ecore_Audio_Input *in = eina_list_data_get(out->inputs);
   struct _Ecore_Audio_Alsa *alsa = (struct _Ecore_Audio_Alsa *)output->module_data;

   /* XXX: Error handling! */
   avail = snd_pcm_avail_update(alsa->handle);
   ERR("Aval: %i", avail);
   if (!avail)
     return EINA_TRUE;
   if (avail < 0)
     goto recover;

   data = calloc(1, sizeof(float) * avail * 8);

   size = in->module->in_ops->input_read((Ecore_Audio_Object *)in, data, avail*8);
   ERR("Read: %i", size);

   ret = snd_pcm_writei(alsa->handle, data, size/8);
   ERR("Written: %i", ret);
   if (ret < 0) {
recover:
       ERR(snd_strerror(ret));
     snd_pcm_recover(alsa->handle, ret, 0);
   }

   return EINA_TRUE;
}

static void
_alsa_output_add_input(Ecore_Audio_Object *output, Ecore_Audio_Object *input)
{
   ecore_timer_add(0.3, _alsa_output_async_cb, output);
}

static struct input_api inops = {
};

static struct output_api outops = {
    .output_new = _alsa_output_new,
    .output_del = _alsa_output_del,
//    .output_volume_set = _alsa_output_volume_set,
    .output_add_input = _alsa_output_add_input,
//    .output_del_input = _alsa_output_del_input,
//    .output_update_input_format = _alsa_output_update_input_format,
};

/* externally accessible functions */

/**
 * @addtogroup Ecore_Audio_Group Ecore_Audio_alsa - Ecore internal ALSA audio functions
 *
 * @{
 */

/**
 * @brief Initialize the Ecore_Audio ALSA module.
 *
 * @return the initialized module success, NULL on error.
 *
 */
Ecore_Audio_Module *
ecore_audio_alsa_init(void)
{
   alsa_module = calloc(1, sizeof(Ecore_Audio_Module));
   if (!alsa_module)
     {
        ERR("Could not allocate memory for the module.");
        return NULL;
     }

   ECORE_MAGIC_SET(alsa_module, ECORE_MAGIC_AUDIO_MODULE);
   alsa_module->type = ECORE_AUDIO_TYPE_ALSA;
   alsa_module->name = "alsa";
   alsa_module->inputs = NULL;
   alsa_module->outputs = NULL;
   alsa_module->in_ops = &inops;
   alsa_module->out_ops = &outops;

   return alsa_module;
}

/**
 * @brief Shut down the Ecore_Audio ALSA module
 */
void
ecore_audio_alsa_shutdown(void)
{
   free(alsa_module);
   alsa_module = NULL;
}

/**
 * @}
 */

#endif /* HAVE_ALSA */