~ubuntu-branches/ubuntu/vivid/jnoise/vivid

« back to all changes in this revision

Viewing changes to source/jnoise.cc

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2011-02-16 15:39:06 UTC
  • mfrom: (2.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20110216153906-r7xl717kpg6poncm
Tags: 0.6.0-2
* Upload to unstable.
* Add myself as uploader.
* Relax build-deps on JACK to let this build on Debian sid.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2003-2010 Fons Adriaensen <fons@kokkinizita.net>
 
3
    
 
4
    This program is free software; you can redistribute it and/or modify
 
5
    it under the terms of the GNU General Public License as published by
 
6
    the Free Software Foundation; either version 2 of the License, or
 
7
    (at your option) any later version.
 
8
 
 
9
    This program is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
    GNU General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU General Public License
 
15
    along with this program; if not, write to the Free Software
 
16
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
*/
 
18
 
 
19
 
 
20
// --------------------------------------------------------------------------------
 
21
 
 
22
 
 
23
#include <stdlib.h>
 
24
#include <stdio.h>
 
25
#include <math.h>
 
26
#include <unistd.h>
 
27
#include <jack/jack.h>
 
28
#include "rngen.h"
 
29
 
 
30
 
 
31
 
 
32
static jack_client_t  *jack_handle;
 
33
static jack_port_t    *jack_out1;
 
34
static jack_port_t    *jack_out2;
 
35
static Rngen          rngen;
 
36
static float          b0, b1, b2, b3, b4, b5, b6;
 
37
static float          gain = 1.0f;
 
38
 
 
39
 
 
40
static void process (int n, float *op1, float *op2)
 
41
{
 
42
    float x;
 
43
 
 
44
    while (n--)
 
45
    {
 
46
        x = gain * rngen.grandf ();
 
47
        *op1++ = 0.07071f * x;             
 
48
        x *= 0.023f;
 
49
        b0 = 0.99886f * b0 + 0.0555179f * x;
 
50
        b1 = 0.99332f * b1 + 0.0750759f * x;
 
51
        b2 = 0.96900f * b2 + 0.1538520f * x;
 
52
        b3 = 0.86650f * b3 + 0.3104856f * x;
 
53
        b4 = 0.55000f * b4 + 0.5329522f * x;
 
54
        b5 = -0.7616f * b5 - 0.0168980f * x;
 
55
        *op2++ = b0 + b1 + b2 + b3 + b4 + b5 + b6 + x * 0.5362f;
 
56
        b6 = x * 0.115926f;
 
57
    }
 
58
}
 
59
 
 
60
 
 
61
static void jack_shutdown (void *arg)
 
62
{
 
63
    exit (1);
 
64
}
 
65
 
 
66
 
 
67
static int jack_callback (jack_nframes_t nframes, void *arg)
 
68
{
 
69
    float *op1, *op2;
 
70
 
 
71
    op1 = (float *)(jack_port_get_buffer (jack_out1, nframes));
 
72
    op2 = (float *)(jack_port_get_buffer (jack_out2, nframes));
 
73
    process (nframes, op1, op2);
 
74
    return 0;
 
75
}
 
76
 
 
77
 
 
78
int main (int ac, char *av [])
 
79
{
 
80
    float           g;
 
81
    jack_options_t  opts;
 
82
    jack_status_t   stat;
 
83
 
 
84
    if (ac > 1)
 
85
    {
 
86
        g = atof (av [1]);
 
87
        if (g > -10) 
 
88
        {
 
89
            fprintf (stderr, "Level is too high, max = -10dB\n");
 
90
            return 1;
 
91
        }
 
92
        gain = powf (10.0f, 0.05f * (g + 20));
 
93
    }
 
94
    
 
95
    opts = JackNoStartServer;
 
96
    if ((jack_handle = jack_client_open ("jnoise", opts, &stat)) == 0)
 
97
    {
 
98
        fprintf (stderr, "Can't connect to JACK\n");
 
99
        return 1;
 
100
    }
 
101
 
 
102
    jack_set_process_callback (jack_handle, jack_callback, 0);
 
103
    jack_on_shutdown (jack_handle, jack_shutdown, 0);
 
104
    jack_out1 = jack_port_register (jack_handle, "white", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
 
105
    jack_out2 = jack_port_register (jack_handle, "pink",  JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
 
106
 
 
107
    rngen.init (0);
 
108
 
 
109
    if (jack_activate (jack_handle))
 
110
    {
 
111
        fprintf(stderr, "Can't activate JACK");
 
112
        return 1;
 
113
    }
 
114
 
 
115
    while (1) usleep (200000);
 
116
    return 0;
 
117
}
 
118
 
 
119
 
 
120
// --------------------------------------------------------------------------------