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

Subversion Repositories sqmusic

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

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
 
22
using namespace std;
23
 
24
int main(int argc, char *argv[]) {
25
        try {
26
                ifstream fin;
27
                if( argc == 2)
28
                        fin.open(argv[1]);
29
                else
30
                        fin.open("/dev/stdin");
31
                ofstream fout("out.wav");
32
                if( fin.bad() ) throw "Cannot open input file";
33
                if( fout.bad() ) throw "Cannot open output file";
34
                assert( sizeof(short int)==2 );
35
                char buffer[1024];
36
                int data=0;
37
 
38
                // depending on the simulator the following "while"
39
                // section might no be needed or modified
40
                // It just skips simulator output until the real data
41
                // starts to come out
42
                while( !fin.eof() ) {
43
                        fin.getline( buffer, sizeof(buffer) );
44
                        if( strcmp(buffer,"ncsim> run" )==0) break;
45
                }
46
 
47
                if( fin.eof() ) throw "Data not found";
48
                fout.seekp(44);
49
                while( !fin.eof() ) {
50
                        short int value;
51
                        fin.getline( buffer, sizeof(buffer) );
52
                        if( buffer[0]=='S' ) break; // reached line "Simulation complete"
53
                        value = atoi( buffer );
54
                        fout.write( (char*) &value, sizeof(value) );
55
                        data++;
56
                }
57
                cout << data << " samples written\n";
58
                // Write the header
59
                const char *RIFF = "RIFF";
60
                fout.seekp(0);
61
                fout.write( RIFF, 4 );
62
                int aux=36+2*data;
63
                fout.write( (char*)&aux, 4 );
64
                const char *WAVE = "WAVE";
65
                fout.write( WAVE, 4 );
66
                const char *fmt = "fmt ";
67
                fout.write( fmt, 4 );
68
                aux=16;
69
                fout.write( (char*)&aux, 4 );// suubchunk 1 size
70
                short int aux_short = 1;
71
                fout.write( (char*)&aux_short, 2 ); // audio format (1)
72
                fout.write( (char*)&aux_short, 2 ); // num channels (1)
73
                aux=44100;
74
                fout.write( (char*)&aux, 4 );
75
                aux=44100*1*2;
76
                fout.write( (char*)&aux, 4 ); // byte rate
77
                aux_short=2;
78
                fout.write( (char*)&aux_short, 2 ); // block align              
79
                aux_short=16;
80
                fout.write( (char*)&aux_short, 2 ); // bits per sample
81
                RIFF="data";
82
                fout.write( RIFF, 4 );
83
                aux = data*2;
84
                fout.write( (char*)&aux, 4 ); // data size              
85
                return 0;
86
        }
87
        catch( const char *msg ) {
88
    cout << msg << "\n";
89
    return -1;
90
  }
91
}

powered by: WebSVN 2.1.0

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