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

Subversion Repositories sqmusic

[/] [sqmusic/] [trunk/] [1942/] [log2wav.cc] - Blame information for rev 7

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

Line No. Rev Author Line
1 3 gryzor
/*
2
        Converts output from 1942.v to .wav file
3
 
4
  (c) Jose Tejada Gomez, 9th May 2013
5
  You can use this file following the GNU GENERAL PUBLIC LICENSE version 3
6
  Read the details of the license in:
7
  http://www.gnu.org/licenses/gpl.txt
8
 
9
  Send comments to: jose.tejada@ieee.org
10
 
11
*/
12
 
13
// Compile with  g++ log2wav.cc -o log2wav
14
 
15
#include <iostream>
16
#include <fstream>
17
#include <vector>
18
#include <string.h>
19
#include <stdlib.h>
20
#include <assert.h>
21 4 gryzor
#include <string.h>
22
#include <string>
23 6 gryzor
#include <signal.h>
24 3 gryzor
 
25
using namespace std;
26
 
27 4 gryzor
class Args {
28
public:
29
  int skip;
30
  string filename;
31
  string outputfile;
32 5 gryzor
  bool stereo;
33
  Args( int argc, char *argv[]) : skip(0), outputfile("out.wav"), stereo(false) {
34 4 gryzor
    int k=1;
35
    bool filename_known=false;
36
    while( k < argc ) {
37
      if( strcmp(argv[k],"-l")==0 ) {
38
        k++;
39
        if( k >= argc ) throw "Expected number of lines to skip after -l";
40
        skip = atoi( argv[k] );
41
        cout << skip << " lines will be skipped\n";
42
        k++;
43
        continue;
44
      }
45
      if( strcmp(argv[k],"-o")==0 ) {
46
        k++;
47
        if( k >= argc ) throw "Expected output file name after -o";
48
        outputfile = argv[k];
49
        k++;
50
        continue;
51
      }
52 5 gryzor
      if( strcmp(argv[k],"-s")==0 ) { // stereo
53
        k++;
54
        stereo = true;
55
        continue;
56
      }
57 4 gryzor
      if( filename_known ) {
58
        cout << "Unknown parameter " << argv[k] << "\n";
59
        throw "Incorrect command line";
60
      }
61
      filename = argv[k];
62
      filename_known = true;
63
      k++;
64
    }
65
    if( filename=="-" ) filename=string("/dev/stdin");
66
  }
67
};
68
 
69 6 gryzor
bool sigint_abort=false;
70
 
71
void sigint_handle(int x ) {
72
        sigint_abort = true;
73
}
74
 
75 3 gryzor
int main(int argc, char *argv[]) {
76
        try {
77
                ifstream fin;
78 4 gryzor
                Args ar( argc, argv );
79
                cout << "Input file " << ar.filename << "\n";
80
                fin.open(ar.filename.c_str());
81
                ofstream fout(ar.outputfile.c_str());
82
                if( fin.bad() || fin.fail() ) throw "Cannot open input file";
83
                if( fout.bad() || fout.fail() ) throw "Cannot open output file";
84 3 gryzor
                assert( sizeof(short int)==2 );
85
                char buffer[1024];
86
                int data=0;
87 4 gryzor
 
88 3 gryzor
                // depending on the simulator the following "while"
89
                // section might no be needed or modified
90
                // It just skips simulator output until the real data
91 6 gryzor
                // starts to come out   
92 4 gryzor
                for( int k=0; k<ar.skip && !fin.eof(); k++ ) {
93 3 gryzor
                        fin.getline( buffer, sizeof(buffer) );
94 4 gryzor
                        //if( strcmp(buffer,"ncsim> run" )==0) break;
95 3 gryzor
                }
96
                if( fin.eof() ) throw "Data not found";
97
                fout.seekp(44);
98 6 gryzor
                signal( 2, sigint_handle ); // capture CTRL+C in order to save the
99
                // WAV header before quiting
100
                while( !fin.eof() && !fin.bad() && !fin.fail() && !sigint_abort ) {
101 3 gryzor
                        short int value;
102
                        fin.getline( buffer, sizeof(buffer) );
103 4 gryzor
 
104 3 gryzor
                        if( buffer[0]=='S' ) break; // reached line "Simulation complete"
105
                        value = atoi( buffer );
106
                        fout.write( (char*) &value, sizeof(value) );
107
                        data++;
108
                }
109
                cout << data << " samples written\n";
110
                // Write the header
111
                const char *RIFF = "RIFF";
112
                fout.seekp(0);
113
                fout.write( RIFF, 4 );
114
                int aux=36+2*data;
115
                fout.write( (char*)&aux, 4 );
116
                const char *WAVE = "WAVE";
117
                fout.write( WAVE, 4 );
118
                const char *fmt = "fmt ";
119
                fout.write( fmt, 4 );
120
                aux=16;
121
                fout.write( (char*)&aux, 4 );// suubchunk 1 size
122
                short int aux_short = 1;
123
                fout.write( (char*)&aux_short, 2 ); // audio format (1)
124 5 gryzor
                aux_short = ar.stereo ? 2 : 1;
125 3 gryzor
                fout.write( (char*)&aux_short, 2 ); // num channels (1)
126
                aux=44100;
127
                fout.write( (char*)&aux, 4 );
128 5 gryzor
                aux=44100*1*2 * (ar.stereo?2:1);
129 3 gryzor
                fout.write( (char*)&aux, 4 ); // byte rate
130 5 gryzor
                aux_short= ar.stereo ? 4 : 2;
131 3 gryzor
                fout.write( (char*)&aux_short, 2 ); // block align              
132
                aux_short=16;
133
                fout.write( (char*)&aux_short, 2 ); // bits per sample
134
                RIFF="data";
135
                fout.write( RIFF, 4 );
136
                aux = data*2;
137
                fout.write( (char*)&aux, 4 ); // data size              
138
                return 0;
139
        }
140
        catch( const char *msg ) {
141
    cout << msg << "\n";
142
    return -1;
143
  }
144
}

powered by: WebSVN 2.1.0

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