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

Subversion Repositories sqmusic

[/] [sqmusic/] [trunk/] [1942/] [pwm2log.cc] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 gryzor
/*
2
        Converts PWM output from 1942.v to .log file
3
        the output can be piped into log2wav in order to get the wav file
4
 
5
  (c) Jose Tejada Gomez, 9th 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@ieee.org
11
 
12
*/
13
 
14
// Compile with  g++ pwm2wav.cc -o pwm2wav
15
 
16
#include <iostream>
17
#include <string.h>
18
#include <stdlib.h>
19
 
20
using namespace std;
21
 
22
struct ym_output {
23
  double a,b,c;
24
};
25
 
26
void parse( char *buf, int len, double pwm[6] ) {
27
  int k=0;
28
  buf=strtok( buf, ",");
29
  for( k=0; k<6; k++ ) {
30
    if( buf==NULL ) throw "incomplete line (1)";
31
    pwm[k] = (double)atoi( buf );
32
    if( pwm[k]!=0 && pwm[k]!=1 ) pwm[k]=0;
33
    buf=strtok( NULL, " ");
34
  }
35
}
36
 
37
void calc_voltage( double& vcap, double pwm[3] ) {
38
  const double res = 7.23;
39
  const double cap = 4e-6; // if RC=7.23 us => f0=22kHz 
40
  double dv=0;
41
  const double dt = 20e-9;
42
  for(int k=0; k<3; k++ )
43
    dv += pwm[k] - vcap;
44
  vcap += dv*dt/res/cap;
45
}
46
 
47
int main( int argc, char *argv[] ) {
48
  double vcap_left=0, vcap_right=0;
49
  char buf[1024];
50
  int skip = 1; // lines to skip
51
  for(int k=0; k<skip; k++ )
52
    cin.getline( buf, sizeof(buf) );
53
  // conversion
54
  try {
55
    long int sample=0;
56
    // skip first line
57 6 gryzor
                buf[0]=0;
58
                while( strncmp("1942 START",buf, 11 ) && !cin.eof() && !cin.fail() )
59
        cin.getline( buf, sizeof(buf) );
60 5 gryzor
 
61
    cin.getline( buf, sizeof(buf) );
62
    while( buf[0] && !cin.eof() ) {
63
      double pwm[6];
64
      parse( buf, sizeof(buf), pwm );
65
      calc_voltage( vcap_left, pwm );
66
      calc_voltage( vcap_right, &pwm[3] );
67
      if( sample == 1134 ) {
68
        cout << (int)(vcap_left*65535) << "\n";
69
        cout << (int)(vcap_right*65535) << "\n";
70
        sample = 0;
71
      } else sample++;
72
      cin.getline( buf, sizeof(buf) );
73
    }
74
    return 0;
75
  }
76
  catch( const char *x ) {
77
    cout << "ERROR: " << x << "\n";
78
    return -1;
79
  }
80
}

powered by: WebSVN 2.1.0

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