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

Subversion Repositories sqmusic

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

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 3 gryzor
 
24
using namespace std;
25
 
26 4 gryzor
class Args {
27
public:
28
  int skip;
29
  string filename;
30
  string outputfile;
31
  Args( int argc, char *argv[]) : skip(0), outputfile("out.wav") {
32
    int k=1;
33
    bool filename_known=false;
34
    while( k < argc ) {
35
      if( strcmp(argv[k],"-l")==0 ) {
36
        k++;
37
        if( k >= argc ) throw "Expected number of lines to skip after -l";
38
        skip = atoi( argv[k] );
39
        cout << skip << " lines will be skipped\n";
40
        k++;
41
        continue;
42
      }
43
      if( strcmp(argv[k],"-o")==0 ) {
44
        k++;
45
        if( k >= argc ) throw "Expected output file name after -o";
46
        outputfile = argv[k];
47
        k++;
48
        continue;
49
      }
50
      if( filename_known ) {
51
        cout << "Unknown parameter " << argv[k] << "\n";
52
        throw "Incorrect command line";
53
      }
54
      filename = argv[k];
55
      filename_known = true;
56
      k++;
57
    }
58
    if( filename=="-" ) filename=string("/dev/stdin");
59
  }
60
};
61
 
62 3 gryzor
int main(int argc, char *argv[]) {
63
        try {
64
                ifstream fin;
65 4 gryzor
                Args ar( argc, argv );
66
                cout << "Input file " << ar.filename << "\n";
67
                fin.open(ar.filename.c_str());
68
                ofstream fout(ar.outputfile.c_str());
69
                if( fin.bad() || fin.fail() ) throw "Cannot open input file";
70
                if( fout.bad() || fout.fail() ) throw "Cannot open output file";
71 3 gryzor
                assert( sizeof(short int)==2 );
72
                char buffer[1024];
73
                int data=0;
74 4 gryzor
 
75 3 gryzor
                // depending on the simulator the following "while"
76
                // section might no be needed or modified
77
                // It just skips simulator output until the real data
78 4 gryzor
                // starts to come out           
79
                for( int k=0; k<ar.skip && !fin.eof(); k++ ) {
80 3 gryzor
                        fin.getline( buffer, sizeof(buffer) );
81 4 gryzor
                        //if( strcmp(buffer,"ncsim> run" )==0) break;
82 3 gryzor
                }
83
                if( fin.eof() ) throw "Data not found";
84
                fout.seekp(44);
85 4 gryzor
                while( !fin.eof() && !fin.bad() && !fin.fail() ) {
86 3 gryzor
                        short int value;
87
                        fin.getline( buffer, sizeof(buffer) );
88 4 gryzor
 
89 3 gryzor
                        if( buffer[0]=='S' ) break; // reached line "Simulation complete"
90
                        value = atoi( buffer );
91
                        fout.write( (char*) &value, sizeof(value) );
92
                        data++;
93
                }
94
                cout << data << " samples written\n";
95
                // Write the header
96
                const char *RIFF = "RIFF";
97
                fout.seekp(0);
98
                fout.write( RIFF, 4 );
99
                int aux=36+2*data;
100
                fout.write( (char*)&aux, 4 );
101
                const char *WAVE = "WAVE";
102
                fout.write( WAVE, 4 );
103
                const char *fmt = "fmt ";
104
                fout.write( fmt, 4 );
105
                aux=16;
106
                fout.write( (char*)&aux, 4 );// suubchunk 1 size
107
                short int aux_short = 1;
108
                fout.write( (char*)&aux_short, 2 ); // audio format (1)
109
                fout.write( (char*)&aux_short, 2 ); // num channels (1)
110
                aux=44100;
111
                fout.write( (char*)&aux, 4 );
112
                aux=44100*1*2;
113
                fout.write( (char*)&aux, 4 ); // byte rate
114
                aux_short=2;
115
                fout.write( (char*)&aux_short, 2 ); // block align              
116
                aux_short=16;
117
                fout.write( (char*)&aux_short, 2 ); // bits per sample
118
                RIFF="data";
119
                fout.write( RIFF, 4 );
120
                aux = data*2;
121
                fout.write( (char*)&aux, 4 ); // data size              
122
                return 0;
123
        }
124
        catch( const char *msg ) {
125
    cout << msg << "\n";
126
    return -1;
127
  }
128
}

powered by: WebSVN 2.1.0

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