summaryrefslogtreecommitdiff
path: root/src/lib/ecore_audio/ecore_audio_tone.c
blob: 08713c1db9ea14a8fc112b3510661a17d8324df5 (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
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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "Ecore.h"
#include "ecore_private.h"
#include "Ecore_Audio.h"
#include "ecore_audio_private.h"

#include <math.h>

static Ecore_Audio_Module *module = NULL;

static Ecore_Audio_Object *
_tone_input_new(Ecore_Audio_Object *input)
{
   Ecore_Audio_Input *in = (Ecore_Audio_Input *)input;
   struct _Ecore_Audio_Tone *tone;

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

   in->samplerate = 44100;
   in->channels = 1;

   in->module_data = tone;

   in->length = 1;
   tone->freq = 1000;
   tone->phase = 0;

   return (Ecore_Audio_Object *)in;
}

static void
_tone_input_del(Ecore_Audio_Object *input)
{
   Ecore_Audio_Input *in = (Ecore_Audio_Input *)input;
   free(in->module_data);
}

static int
_tone_input_read(Ecore_Audio_Object *input, void *data, int len)
{
   Ecore_Audio_Input *in = (Ecore_Audio_Input *)input;
   struct _Ecore_Audio_Tone *tone = in->module_data;
   float *val = data;
   int i, remain;

   remain = in->length * in->samplerate * 4 - tone->phase * 4;
   if (remain > len)
     remain = len;

   for (i = 0; i < remain / 4; i++)
     {
        val[i] = sin(2 * M_PI * tone->freq * (tone->phase + i) / in->samplerate);
     }
   tone->phase += i;

   return remain;
}

static double
_tone_input_seek(Ecore_Audio_Object *input, double offs, int mode)
{
   Ecore_Audio_Input *in = (Ecore_Audio_Input *)input;
   struct _Ecore_Audio_Tone *tone = in->module_data;
   int tmp;

   switch (mode) {
     case SEEK_SET:
       tmp = offs * in->samplerate;
       break;
     case SEEK_CUR:
       tmp = tone->phase + offs * in->samplerate;
       break;
     case SEEK_END:
       tmp = (in->length + offs) * in->samplerate;
       break;
     default:
       return -1;
   }

   if ((tmp < 0) || (tmp > in->length * in->samplerate))
     return -1;

   tone->phase = tmp;
   return (double)tone->phase / in->samplerate;
}

static struct input_api inops = {
   .input_new = _tone_input_new,
   .input_del = _tone_input_del,
   .input_read = _tone_input_read,
   .input_seek = _tone_input_seek,
};

EAPI void ecore_audio_input_tone_frequency_set(Ecore_Audio_Object *input, int freq)
{
   Ecore_Audio_Input *in = (Ecore_Audio_Input *)input;
   struct _Ecore_Audio_Tone *tone = in->module_data;

   tone->freq = freq;
}

EAPI void ecore_audio_input_tone_duration_set(Ecore_Audio_Object *input, double duration)
{
   Ecore_Audio_Input *in = (Ecore_Audio_Input *)input;

   in->length = duration;
}


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

   ECORE_MAGIC_SET(module, ECORE_MAGIC_AUDIO_MODULE);
   module->type = ECORE_AUDIO_TYPE_TONE;
   module->name = "tone";
   module->priv = NULL;
   module->inputs = NULL;
   module->outputs = NULL;
   module->in_ops = &inops;
   module->out_ops = NULL;

   DBG("Initialized");
   return module;
}

/**
 * @brief Shut down the Ecore_Audio sndfile module
 */
void
ecore_audio_tone_shutdown(void)
{
   free(module);
   module = NULL;

   DBG("Shutting down");
}