commit 3e1f3a7

zzo38  ·  2022-11-05 22:31:08 +0000 UTC
parent 5c7eb4f
Some bug fixes. Also add possibility of using -a and -n switches together (currently stdin cannot be read in this case; I will hope to fix this later)
2 files changed,  +61, -10
M main.c
+7, -3
 1@@ -31,7 +31,9 @@
 2   Use UTC instead of local time. (Has no effect in combination with -T.)
 3 
 4 -a #
 5-  Set audio. See the below section about audio for details.
 6+  Set audio. See the below section about audio for details. If both -a and
 7+  -n switches are used, then audio data is written to stdout (it is signed
 8+  16-bits big-endian).
 9 
10 -d
11   Enable some debugging features. (Some will work even without this.)
12@@ -122,14 +124,16 @@ c = Number of output channels, which can be 1 (mono) or 2 (stereo).
13 
14 e = Envelope speed. If not specified, then it is fifteenths of seconds.
15 
16-m = If set to 1, then audio is initially muted.
17+m = If set to 1, then audio is initially muted. (This option is ignored if
18+the -n switch is used.)
19 
20 n = Transpose amount.
21 
22 r = Sample rate in hertz. Normally, this setting is also the sample rate
23 for middle C.
24 
25-v = Output volume from 0 to 273.
26+v = Output volume from 0 to 273. If not all of the four VM audio channels
27+are being used, then this number may be set higher than 273.
28 
29 (Currently there is not the setting of interpolation, filter, etc.)
30 
M main.c
+54, -7
  1@@ -344,7 +344,6 @@ static void system_out(Device*dev,Uint8 id) {
  2       }
  3       break;
  4     case 8 ... 13:
  5-      if(!screen) break;
  6       colors[070].r=colors[074].r=(dev->d[8]>>4)*0x11;
  7       colors[071].r=colors[075].r=(dev->d[8]&15)*0x11;
  8       colors[072].r=colors[076].r=(dev->d[9]>>4)*0x11;
  9@@ -357,7 +356,7 @@ static void system_out(Device*dev,Uint8 id) {
 10       colors[071].b=colors[075].b=(dev->d[12]&15)*0x11;
 11       colors[072].b=colors[076].b=(dev->d[13]>>4)*0x11;
 12       colors[073].b=colors[077].b=(dev->d[13]&15)*0x11;
 13-      if(palet==7) {
 14+      if(screen && palet==7) {
 15         SDL_SetColors(screen,colors+palet*8,0,8);
 16         picture_changed=1;
 17       }
 18@@ -681,7 +680,7 @@ static void audio_out(Device*dev,Uint8 id) {
 19   UxnAudio*a;
 20   Uint32 s,n;
 21   if(id!=15) return;
 22-  SDL_LockAudio();
 23+  if(use_screen) SDL_LockAudio();
 24   a=dev->aux;
 25   a->env[0]=(dev->d[8]>>4)*audio_option['e'-'a'];
 26   a->env[1]=(dev->d[8]&15)*audio_option['e'-'a'];
 27@@ -703,7 +702,7 @@ static void audio_out(Device*dev,Uint8 id) {
 28     a->vol[0]=dev->d[14]>>4;
 29     a->vol[1]=dev->d[14]&15;
 30   }
 31-  SDL_UnlockAudio();
 32+  if(use_screen) SDL_UnlockAudio();
 33 }
 34 
 35 static Uint32 envelope(const UxnAudio*a) {
 36@@ -725,8 +724,7 @@ static void audio_callback(void*userdata,Uint8*stream,int len) {
 37   Uint32 vol=audio_option['m'-'a']?0:audio_option['v'-'a'];
 38   Sint16*buf=(void*)stream;
 39   UxnAudio*a;
 40-  int d,n;
 41-  Uint32 o0,o1,e;
 42+  Uint32 o0,o1,d,n,e;
 43   len>>=1;
 44   for(n=0;n<len;n+=c) {
 45     o0=o1=0;
 46@@ -754,6 +752,53 @@ static void audio_callback(void*userdata,Uint8*stream,int len) {
 47   }
 48 }
 49 
 50+static void run_audio(void) {
 51+  Uint8 c=audiospec.channels;
 52+  Uint32 vol=audio_option['m'-'a']?0:audio_option['v'-'a'];
 53+  Uint32 fram=(timer_rate*(long long)audiospec.freq)/1000;
 54+  Uint8 q=0;
 55+  Uint32 f=0;
 56+  UxnAudio*a;
 57+  Uint32 o0,o1,d,e;
 58+  for(;;) {
 59+    if(++f==fram) {
 60+      f=0;
 61+      run(GET16(device[2].d));
 62+    }
 63+    if(q) {
 64+      for(d=3;d<7;d++) if(q&(1<<d)) run(GET16(device[d].d));
 65+      q=0;
 66+    }
 67+    //TODO: check for input from stdin
 68+    o0=o1=0;
 69+    for(d=3;d<7;d++) {
 70+      a=device[d].aux;
 71+      if(a->pos>=a->end && !a->length) {
 72+        end:
 73+        if(a->length) q|=1<<d;
 74+        a->pos=a->length=a->end=0;
 75+        continue;
 76+      }
 77+      e=envelope(a);
 78+      if(!e) goto end;
 79+      a->envtime++;
 80+      e*=(Sint8)(mem[a->pos]^0x80);
 81+      a->phase+=a->freq;
 82+      if(a->phase>=0x1000000) {
 83+        a->pos+=a->phase>>24;
 84+        a->phase&=0xFFFFFF;
 85+        while(a->pos>=a->end && a->length) a->pos-=a->length;
 86+      }
 87+      o0+=e*a->vol[0];
 88+      o1+=e*a->vol[1];
 89+    }
 90+    d=(o0*vol)>>12; putchar(d>>8); putchar(d);
 91+    if(c==2) {
 92+      d=(o1*vol)>>12; putchar(d>>8); putchar(d);
 93+    }
 94+  }
 95+}
 96+
 97 static void redraw(void) {
 98   int a,r,u,v,x,y;
 99   Uint8*p;
100@@ -887,7 +932,7 @@ static void set_audio(char*s) {
101     if(*s<'a' && *s>'z') errx(1,"Invalid audio option");
102     audio_option[*s-'a']=strtol(s+1,&s,10);
103   }
104-  audiospec.freq=audio_option['f'-'a'];
105+  audiospec.freq=audio_option['r'-'a'];
106   audiospec.channels=audio_option['c'-'a'];
107   audiospec.samples=audio_option['b'-'a'];
108   audiospec.format=AUDIO_S16SYS;
109@@ -974,6 +1019,8 @@ int main(int argc,char**argv) {
110       // set_screen_mode(default_width,default_height);
111       goto restart;
112     }
113+  } else if(audio_option) {
114+    run_audio();
115   } else {
116     while((device[1].d[0] || device[1].d[1]) && ((i=getchar())>=0)) {
117       device[1].d[2]=i;