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

Subversion Repositories sqmusic

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

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

powered by: WebSVN 2.1.0

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