aboutsummaryrefslogtreecommitdiff
path: root/pd/src/s_audio_esd.c
blob: eb08815d647c11e6d76a9394f07ae677393721a7 (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
/* Copyright (c) 2006 Guenter Geiger, 
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

#ifdef USEAPI_ESD

#include <stdio.h>
#include <string.h> /* memset */
#include <esd.h>
#include "m_pd.h"
#include "s_stuff.h"
#include "m_fixed.h"

/* exported variables */

float sys_dacsr;


/* private data */

static int esd_socket_out;
static int esd_channels_out;

static int esd_socket_in;
static int esd_channels_in;


int esd_open_audio(int nindev,  int *indev,  int nchin,  int *chin,
    int noutdev, int *outdev, int nchout, int *chout, int rate)
{ 

    esd_format_t format = ESD_BITS16 | ESD_STEREO | ESD_STREAM | ESD_PLAY;

    indev[0] = 0;
    nindev = 1;
    
    outdev[0] = 0;
    noutdev = 1;

    rate = sys_dacsr = ESD_DEFAULT_RATE;
     
    if (*chout == 2) {
      esd_socket_out = esd_play_stream_fallback(format, ESD_DEFAULT_RATE, NULL, "pd");
      if (esd_socket_out <= 0) {
        fprintf (stderr, "Error at esd open: %d\n", esd_socket_out);
        esd_channels_out = *chout = 0;
        return 0;
      }
      esd_channels_out = *chout = 2;
    }
    
    if (*chin == 2) {
      esd_socket_in = esd_record_stream_fallback(format, ESD_DEFAULT_RATE, NULL, "pd-in");
      if (esd_socket_in <= 0) {
        fprintf (stderr, "Error at esd open: %d\n", esd_socket_in);
        esd_channels_in = *chin = 0;
        return 0;
      }
      esd_channels_in = *chin = 2;
    }
    return (0);
}

void esd_close_audio( void)
{
  close(esd_socket_out);
  close(esd_socket_in);
}


#define DEFDACBLKSIZE 64
#define MAXCHANS 2

static short buf[DEFDACBLKSIZE*MAXCHANS];

int esd_send_dacs(void)
{
  t_sample* fp1,*fp2;
  short* sp;
  int i,j;

  /* Do input */

  if (esd_channels_in) {
    read(esd_socket_in,buf,DEFDACBLKSIZE*esd_channels_out*2);  
    for (i = DEFDACBLKSIZE,  fp1 = sys_soundin,
                 sp = (short *)buf; i--; fp1++, sp += esd_channels_in) {
      for (j=0, fp2 = fp1; j<esd_channels_in; j++, fp2 += DEFDACBLKSIZE)
        {
          int s = INVSCALE16(sp[j]);
          *fp2 = s;
        }
    }
  }

  /* Do output */

  if (esd_channels_out) {
    for (i = DEFDACBLKSIZE,  fp1 = sys_soundout,
         sp = (short *)buf; i--; fp1++, sp += esd_channels_out) {
      for (j=0, fp2 = fp1; j<esd_channels_out; j++, fp2 += DEFDACBLKSIZE)
        {
          int s = SCALE16(*fp2);
          if (s > 32767) s = 32767;
          else if (s < -32767) s = -32767;
          sp[j] = s;
        }
    }
  
    write(esd_socket_out,buf,DEFDACBLKSIZE*esd_channels_out*2);  
  }
  
  memset(sys_soundin,0,DEFDACBLKSIZE*esd_channels_out*2);
  memset(sys_soundout,0,DEFDACBLKSIZE*esd_channels_out*4);

  return (SENDDACS_YES);
}

void esd_listdevs( void)
{
    post("device listing not implemented for ESD yet\n");
}

void esd_getdevs(char *indevlist, int *nindevs,
    char *outdevlist, int *noutdevs, int *canmulti, 
        int maxndev, int devdescsize)
{
    int i, ndev;
    *canmulti = 1;  /* supports multiple devices */
    sprintf(indevlist, "ESD device");
    sprintf(outdevlist, "ESD device");
    *nindevs = *noutdevs = 1;
}

#endif