source: roaraudio/plugins/roard/dmx-random.c @ 5907:e4b706566364

Last change on this file since 5907:e4b706566364 was 5907:e4b706566364, checked in by phi, 6 years ago

support samples-per-tick parameter

File size: 4.0 KB
Line 
1//dmx-random.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011-2013
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
28struct channel {
29 int32_t endsamp;
30 int32_t cursamp;
31 unsigned char startval, endval;
32};
33
34struct state {
35 size_t startaddr;
36 size_t len;
37 int32_t samples_per_tick;
38 struct channel * channels;
39};
40
41static struct state * g_state;
42static struct state   g_state_init = {
43 .startaddr        = 0,
44 .len              = 4,
45 .samples_per_tick = 2, /* default: 2, good for fireflies, for moodlight 8 seems to be a better value */
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() * g_state->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 p = roar_keyval_lookup(para->argv, "samples-per-tick", para->argc, 1);
85 if ( p != NULL && p->value != NULL )
86  g_state->samples_per_tick = atoi(p->value);
87
88 g_state->channels = roar_mm_malloc(g_state->len*sizeof(struct channel));
89 if ( g_state->channels == NULL )
90  return -1;
91
92 memset(g_state->channels, 0, g_state->len*sizeof(struct channel));
93
94 return 0;
95}
96
97static int _free  (struct roar_dl_librarypara * para) {
98 (void)para;
99
100 if ( g_state->channels != NULL )
101  roar_mm_free(g_state->channels);
102 g_state->channels = NULL;
103 return 0;
104}
105
106static int _update  (struct roar_dl_librarypara * para) {
107 size_t i;
108
109 (void)para;
110
111 for (i = 0; i < g_state->len; i++) {
112  calc_end(i);
113  calc_channel(i);
114  g_state->channels[i].cursamp += ROAR_OUTPUT_BUFFER_SAMPLES;
115 }
116
117 return 0;
118}
119
120static struct roar_dl_appsched sched = {
121 .init   = _init,
122 .free   = _free,
123 .update = _update,
124 .tick   = NULL,
125 .wait   = NULL
126};
127
128ROAR_DL_PLUGIN_START(dmx_random) {
129 ROARD_DL_CHECK_VERSIONS();
130
131 ROAR_DL_PLUGIN_META_PRODUCT_NIV("dmx-random", ROAR_VID_ROARAUDIO, ROAR_VNAME_ROARAUDIO);
132 ROAR_DL_PLUGIN_META_VERSION(ROAR_VERSION_STRING);
133 ROAR_DL_PLUGIN_META_LICENSE_TAG(GPLv3_0);
134 ROAR_DL_PLUGIN_META_CONTACT_FLNE("Philipp", "Schafft", "ph3-der-loewe", "lion@lion.leolix.org");
135 ROAR_DL_PLUGIN_META_DESC("This generates random light effects using roard's DMX interface.");
136
137 ROAR_DL_PLUGIN_REG_GLOBAL_DATA(g_state, g_state_init);
138 ROAR_DL_PLUGIN_REG_APPSCHED(&sched); 
139} ROAR_DL_PLUGIN_END
140
141//ll
Note: See TracBrowser for help on using the repository browser.