File: blender.c

package info (click to toggle)
freebirth 0.3.2-9.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 3,632 kB
  • ctags: 640
  • sloc: ansic: 4,724; makefile: 89; sh: 1
file content (154 lines) | stat: -rw-r--r-- 3,915 bytes parent folder | download | duplicates (4)
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
/* Freebirth
 * Copyright (C) 1999 topher lafata <topher@topher.com>,
 *		      Jake Donham <jake@bitmechanic.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program (see COPYING); if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "blender.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void blender_set_strength(blender *this,int amp,int index)
{
  int i;
  /* make sure we don't run off the end of the array */
  if(index >= this->length || index < 0)
    return;
  this->strength[index] = amp;
  this->blend_factor = 1;
  for(i = 0;i < this->length; i++)
    this->blend_factor += this->strength[i];
}

void blender_next_buffer(blender *this)
{
  this->next = 1;
}

sample *blender_get_buffer(blender *this)
{
  int i,x;

  if (this->next) {
    this->next = 0;
    for(x = 0; x < this->length;x++)
      this->bufs[x] = this->source[x]->get_buffer(this->source[x]);
    
    for(i = 0; i < TBASS_BUFF_SIZE;i++) {
      long tmp = 0;
      for (x = 0; x < this->length; x++)
	tmp += (long)((this->bufs[x][i] * this->strength[x]) / this->blend_factor);
      this->buffer[i] = tmp;
    }
  }

  return this->buffer; 
  
}


static sample_producer **get_children(blender *this)
{
  return this->source;
}

static char **get_header(blender *this)
{
  if (!this->header) {
    int i;
    this->header = (char **)malloc(sizeof(char *) * this->length + 2);
    this->header[0] = "int $n_blend = ((blender *)$t)->blend_factor;";
    for (i=0; i < this->length; i++) {
      this->header[i + 1] = (char *)malloc(sizeof(char) * 80);
      sprintf(this->header[i + 1],
	      "int $n_strength%d = ((blender *)$t)->strength[%d];",
	      i, i);
    }
    this->header[this->length + 1] = NULL;
  }

  return this->header;
}

static char **get_code(blender *this)
{
  if (!this->code) {
    int i;
    char *cp;
    this->code = (char **)malloc(sizeof(char *) * 2);
    // XXX make sure this is enough memory
    this->code[0] = (char *)malloc(sizeof(char) * (this->length * 20 + 50));
    cp = this->code[0];
    cp += sprintf(cp, "$o = ");
    for (i=0; i < this->length; i++) {
      cp += sprintf(cp, "($i%d * $n_strength%d / $n_blend)%s",
		    i, i, (i == this->length - 1) ? "" : " + ");
    }
    sprintf(cp, ";");
    this->code[1] = NULL;
  }

  return this->code;
}

static char **get_footer(blender *this)
{
  static char *footer[] = { NULL };
  return footer;
}


blender* blender_new(sample_producer **sources)
{
  int i;
  sample_producer **tmp = sources;

  blender* out      = (blender *)malloc(sizeof(blender));
  out->source       = sources;
  out->length       = 0;
  while (*tmp++) out->length++;
  out->strength     = (int *)malloc(sizeof(int) * out->length);
  out->get_buffer   = blender_get_buffer;
  out->next_buffer  = blender_next_buffer;
  out->get_children = get_children;
  out->get_header   = get_header;
  out->get_code	    = get_code;
  out->get_footer   = get_footer;
  out->header	    = NULL;
  out->code	    = NULL;
  out->next	    = 0;
  out->buffer       = (sample*)malloc(sizeof(sample) * TBASS_BUFF_SIZE);
  out->bufs         = (sample **)malloc(sizeof(sample *) * out->length);
  out->blend_factor = 0;
  for(i=0; i < out->length; i++)
    out->strength[i] = 0;
  return out;
}

/*
  Local Variables:
  mode: font-lock
  End:
*/