File: rawtofile.cc

package info (click to toggle)
mp3blaster 1:3.2.5-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 2,156 kB
  • ctags: 3,421
  • sloc: cpp: 18,610; ansic: 1,452; sh: 701; makefile: 91
file content (172 lines) | stat: -rw-r--r-- 3,952 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* MPEG/WAVE Sound library

	 (C) 1997 by Jung woo-jae */

// Rawtofile.cc
// Output raw data to file.

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <fcntl.h>
#include <unistd.h>

#include "mpegsound.h"
#include "mpegsound_locals.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif
#ifdef WORDS_BIGENDIAN
typedef union {
		long arg;
		char byte_represent[4];
} endian_hack_1;

typedef union {
		short arg;
		char byte_represent[2];
} endian_hack_2;

inline short HOST_TO_LE16(short x)
{
		endian_hack_2 in,out;
		in.arg=x;
		out.arg=0;
		out.byte_represent[0]=in.byte_represent[1];
		out.byte_represent[1]=in.byte_represent[0];
		return (short)out.arg;
}
inline int HOST_TO_LE32(int x)
{
		endian_hack_1 in,out;
		in.arg=x;
		out.arg=0;
		out.byte_represent[0]=in.byte_represent[3];
		out.byte_represent[1]=in.byte_represent[2];
		out.byte_represent[2]=in.byte_represent[1];
		out.byte_represent[3]=in.byte_represent[0];
		return out.arg;
}
#else
#define HOST_TO_LE16(x)  (x)
#define HOST_TO_LE32(x)  (x)
#endif
// Rawplayer class
Rawtofile::~Rawtofile()
{
	if (filetype == ST_WAV)
	{
		off_t filelen = lseek(audiohandle, 0, SEEK_CUR);
		lseek(audiohandle, 0, SEEK_SET);
		hdr.length = HOST_TO_LE32((u_int32_t)(filelen-8)); /* file length */
		hdr.data_length = HOST_TO_LE32((u_int32_t)(filelen - 44));
		(void)write(audiohandle, &hdr, sizeof(hdr));
	}
	close(audiohandle);
}

Rawtofile::Rawtofile(int audiohandle)
{
	this->audiohandle = audiohandle;
	init_putblock = 1;
}

Rawtofile *Rawtofile::opendevice(const char *filename)
{
	int audiohandle;
	if(filename==NULL)audiohandle=1;
	else if((audiohandle=creat(filename,0644))==-1)
		return NULL;
	if (isatty(audiohandle)) return NULL;

	return new Rawtofile(audiohandle);
}

bool Rawtofile::setsoundtype(int stereo,int samplesize,int speed)
{
	static bool myinit = false;
	/* changing sample specs when writing to a file is not done! */
	if (myinit && (
		(rawstereo != stereo) ||
		(rawsamplesize != samplesize) ||
		(rawspeed != speed)))
	{
		debug("Change in sample size/speed/mode.\n");
		return false;
	}
	else
		myinit = true;

	rawstereo=stereo;
	rawsamplesize=samplesize;
	rawspeed=speed;

	return true;
}

/* set type of file to write. Default: ST_RAW (no header) */
bool Rawtofile::setfiletype(soundtype filetype)
{
	if (filetype != ST_RAW && filetype != ST_WAV)
		return false;

	this->filetype = filetype;	
	return true;
}
int Rawtofile::putblock_nt(void *buffer, int size)
{
	if (init_putblock && filetype != ST_RAW)
	{
		int wordsize;
	
		wordsize = rawsamplesize;

		if (filetype == ST_WAV)
		{
		//initial datasize = 0...when all data is written, determine filesize
		//and rewrite the header.
			hdr.main_chunk = RIFF;
			hdr.length = HOST_TO_LE32(0 + 36); /* file length */
			hdr.chunk_type = WAVE;
			hdr.sub_chunk = FMT;
			hdr.sc_len = HOST_TO_LE32(wordsize); /* = 16 */
			hdr.format = PCM_CODE;
			hdr.modus = (rawstereo ? WAVE_STEREO : WAVE_MONO); /* stereo, 1 = mono */
			hdr.sample_fq = HOST_TO_LE32(rawspeed); /* sample frequency */
			hdr.byte_p_sec = HOST_TO_LE32((rawspeed * (wordsize/8) * (rawstereo?2:1)));
			hdr.byte_p_spl = HOST_TO_LE16((rawstereo?2:1) * (wordsize/8));
			hdr.bit_p_spl = HOST_TO_LE16(wordsize); /* 8, 12, or 16 bit */
			hdr.data_chunk = DATA;
			hdr.data_length = 0; /* file length without this header */
			if (write(audiohandle, &hdr, sizeof(hdr)) != sizeof(hdr))
				return false;
		}
	}
	init_putblock = 0;
#ifdef WORDS_BIGENDIAN
	if (rawsamplesize == 16)
	{
		/* big endian -> switch bytes in output file */
		unsigned short *sh_buffer=(unsigned short *)(buffer);
		int modifiedsize=size/2;
		/* data is in 16 bits bigendian, target is 16 bits little endian. */
		while(modifiedsize--)
		{
			sh_buffer[modifiedsize]=HOST_TO_LE16(sh_buffer[modifiedsize]);
		}
	}
#endif
	return write(audiohandle,buffer,size);
}

bool Rawtofile::putblock(void *buffer,int size)
{
	return putblock_nt(buffer,size)==size;
};