File: split.c

package info (click to toggle)
xcfa 5.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,300 kB
  • sloc: ansic: 47,148; sh: 4,380; makefile: 136; sed: 16
file content (143 lines) | stat: -rw-r--r-- 5,257 bytes parent folder | download | duplicates (5)
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
/*
 *  file      : src/split.c
 *  project   : xcfa_cli
 *  copyright : (C) 2014 by BULIN Claude
 *
 *  This file is part of xcfa_cli project
 *
 *  xcfa_cli is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; version 3.
 *
 *  xcfa_cli is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA
 */



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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "global.h"
#include "tags.h"
#include "conv.h"


//
// Split file 'char *p_pathnamefile' from 'double p_split_begin_ss' for a length of 'int p_split_length_ss'
//
void split_wav( char *p_pathnamefile, double p_split_begin_ss, int p_split_length_ss )
{
	WAVE	WaveHeader;
	double	Percent = 0.;
	double	PercentBegin;
	double	PercentEnd;
	char	*NewName = NULL;
	char	*buffer = NULL;
	FILE	*fp = NULL;
	size_t	Ret;
	size_t	RetRead;
	size_t	WriteValue;

	if (FALSE == tagswav_read_file( p_pathnamefile, &WaveHeader )) {
		tagswav_close_file( &WaveHeader );
		printf ( "%s", BOLD_RED );
		printf("\n!--- S P L I T ----------------\n");
		printf ( "! File not found to split operation: \"%s\"", p_pathnamefile );
		printf ( "%s\n\n", RESET );
		return;
	}
	if( (p_split_begin_ss + p_split_length_ss) > (WaveHeader.DATA.Subchunk2Size / WaveHeader.FMT.ByteRate) ) {
		printf ( "%s", BOLD_RED );
		printf("\n!--- S P L I T ----------------\n");
		printf ( "! The size of the '--split' and '--length' is greater than the size of the file." );
		printf ( "! %s\n", p_pathnamefile );
		printf ( "! %d secondes\n", WaveHeader.DATA.Subchunk2Size / WaveHeader.FMT.ByteRate );
		printf ( "%s\n\n", RESET );
		tagswav_close_file( &WaveHeader );
		return;
	}

	PercentBegin = (((double)p_split_begin_ss * (double)WaveHeader.FMT.ByteRate) /  WaveHeader.DATA.Subchunk2Size) * 100.0;
	PercentEnd = (((double)p_split_length_ss * (double)WaveHeader.FMT.ByteRate) /  WaveHeader.DATA.Subchunk2Size) * 100.0;
	PercentEnd += PercentBegin;

	NewName = C_strdup_printf( "%s.split.wav", p_pathnamefile );

	printf("\n!--- S P L I T ---------------------------------------------!\n");
	printf( "! %s\n", p_pathnamefile );
	printf( "! %s\n", NewName );

	buffer = (char *)malloc ((sizeof(char) * BUFSIZ) + 10);

	fp = fopen( NewName, "w" );
	// ECRITURE ENTETE FICHIER WAV
	fwrite( &WaveHeader.RIFF.ChunkID, 4, 1, fp );		// RIFF
	fwrite( &WaveHeader.RIFF.ChunkSize, 4, 1, fp );		// taille du fichier entier en octets (sans compter les 8 octets de ce champ et le champ précédent
	fwrite( &WaveHeader.RIFF.Format, 4, 1, fp );		// WAVE
	fwrite( &WaveHeader.FMT.Subchunk1ID, 4, 1, fp );	// 'fmt '
	fwrite( &WaveHeader.FMT.Subchunk1Size, 4, 1, fp );	// taille en octet des données à suivre
	fwrite( &WaveHeader.FMT.AudioFormat, 2, 1, fp );	// format de compression (une valeur autre que 1 indique une compression)
	fwrite( &WaveHeader.FMT.NumChannels, 2, 1, fp );	// nombre de canaux
	fwrite( &WaveHeader.FMT.SampleRate, 4, 1, fp );		// fréquence d'échantillonage (nombre d'échantillons par secondes)
	fwrite( &WaveHeader.FMT.ByteRate, 4, 1, fp );		// nombre d'octects par secondes
	fwrite( &WaveHeader.FMT.Blockalign, 2, 1, fp );		// nombre d'octects pour coder un échantillon
	fwrite( &WaveHeader.FMT.BitsPerSample, 2, 1, fp );	// nombre de bits pour coder un échantillon
	fwrite( &WaveHeader.DATA.Subchunk2ID, 4, 1, fp );	// 'data'
	fwrite( &WaveHeader.DATA.Subchunk2Size, 4, 1, fp );	//  taille des données audio( nombre total d'octets codant les données audio)

	RetRead = 0;
	WriteValue = 0;
	while(( Ret = fread (buffer,  1, BUFSIZ, WaveHeader.file )) > 0) {
		RetRead += Ret;
		Percent = (float)(RetRead / (float)WaveHeader.DATA.Subchunk2Size) * 100.0;
		if( Percent >= PercentBegin ) {
			fwrite( buffer, Ret, 1, fp );
			WriteValue += Ret;
	 	}
		if( Percent > PercentEnd ) break;
	}
	/*
	fseek( fp, 4L, SEEK_SET );
	WriteValue += 44;
	WriteValue -= 8;
	fwrite( &WriteValue, 4, 1, fp );	// taille du fichier entier en octets (sans compter les 8 octets de ce champ et le champ précédent
	fseek( fp, 40L, SEEK_SET );
	WriteValue += 8;
	WriteValue -= 44;
	fwrite( &WriteValue, 4, 1, fp );	//  taille des données audio( nombre total d'octets codant les données audio )
	*/
	fseek( fp, 40L, SEEK_SET );
	fwrite( &WriteValue, 4, 1, fp );	//  taille des données audio( nombre total d'octets codant les données audio )
	fseek( fp, 4L, SEEK_SET );
	WriteValue += 44;
	WriteValue -= 8;
	fwrite( &WriteValue, 4, 1, fp );	// taille du fichier entier en octets (sans compter les 8 octets de ce champ et le champ précédent

	tagswav_close_file( &WaveHeader );
	fclose (fp );
	fp = NULL;

	conv_copy_src_to_dest( NewName, p_pathnamefile );

	remove( NewName );

	free( NewName );
	NewName = NULL;
	free( buffer );
	buffer = NULL;
}