uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

wavpcm.c (1101B)


      1 #include <stdio.h>
      2 #include <math.h>
      3 
      4 /* 
      5 Copyright (c) 2020 Devine Lu Linvega
      6 
      7 Permission to use, copy, modify, and distribute this software for any
      8 purpose with or without fee is hereby granted, provided that the above
      9 copyright notice and this permission notice appear in all copies.
     10 
     11 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12 WITH REGARD TO THIS SOFTWARE.
     13 */
     14 
     15 #define PI 3.14159265358979323846
     16 #define SAMPLES 256
     17 #define RATE 1
     18 
     19 typedef unsigned char Uint8;
     20 
     21 int
     22 clamp(int val, int min, int max)
     23 {
     24 	return (val >= min) ? (val <= max) ? val : max : min;
     25 }
     26 
     27 Uint8
     28 sinw(int i)
     29 {
     30 	return 0x7f * sin(i * RATE * 2 * PI / SAMPLES);
     31 }
     32 
     33 Uint8
     34 triw(int i)
     35 {
     36 	if(i < 0x40)
     37 		return i * 2;
     38 	if(i >= 0xc0)
     39 		return (i - 0xc0) * 2 - 0x7f;
     40 	return 0x7f - (i - 0x40) * 2;
     41 }
     42 
     43 Uint8
     44 sqrw(int i)
     45 {
     46 	return ((i * RATE) % 0xff) < 0x80 ? 0x7f : 0x80;
     47 }
     48 
     49 int
     50 main()
     51 {
     52 	int i;
     53 	printf("%d:\n\n", SAMPLES);
     54 	for(i = 0; i < SAMPLES; ++i) {
     55 		if(i % 0x10 == 0)
     56 			printf("\n");
     57 		else if(i % 2 == 0)
     58 			printf(" ");
     59 		printf("%02x", (triw(i) + sinw(i)) / 2);
     60 	}
     61 	printf("\n\n");
     62 	return 0;
     63 }