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

Subversion Repositories sqmusic

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

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 9 gryzor
        const char *waitline;
34
  Args( int argc, char *argv[]) :
35
                skip(0), outputfile("out.wav"), stereo(false), waitline(NULL)
36
        {
37 4 gryzor
    int k=1;
38
    bool filename_known=false;
39
    while( k < argc ) {
40
      if( strcmp(argv[k],"-l")==0 ) {
41
        k++;
42
        if( k >= argc ) throw "Expected number of lines to skip after -l";
43
        skip = atoi( argv[k] );
44
        cout << skip << " lines will be skipped\n";
45
        k++;
46
        continue;
47
      }
48
      if( strcmp(argv[k],"-o")==0 ) {
49
        k++;
50
        if( k >= argc ) throw "Expected output file name after -o";
51
        outputfile = argv[k];
52
        k++;
53
        continue;
54
      }
55 9 gryzor
      if( strcmp(argv[k],"--wait")==0 ) {
56
        k++;
57
        if( k >= argc ) throw "Expected output file name after --wait";
58
        waitline = argv[k];
59
        k++;
60
        continue;
61
      }
62 5 gryzor
      if( strcmp(argv[k],"-s")==0 ) { // stereo
63
        k++;
64
        stereo = true;
65
        continue;
66
      }
67 4 gryzor
      if( filename_known ) {
68
        cout << "Unknown parameter " << argv[k] << "\n";
69
        throw "Incorrect command line";
70
      }
71
      filename = argv[k];
72
      filename_known = true;
73
      k++;
74
    }
75 9 gryzor
    if( filename=="-" || !filename_known ) filename=string("/dev/stdin");
76 4 gryzor
  }
77
};
78
 
79 6 gryzor
bool sigint_abort=false;
80
 
81
void sigint_handle(int x ) {
82
        sigint_abort = true;
83
}
84
 
85 3 gryzor
int main(int argc, char *argv[]) {
86
        try {
87
                ifstream fin;
88 4 gryzor
                Args ar( argc, argv );
89
                cout << "Input file " << ar.filename << "\n";
90
                fin.open(ar.filename.c_str());
91
                ofstream fout(ar.outputfile.c_str());
92
                if( fin.bad() || fin.fail() ) throw "Cannot open input file";
93
                if( fout.bad() || fout.fail() ) throw "Cannot open output file";
94 3 gryzor
                assert( sizeof(short int)==2 );
95
                char buffer[1024];
96
                int data=0;
97 4 gryzor
 
98 3 gryzor
                // depending on the simulator the following "while"
99
                // section might no be needed or modified
100
                // It just skips simulator output until the real data
101 6 gryzor
                // starts to come out   
102 4 gryzor
                for( int k=0; k<ar.skip && !fin.eof(); k++ ) {
103 3 gryzor
                        fin.getline( buffer, sizeof(buffer) );
104 4 gryzor
                        //if( strcmp(buffer,"ncsim> run" )==0) break;
105 3 gryzor
                }
106 9 gryzor
                // wait for a given line in the output
107
                buffer[0]=0;
108
                if( ar.waitline )
109
                        while( !fin.eof() && strcmp( buffer, ar.waitline) )
110
                                fin.getline( buffer, sizeof(buffer) );
111
 
112
                // start conversion
113 3 gryzor
                if( fin.eof() ) throw "Data not found";
114
                fout.seekp(44);
115 6 gryzor
                signal( 2, sigint_handle ); // capture CTRL+C in order to save the
116
                // WAV header before quiting
117
                while( !fin.eof() && !fin.bad() && !fin.fail() && !sigint_abort ) {
118 3 gryzor
                        short int value;
119
                        fin.getline( buffer, sizeof(buffer) );
120 4 gryzor
 
121 3 gryzor
                        if( buffer[0]=='S' ) break; // reached line "Simulation complete"
122
                        value = atoi( buffer );
123
                        fout.write( (char*) &value, sizeof(value) );
124
                        data++;
125
                }
126
                cout << data << " samples written\n";
127
                // Write the header
128
                const char *RIFF = "RIFF";
129
                fout.seekp(0);
130
                fout.write( RIFF, 4 );
131
                int aux=36+2*data;
132
                fout.write( (char*)&aux, 4 );
133
                const char *WAVE = "WAVE";
134
                fout.write( WAVE, 4 );
135
                const char *fmt = "fmt ";
136
                fout.write( fmt, 4 );
137
                aux=16;
138
                fout.write( (char*)&aux, 4 );// suubchunk 1 size
139
                short int aux_short = 1;
140
                fout.write( (char*)&aux_short, 2 ); // audio format (1)
141 5 gryzor
                aux_short = ar.stereo ? 2 : 1;
142 3 gryzor
                fout.write( (char*)&aux_short, 2 ); // num channels (1)
143
                aux=44100;
144
                fout.write( (char*)&aux, 4 );
145 5 gryzor
                aux=44100*1*2 * (ar.stereo?2:1);
146 3 gryzor
                fout.write( (char*)&aux, 4 ); // byte rate
147 5 gryzor
                aux_short= ar.stereo ? 4 : 2;
148 3 gryzor
                fout.write( (char*)&aux_short, 2 ); // block align              
149
                aux_short=16;
150
                fout.write( (char*)&aux_short, 2 ); // bits per sample
151
                RIFF="data";
152
                fout.write( RIFF, 4 );
153
                aux = data*2;
154
                fout.write( (char*)&aux, 4 ); // data size              
155
                return 0;
156
        }
157
        catch( const char *msg ) {
158
    cout << msg << "\n";
159
    return -1;
160
  }
161
}

powered by: WebSVN 2.1.0

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