OpenCores
URL https://opencores.org/ocsvn/sqmusic/sqmusic/trunk

Subversion Repositories sqmusic

[/] [sqmusic/] [trunk/] [cpp/] [sintable.cc] - Blame information for rev 18

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 gryzor
/*
2
        sintable: sine wave table generator
3
        Based on MAME's fm.c file.
4
 
5
  (c) Jose Tejada Gomez, May 2013
6
  You can use this file following the GNU GENERAL PUBLIC LICENSE version 3
7
  Read the details of the license in:
8
  http://www.gnu.org/licenses/gpl.txt
9
 
10
  Send comments to: jose.tejada at ieee.org
11
 
12
*/
13
 
14 12 gryzor
#include <iostream>
15
#include <cmath>
16 18 gryzor
#include "../cpp/args.h"
17 12 gryzor
 
18
using namespace std;
19
 
20 15 gryzor
#define ENV_BITS        10
21
#define ENV_LEN         (1<<ENV_BITS)
22
#define ENV_STEP        (128.0/ENV_LEN)
23
#define TL_RES_LEN      (256) /* 8 bits addressing (real chip) */
24
#define SIN_BITS        10
25
#define SIN_LEN         (1<<SIN_BITS)
26
#define SIN_MASK        (SIN_LEN-1)
27
 
28 18 gryzor
signed int tl_tab[TL_RES_LEN];
29 15 gryzor
unsigned int sin_tab[SIN_LEN];
30
 
31
void init_tables(void) // copied from fm.c
32
{
33
        signed int i,x;
34
        signed int n;
35
        double o,m;
36
 
37
        for (x=0; x<TL_RES_LEN; x++)
38
        {
39
                m = (1<<16) / pow(2, (x+1) * (ENV_STEP/4.0) / 8.0);
40
                m = floor(m);
41
 
42
                /* we never reach (1<<16) here due to the (x+1) */
43
                /* result fits within 16 bits at maximum */
44
 
45
                n = (int)m;     /* 16 bits here */
46
                n >>= 4;        /* 12 bits here */
47
                if (n&1)        /* round to nearest */
48
                        n = (n>>1)+1;
49
                else
50
                        n = n>>1;
51
                                                /* 11 bits here (rounded) */
52
                n <<= 2;        /* 13 bits here (as in real chip) */
53 18 gryzor
                tl_tab[ x ] = n;
54 15 gryzor
        }
55
 
56
        for (i=0; i<SIN_LEN; i++)
57
        {
58
                /* non-standard sinus */
59
                m = sin( ((i*2)+1) * M_PI / SIN_LEN ); /* checked against the real chip */
60
 
61
                /* we never reach zero here due to ((i*2)+1) */
62
                if (m>0.0)
63
                        o = 8*log(1.0/m)/log(2.0);  /* convert to 'decibels' */
64
                else
65
                        o = 8*log(-1.0/m)/log(2.0); /* convert to 'decibels' */
66
 
67
                o = o / (ENV_STEP/4);
68
 
69
                n = (int)(2.0*o);
70
                if (n&1)                        /* round to nearest */
71
                        n = (n>>1)+1;
72
                else
73
                        n = n>>1;
74
 
75
                sin_tab[ i ] = n*2 + (m>=0.0? 0: 1 );
76
                /*logerror("FM.C: sin [%4i]= %4i (tl_tab value=%5i)\n", i, sin_tab[i],tl_tab[sin_tab[i]]);*/
77
        }
78
}
79
 
80
void dump_tl_tab() {
81 18 gryzor
  for( int i=0; i<TL_RES_LEN; i++ ) {
82
    cout <<  tl_tab[i] << "\n";
83 15 gryzor
  }
84
}
85
 
86
void dump_sin_tab() {
87
  for( int i=0; i<SIN_LEN; i++ ) {
88 18 gryzor
    cout << sin_tab[i] << "\n";
89 15 gryzor
  }
90
}
91
 
92
void dump_composite() {
93
        // cout.setf( ios::hex, ios::basefield );
94
  for( int i=0; i<SIN_LEN; i++ ) {
95
    cout << sin_tab[i] << "," << tl_tab[ sin_tab[i] ] << "\n";
96
  }
97
}
98
 
99 14 gryzor
unsigned conv( double x ) {
100
  double xmax = 0xFFFFF; // 20 bits, all ones
101
  return (unsigned)(xmax* 20*log(x+0.5));
102
}
103
 
104 18 gryzor
int main(int argc, char *argv[]) {
105
  arg_vector_t legal_args;
106
  argument_t arg_hex( legal_args, "hex", argument_t::flag,
107
    "set output to hexadecimal mode" );
108
  argument_t arg_sin( legal_args, "sin", argument_t::flag,
109
    "dump sine wave" );
110
  argument_t arg_pow( legal_args, "pow", argument_t::flag,
111
    "dump power table" );
112
  Args args_parser( argc, argv, legal_args );
113
  if( args_parser.help_request() ) { return 0; }
114
  if( argc==1 ) { args_parser.show_help(); return 0; }
115 15 gryzor
  init_tables();
116 18 gryzor
        //dump_composite();
117
        if( arg_hex.is_set() ) cout.setf( ios::hex, ios::basefield );
118
        if( arg_pow.is_set() ) dump_tl_tab();
119
        if( arg_sin.is_set() ) dump_sin_tab();
120 12 gryzor
        return 0;
121
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.