summaryrefslogtreecommitdiffstats
path: root/Cancer/Kübelwagen/index.c
blob: 9a15c7c3ed8870afd63d86f29ca627fb03071455 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <math.h>

#include <jack/jack.h>

const double PI = 3.14;

/*Our output port*/
jack_port_t *output_port;

typedef jack_default_audio_sample_t sample_t;

/*The current sample rate*/
jack_nframes_t sr;

/*samples in cycle*/
jack_nframes_t samincy;
/*the current offset*/
long offset=0;

/*frequency of our sound*/
int tone = 262;
int length = 1000000;

char const *name = "<undefined>";

int process (jack_nframes_t nframes, void *arg){
  /*grab our output buffer*/
  sample_t *out = (sample_t *) jack_port_get_buffer 
                               (output_port, nframes);

  int _tone = tone > 0 ? tone : 1;

  /*For each required sample*/
  for(jack_nframes_t i=0;i < nframes;i++){
    /*Copy the sample at the current position in the cycle to the buffer*/

    jack_nframes_t samincy = sr / _tone;
    sample_t scale = 2 * PI / samincy;
    out[i] = sin(offset * scale);

    /*and increment the offset, wrapping to 0 if needed*/
    /*(Dumb increment fixed thanks to Jussi Sainio)*/
    offset++;
    if(offset >= samincy)
      offset = 0;    
  }
           
  return 0;      
}

int srate (jack_nframes_t nframes, void *arg){
  printf ("the sample rate is now %lu/sec\n", nframes);
  sr=nframes;
  return 0;
}

void error (const char *desc){
  fprintf (stderr, "JACK error: %s\n", desc);
}

void jack_shutdown (void *arg){
  exit (1);
}

void usage(void) {
  fprintf (stderr, "usage: %s [Hz [ms]]\n", name);
}

int main (int argc, char *argv[]){
  jack_client_t *client;
  const char **ports;

  name = argv[0];
  
  if (argc < 2) {
    usage();
    return 1;
  }
  if (argc >= 2) {
    tone = atoi(argv[1]);
    if (tone == 0) {
      usage();
      return 1;
    } 
    fprintf(stderr, "tone: %dHz\n", tone);
    if (argc >= 3) {
      length = atoi(argv[2]) * 1000;
      if (length == 0) {
        usage();
        return 1;
      } 
      fprintf(stderr, "length: %dms\n", length/1000);
    }
  }
  
  /* tell the JACK server to call error() whenever it
     experiences an error.  Notice that this callback is
     global to this process, not specific to each client.
     
     This is set here so that it can catch errors in the
     connection process
  */
  jack_set_error_function (error);
  
  /* try to become a client of the JACK server */
  
  if ((client = jack_client_open(argv[0], JackNullOption, NULL)) == 0) {
    fprintf (stderr, "jack server not running?\n");
    return 1;
  }
  
  /* tell the JACK server to call `process()' whenever
     there is work to be done.
  */
  
  jack_set_process_callback (client, process, 0);
  
  /* tell the JACK server to call `srate()' whenever
     the sample rate of the system changes.
  */
  
  
  jack_set_sample_rate_callback (client, srate, 0);
  
  /* tell the JACK server to call `jack_shutdown()' if
     it ever shuts down, either entirely, or if it
     just decides to stop calling us.
  */
  
  jack_on_shutdown (client, jack_shutdown, 0);
  
  /* display the current sample rate. once the client is activated 
     (see below), you should rely on your own sample rate
     callback (see above) for this value.
  */
  printf ("engine sample rate: %lu\n", jack_get_sample_rate (client));
  

  sr=jack_get_sample_rate (client);
  
  /* create two ports */
  
  
  output_port = jack_port_register (client, "output", 
                     JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);

  /* tell the JACK server that we are ready to roll */
  
  if (jack_activate (client)) {
    fprintf (stderr, "cannot activate client");
    return 1;
  }
  
  /* connect the ports*/
  if ((ports = jack_get_ports (client, NULL, NULL, 
                   JackPortIsPhysical|JackPortIsInput)) == NULL) {
    fprintf(stderr, "Cannot find any physical playback ports\n");
    exit(1);
  }
  
  int i=0;
  while(ports[i]!=NULL){
    if (jack_connect (client, jack_port_name (output_port), ports[i])) {
      fprintf (stderr, "cannot connect output ports\n");
    }
    i++;
  }
  
  free (ports);

  //while (1) { //scanf("%i %i", &tone, &length) == 2) {
  char buf[BUFSIZ];
  while (fgets(buf, BUFSIZ, stdin) == buf) {
    //if (strstr(buf, "Hz\n")) {
    //  sscanf(buf, "%i", &tone);
    //  fprintf(stderr, "%dHz\n", tone);
    //}
    //if (strstr(buf, "us\n")) {
    //  sscanf(buf, "%i", &length);
    //  fprintf(stderr, "%dus\n", length);
    //}

    int length = 0;
    int c;
    int pos = 0, n;
    while ((c = sscanf(buf + pos, "%i%i%n", &tone, &length, &n)) == 2) {

      if (length > 0) {
        usleep(length * 1000);
      }

      pos += n;

      fprintf(stderr, "%dHz %dms\n", tone, length);
    }
  }
  ///* 3 seconds of bleep is plenty*/
  ///usleep(length);
  jack_client_close(client);

  exit(0);
}