source: roaraudio/plugins/roard/dmx-random.c @ 5822:6000fdb72b8e

Last change on this file since 5822:6000fdb72b8e was 5709:41cdb8bba0b4, checked in by phi, 11 years ago

Added new plugin: dmx-random.

File size: 3.8 KB
Line 
1//dmx-random.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011-2012
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include <roard/include/roard.h>
27
28#define SAMPLES_PER_TICK 2
29
30struct channel {
31 int32_t endsamp;
32 int32_t cursamp;
33 unsigned char startval, endval;
34};
35
36struct state {
37 size_t startaddr;
38 size_t len;
39 struct channel * channels;
40};
41
42static struct state * g_state;
43static struct state   g_state_init = {
44 .startaddr = 0,
45 .len       = 4,
46 .channels  = NULL
47};
48
49
50static void calc_end(size_t index) {
51 if ( g_state->channels[index].cursamp <= g_state->channels[index].endsamp )
52  return;
53
54 g_state->channels[index].cursamp = 0;
55 g_state->channels[index].endsamp = (int32_t)1 + (int32_t)roar_random_uint16() * (int32_t)SAMPLES_PER_TICK;
56
57 g_state->channels[index].startval = g_state->channels[index].endval;
58
59 if ( roar_random_uint16() < 32768 )
60  return;
61
62 g_state->channels[index].endval = roar_random_uint16() & 0xE0;
63}
64
65static void calc_channel(size_t index) {
66 // trel = cursamp/endsamp
67 double trel = (double)g_state->channels[index].cursamp/(double)g_state->channels[index].endsamp;
68 double valdiff = (double)(g_state->channels[index].endval - g_state->channels[index].startval)*trel;
69 valdiff += (double)g_state->channels[index].startval;
70 light_dmxchannel_set(g_state->startaddr + index, (unsigned char)(unsigned int)(int)valdiff);
71}
72
73static int _init  (struct roar_dl_librarypara * para) {
74 struct roar_keyval * p;
75
76 p = roar_keyval_lookup(para->argv, "startaddr", para->argc, 1);
77 if ( p != NULL && p->value != NULL )
78  g_state->startaddr = atoi(p->value);
79
80 p = roar_keyval_lookup(para->argv, "len", para->argc, 1);
81 if ( p != NULL && p->value != NULL )
82  g_state->len = atoi(p->value);
83
84 g_state->channels = roar_mm_malloc(g_state->len*sizeof(struct channel));
85 if ( g_state->channels == NULL )
86  return -1;
87
88 memset(g_state->channels, 0, g_state->len*sizeof(struct channel));
89
90 return 0;
91}
92
93static int _free  (struct roar_dl_librarypara * para) {
94 (void)para;
95
96 if ( g_state->channels != NULL )
97  roar_mm_free(g_state->channels);
98 g_state->channels = NULL;
99 return 0;
100}
101
102static int _update  (struct roar_dl_librarypara * para) {
103 size_t i;
104
105 (void)para;
106
107 for (i = 0; i < g_state->len; i++) {
108  calc_end(i);
109  calc_channel(i);
110  g_state->channels[i].cursamp += ROAR_OUTPUT_BUFFER_SAMPLES;
111 }
112
113 return 0;
114}
115
116static struct roar_dl_appsched sched = {
117 .init   = _init,
118 .free   = _free,
119 .update = _update,
120 .tick   = NULL,
121 .wait   = NULL
122};
123
124ROAR_DL_PLUGIN_START(dmx_random) {
125 ROARD_DL_CHECK_VERSIONS();
126
127 ROAR_DL_PLUGIN_META_PRODUCT_NIV("dmx-random", ROAR_VID_ROARAUDIO, ROAR_VNAME_ROARAUDIO);
128 ROAR_DL_PLUGIN_META_VERSION(ROAR_VERSION_STRING);
129 ROAR_DL_PLUGIN_META_LICENSE_TAG(GPLv3_0);
130 ROAR_DL_PLUGIN_META_CONTACT_FLNE("Philipp", "Schafft", "ph3-der-loewe", "lion@lion.leolix.org");
131 ROAR_DL_PLUGIN_META_DESC("This generates random light effects using roard's DMX interface.");
132
133 ROAR_DL_PLUGIN_REG_GLOBAL_DATA(g_state, g_state_init);
134 ROAR_DL_PLUGIN_REG_APPSCHED(&sched);
135} ROAR_DL_PLUGIN_END
136
137//ll
Note: See TracBrowser for help on using the repository browser.