Ejemplo n.º 1
0
static status WriteFrameCount (AFfilehandle file)
{
	_Track		*track = NULL;
	_WAVEInfo	*waveinfo = NULL;
	u_int32_t	factSize = 4;
	u_int32_t	totalFrameCount;

	assert(file != NULL);

	track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK);
	waveinfo = (_WAVEInfo *) file->formatSpecific;

	/* We only write the fact chunk for compressed audio. */
	if (track->f.compressionType == AF_COMPRESSION_NONE)
		return AF_SUCCEED;

	/*
		If the offset for the fact chunk hasn't been set yet,
		set it to the file's current position.
	*/
	if (waveinfo->factOffset == 0)
		waveinfo->factOffset = af_ftell(file->fh);
	else
		af_fseek(file->fh, waveinfo->factOffset, SEEK_SET);

	af_fwrite("fact", 4, 1, file->fh);
	factSize = HOST_TO_LENDIAN_INT32(factSize);
	af_fwrite(&factSize, 4, 1, file->fh);

	totalFrameCount = HOST_TO_LENDIAN_INT32(track->totalfframes);
	af_fwrite(&totalFrameCount, 4, 1, file->fh);

	return AF_SUCCEED;
}
Ejemplo n.º 2
0
status _af_wave_update (AFfilehandle file)
{
	_Track	*track;

	track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK);

	if (track->fpos_first_frame != 0)
	{
		size_t	dataLength, fileLength;

		/*
			We call _af_format_frame_size to calculate the
			frame size of normal PCM data or compressed data.
		*/
		dataLength = track->totalfframes * _af_format_frame_size(&track->f, AF_FALSE);

		dataLength = HOST_TO_LENDIAN_INT32(dataLength);
		af_fseek(file->fh, track->fpos_first_frame - 4, SEEK_SET);
		af_fwrite(&dataLength, 4, 1, file->fh);

		fileLength = af_flength(file->fh);
		fileLength -= 8;
		fileLength = HOST_TO_LENDIAN_INT32(fileLength);

		af_fseek(file->fh, 4, SEEK_SET);
		af_fwrite(&fileLength, 4, 1, file->fh);
	}

	return AF_SUCCEED;
}
Ejemplo n.º 3
0
static status next_write_header (AFfilehandle file)
{
	_Track		*track;
	int		frameSize;
	uint32_t	offset, length, encoding, sampleRate, channelCount;

	track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK);

	frameSize = _af_format_frame_size(&track->f, false);

	offset = track->fpos_first_frame;
	length = track->totalfframes * frameSize;
	encoding = nextencodingtype(&track->f);
	sampleRate = track->f.sampleRate;
	channelCount = track->f.channelCount;

	if (af_fseek(file->fh, 0, SEEK_SET) != 0)
		_af_error(AF_BAD_LSEEK, "bad seek");

	af_fwrite(".snd", 4, 1, file->fh);
	af_write_uint32_be(&offset, file->fh);
	af_write_uint32_be(&length, file->fh);
	af_write_uint32_be(&encoding, file->fh);
	af_write_uint32_be(&sampleRate, file->fh);
	af_write_uint32_be(&channelCount, file->fh);

	return AF_SUCCEED;
}
Ejemplo n.º 4
0
static status WriteFrameCount (AFfilehandle file)
{
	_Track		*track = NULL;
	_WAVEInfo	*waveinfo = NULL;
	uint32_t	factSize = 4;
	uint32_t	totalFrameCount;

	assert(file != NULL);

	track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK);
	waveinfo = (_WAVEInfo *) file->formatSpecific;

	/* Omit the fact chunk only for uncompressed integer audio formats. */
	if (track->f.compressionType == AF_COMPRESSION_NONE &&
		(track->f.sampleFormat == AF_SAMPFMT_TWOSCOMP ||
		track->f.sampleFormat == AF_SAMPFMT_UNSIGNED))
		return AF_SUCCEED;

	/*
		If the offset for the fact chunk hasn't been set yet,
		set it to the file's current position.
	*/
	if (waveinfo->factOffset == 0)
		waveinfo->factOffset = af_ftell(file->fh);
	else
		af_fseek(file->fh, waveinfo->factOffset, SEEK_SET);

	af_fwrite("fact", 4, 1, file->fh);
	af_write_uint32_le(&factSize, file->fh);

	totalFrameCount = track->totalfframes;
	af_write_uint32_le(&totalFrameCount, file->fh);

	return AF_SUCCEED;
}
Ejemplo n.º 5
0
status _af_wave_update (AFfilehandle file)
{
	_Track		*track;
	_WAVEInfo	*wave = (_WAVEInfo *) file->formatSpecific;

	track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK);

	if (track->fpos_first_frame != 0)
	{
		u_int32_t	dataLength, fileLength;

		/* Update the frame count chunk if present. */
		WriteFrameCount(file);

		/* Update the length of the data chunk. */
		af_fseek(file->fh, wave->dataSizeOffset, SEEK_SET);

		/*
			We call _af_format_frame_size to calculate the
			frame size of normal PCM data or compressed data.
		*/
		dataLength = (u_int32_t) track->totalfframes *
			_af_format_frame_size(&track->f, AF_FALSE);
		dataLength = HOST_TO_LENDIAN_INT32(dataLength);
		af_fwrite(&dataLength, 4, 1, file->fh);

		/* Update the length of the RIFF chunk. */
		fileLength = (u_int32_t) af_flength(file->fh);
		fileLength -= 8;
		fileLength = HOST_TO_LENDIAN_INT32(fileLength);

		af_fseek(file->fh, 4, SEEK_SET);
		af_fwrite(&fileLength, 4, 1, file->fh);
	}

	/*
		Write the actual data that was set after initializing
		the miscellaneous IDs.	The size of the data will be
		unchanged.
	*/
	WriteMiscellaneous(file);

	/* Write the new positions; the size of the data will be unchanged. */
	WriteCues(file);

	return AF_SUCCEED;
}
Ejemplo n.º 6
0
status af_write_uint16_le (const uint16_t *value, AFvirtualfile *vf)
{
	uint16_t	v;
	v = HOST_TO_LENDIAN_INT16(*value);
	if (af_fwrite(&v, sizeof (v), 1, vf) != 1)
		return AF_FAIL;
	return AF_SUCCEED;
}
Ejemplo n.º 7
0
void g711run_push (_AFmoduleinst *i)
{
	g711_data *d = (g711_data *)i->modspec;
	AFframecount frames2write = i->inc->nframes;
	AFframecount samps2write = i->inc->nframes * i->inc->f.channelCount;
	int framesize = sizeof (g711samp) * (i->inc->f.channelCount);
	AFframecount nfr;

	assert(d->trk->f.compressionType == AF_COMPRESSION_G711_ULAW ||
		d->trk->f.compressionType == AF_COMPRESSION_G711_ALAW);

	/* Compress frames into i->outc. */

	if (d->trk->f.compressionType == AF_COMPRESSION_G711_ULAW)
		linear2ulaw_buf(i->inc->buf, i->outc->buf, samps2write);
	else
		linear2alaw_buf(i->inc->buf, i->outc->buf, samps2write);

	/* Write the compressed data. */

	nfr = af_fwrite(i->outc->buf, framesize, frames2write, d->fh);

	CHNK(printf("writing %d frames to g711 file\n", frames2write));

	if (nfr != frames2write)
	{
		/* report error if we haven't already */
		if (d->trk->filemodhappy)
		{
			/* i/o error */
			if (nfr < 0)
				_af_error(AF_BAD_WRITE,
					"unable to write data (%s) -- "
					"wrote %d out of %d frames",
					strerror(errno),
					d->trk->nextfframe + nfr,
					d->trk->nextfframe + frames2write);

			/* usual disk full error */
			else
				_af_error(AF_BAD_WRITE,
					"unable to write data (disk full) -- "
					"wrote %d out of %d frames",
					d->trk->nextfframe + nfr,
					d->trk->nextfframe + frames2write);

			d->trk->filemodhappy = AF_FALSE;
		}
	}

	d->trk->nextfframe += nfr;
	d->trk->totalfframes = d->trk->nextfframe;
	d->trk->fpos_next_frame += (nfr>0) ? nfr/framesize : 0;

	assert(!d->seekok || (af_ftell(d->fh) == d->trk->fpos_next_frame));
}
Ejemplo n.º 8
0
/**
 * Write an Unitex file content (to system filesystem or filespace)
 * it write from two buffer (prefix and suffix). This is useful for writing both header and footer (or BOM and text...)
 */
UNITEX_FUNC int UNITEX_CALL WriteUnitexFile(const char*name,const void*buffer_prefix,size_t size_prefix,const void*buffer_suffix,size_t size_suffix)
{
    ABSTRACTFILE* vfWrite = af_fopen(name, "wb");
    if (vfWrite == NULL)
    {
        return 1;
    }
    int retValue = 0;
    if (size_prefix > 0)
        if (size_prefix != af_fwrite(buffer_prefix,1,size_prefix,vfWrite))
            retValue = 1;

    if (retValue==0 && (size_suffix > 0))
        if (size_suffix != af_fwrite(buffer_suffix,1,size_suffix,vfWrite))
            retValue = 1;

    af_fclose(vfWrite);
    return retValue;
}
Ejemplo n.º 9
0
status _af_wave_write_init (AFfilesetup setup, AFfilehandle filehandle)
{
	u_int32_t	zero = 0;

	if (_af_filesetup_make_handle(setup, filehandle) == AF_FAIL)
		return AF_FAIL;

	filehandle->formatSpecific = waveinfo_new();

	af_fseek(filehandle->fh, 0, SEEK_SET);
	af_fwrite("RIFF", 4, 1, filehandle->fh);
	af_fwrite(&zero, 4, 1, filehandle->fh);
	af_fwrite("WAVE", 4, 1, filehandle->fh);

	WriteMiscellaneous(filehandle);
	WriteFormat(filehandle);
	WriteData(filehandle);

	return AF_SUCCEED;
}
Ejemplo n.º 10
0
static status next_write_header (AFfilehandle file)
{
	_Track		*track;
	int		frameSize;
	u_int32_t	offset, length, encoding, sampleRate, channelCount;

	track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK);

	frameSize = _af_format_frame_size(&track->f, AF_FALSE);

        if (track->f.compressionType == AF_COMPRESSION_G711_ULAW ||
            track->f.compressionType == AF_COMPRESSION_G711_ALAW)
           frameSize = frameSize / 2;

	offset = HOST_TO_BENDIAN_INT32(track->fpos_first_frame);
	length = HOST_TO_BENDIAN_INT32(track->totalfframes * frameSize);
	encoding = HOST_TO_BENDIAN_INT32(nextencodingtype(&track->f));
	sampleRate = HOST_TO_BENDIAN_INT32(track->f.sampleRate);
	channelCount = HOST_TO_BENDIAN_INT32(track->f.channelCount);

	if (af_fseek(file->fh, 0, SEEK_SET) != 0)
		_af_error(AF_BAD_LSEEK, "bad seek");

	af_fwrite(".snd", 4, 1, file->fh);
	af_fwrite(&offset, 4, 1, file->fh);
	af_fwrite(&length, 4, 1, file->fh);
	af_fwrite(&encoding, 4, 1, file->fh);
	af_fwrite(&sampleRate, 4, 1, file->fh);
	af_fwrite(&channelCount, 4, 1, file->fh);

	return AF_SUCCEED;
}
Ejemplo n.º 11
0
uLong ZCALLBACK afwrite_file_func (
   voidpf opaque,
   voidpf stream,
   const void* buf,
   uLong size)
{
    uLong ret;

    (void)opaque;

    ret = (uLong)af_fwrite(buf, 1, (size_t)size, (ABSTRACTFILE *)stream);
    return ret;
}
Ejemplo n.º 12
0
static status WriteData (AFfilehandle file)
{
	_Track		*track;
	u_int32_t	frameSize, chunkSize;
	_WAVEInfo	*waveinfo;

	assert(file);

	waveinfo = file->formatSpecific;
	track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK);

	af_fwrite("data", 4, 1, file->fh);
	waveinfo->dataSizeOffset = af_ftell(file->fh);

	frameSize = _af_format_frame_size(&track->f, AF_FALSE);
	chunkSize = frameSize * track->totalfframes;
	chunkSize = HOST_TO_LENDIAN_INT32(chunkSize);
	af_fwrite(&chunkSize, 4, 1, file->fh);
	track->fpos_first_frame = af_ftell(file->fh);

	return AF_SUCCEED;
}
Ejemplo n.º 13
0
status af_write_pstring (const char *s, AFvirtualfile *vf)
{
	size_t length = strlen(s);
	if (length > 255)
		return AF_FAIL;
	uint8_t sizeByte = (uint8_t) length;
	af_write_uint8(&sizeByte, vf);
	af_fwrite(s, sizeByte, 1, vf);
	/*
		Add a pad byte if the length of the Pascal-style string
		(including the size byte) is odd.
	*/
	if ((length % 2) == 0)
	{
		uint8_t zero = 0;
		af_write_uint8(&zero, vf);
	}
	return AF_SUCCEED;
}
Ejemplo n.º 14
0
static status WriteData (AFfilehandle file)
{
	_Track		*track;
	uint32_t	chunkSize;
	_WAVEInfo	*waveinfo;

	assert(file);

	waveinfo = file->formatSpecific;
	track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK);

	af_fwrite("data", 4, 1, file->fh);
	waveinfo->dataSizeOffset = af_ftell(file->fh);

	chunkSize = (int) _af_format_frame_size(&track->f, false) *
		track->totalfframes;

	af_write_uint32_le(&chunkSize, file->fh);
	track->fpos_first_frame = af_ftell(file->fh);

	return AF_SUCCEED;
}
Ejemplo n.º 15
0
static status WriteFormat (AFfilehandle file)
{
	_Track		*track = NULL;

	uint16_t	formatTag, channelCount;
	uint32_t	sampleRate, averageBytesPerSecond;
	uint16_t	blockAlign;
	uint32_t	chunkSize;
	uint16_t	bitsPerSample;

	assert(file != NULL);

	track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK);

	af_fwrite("fmt ", 4, 1, file->fh);

	switch (track->f.compressionType)
	{
		case AF_COMPRESSION_NONE:
			chunkSize = 16;
			if (track->f.sampleFormat == AF_SAMPFMT_FLOAT ||
				track->f.sampleFormat == AF_SAMPFMT_DOUBLE)
			{
				formatTag = WAVE_FORMAT_IEEE_FLOAT;
			}
			else if (track->f.sampleFormat == AF_SAMPFMT_TWOSCOMP ||
				track->f.sampleFormat == AF_SAMPFMT_UNSIGNED)
			{
				formatTag = WAVE_FORMAT_PCM;
			}
			else
			{
				_af_error(AF_BAD_COMPTYPE, "bad sample format");
				return AF_FAIL;
			}

			blockAlign = _af_format_frame_size(&track->f, false);
			bitsPerSample = 8 * _af_format_sample_size(&track->f, false);
			break;

		/*
			G.711 compression uses eight bits per sample.
		*/
		case AF_COMPRESSION_G711_ULAW:
			chunkSize = 18;
			formatTag = IBM_FORMAT_MULAW;
			blockAlign = track->f.channelCount;
			bitsPerSample = 8;
			break;

		case AF_COMPRESSION_G711_ALAW:
			chunkSize = 18;
			formatTag = IBM_FORMAT_ALAW;
			blockAlign = track->f.channelCount;
			bitsPerSample = 8;
			break;

		default:
			_af_error(AF_BAD_COMPTYPE, "bad compression type");
			return AF_FAIL;
	}

	af_write_uint32_le(&chunkSize, file->fh);
	af_write_uint16_le(&formatTag, file->fh);

	channelCount = track->f.channelCount;
	af_write_uint16_le(&channelCount, file->fh);

	sampleRate = track->f.sampleRate;
	af_write_uint32_le(&sampleRate, file->fh);

	averageBytesPerSecond =
		track->f.sampleRate * _af_format_frame_size(&track->f, false);
	af_write_uint32_le(&averageBytesPerSecond, file->fh);

	blockAlign = _af_format_frame_size(&track->f, false);
	af_write_uint16_le(&blockAlign, file->fh);

	af_write_uint16_le(&bitsPerSample, file->fh);

	if (track->f.compressionType == AF_COMPRESSION_G711_ULAW ||
		track->f.compressionType == AF_COMPRESSION_G711_ALAW)
	{
		uint16_t	zero = 0;
		af_write_uint16_le(&zero, file->fh);
	}

	return AF_SUCCEED;
}
Ejemplo n.º 16
0
static status WriteCues (AFfilehandle file)
{
	int		i, *markids, markCount;
	uint32_t	numCues, cueChunkSize, listChunkSize;
	_WAVEInfo	*wave;

	assert(file);

	markCount = afGetMarkIDs(file, AF_DEFAULT_TRACK, NULL);
	if (markCount == 0)
		return AF_SUCCEED;

	wave = file->formatSpecific;

	if (wave->markOffset == 0)
		wave->markOffset = af_ftell(file->fh);
	else
		af_fseek(file->fh, wave->markOffset, SEEK_SET);

	af_fwrite("cue ", 4, 1, file->fh);

	/*
		The cue chunk consists of 4 bytes for the number of cue points
		followed by 24 bytes for each cue point record.
	*/
	cueChunkSize = 4 + markCount * 24;
	af_write_uint32_le(&cueChunkSize, file->fh);
	numCues = markCount;
	af_write_uint32_le(&numCues, file->fh);

	markids = _af_calloc(markCount, sizeof (int));
	assert(markids != NULL);
	afGetMarkIDs(file, AF_DEFAULT_TRACK, markids);

	/* Write each marker to the file. */
	for (i=0; i < markCount; i++)
	{
		uint32_t	identifier, position, chunkStart, blockStart;
		uint32_t	sampleOffset;
		AFframecount	markposition;

		identifier = markids[i];
		af_write_uint32_le(&identifier, file->fh);

		position = i;
		af_write_uint32_le(&position, file->fh);

		/* For now the RIFF id is always the first data chunk. */
		af_fwrite("data", 4, 1, file->fh);

		/*
			For an uncompressed WAVE file which contains
			only one data chunk, chunkStart and blockStart
			are zero.
		*/
		chunkStart = 0;
		af_fwrite(&chunkStart, sizeof (uint32_t), 1, file->fh);

		blockStart = 0;
		af_fwrite(&blockStart, sizeof (uint32_t), 1, file->fh);

		markposition = afGetMarkPosition(file, AF_DEFAULT_TRACK, markids[i]);

		/* Sample offsets are stored in the WAVE file as frames. */
		sampleOffset = markposition;
		af_write_uint32_le(&sampleOffset, file->fh);
	}

	/*
		Now write the cue names which is in a master list chunk
		with a subchunk for each cue's name.
	*/

	listChunkSize = 4;
	for (i=0; i<markCount; i++)
	{
		const char *name;

		name = afGetMarkName(file, AF_DEFAULT_TRACK, markids[i]);

		/*
			Each label chunk consists of 4 bytes for the
			"labl" chunk ID, 4 bytes for the chunk data
			size, 4 bytes for the cue point ID, and then
			the length of the label as a Pascal-style string.

			In all, this is 12 bytes plus the length of the
			string, its size byte, and a trailing pad byte
			if the length of the chunk is otherwise odd.
		*/
		listChunkSize += 12 + (strlen(name) + 1) +
			((strlen(name) + 1) % 2);
	}

	af_fwrite("LIST", 4, 1, file->fh);
	af_write_uint32_le(&listChunkSize, file->fh);
	af_fwrite("adtl", 4, 1, file->fh);

	for (i=0; i<markCount; i++)
	{
		const char	*name;
		uint32_t	labelSize, cuePointID;

		name = afGetMarkName(file, AF_DEFAULT_TRACK, markids[i]);

		/* Make labelSize even if it is not already. */
		labelSize = 4+(strlen(name)+1) + ((strlen(name) + 1) % 2);
		cuePointID = markids[i];

		af_fwrite("labl", 4, 1, file->fh);
		af_write_uint32_le(&labelSize, file->fh);
		af_write_uint32_le(&cuePointID, file->fh);
		af_fwrite(name, strlen(name) + 1, 1, file->fh);
		/*
			If the name plus the size byte comprises an odd
			length, add another byte to make the string an
			even length.
		*/
		if (((strlen(name) + 1) % 2) != 0)
		{
			uint8_t	zero=0;
			af_write_uint8(&zero, file->fh);
		}
	}

	free(markids);

	return AF_SUCCEED;
}
Ejemplo n.º 17
0
status WriteMiscellaneous (AFfilehandle filehandle)
{
	_WAVEInfo	*wave = (_WAVEInfo *) filehandle->formatSpecific;

	if (filehandle->miscellaneousCount != 0)
	{
		int		i;
		uint32_t	miscellaneousBytes;
		uint32_t 	chunkSize;

		/* Start at 12 to account for 'LIST', size, and 'INFO'. */
		miscellaneousBytes = 12;

		/* Then calculate the size of the whole INFO chunk. */
		for (i=0; i<filehandle->miscellaneousCount; i++)
		{
			uint32_t	miscid;

			/* Skip miscellaneous data of an unsupported type. */
			if (misc_type_to_wave(filehandle->miscellaneous[i].type,
				&miscid) == AF_FAIL)
				continue;

			/* Account for miscellaneous type and size. */
			miscellaneousBytes += 8;
			miscellaneousBytes += filehandle->miscellaneous[i].size;

			/* Add a pad byte if necessary. */
			if (filehandle->miscellaneous[i].size % 2 != 0)
				miscellaneousBytes++;

			assert(miscellaneousBytes % 2 == 0);
		}

		if (wave->miscellaneousStartOffset == 0)
			wave->miscellaneousStartOffset = af_ftell(filehandle->fh);
		else
			af_fseek(filehandle->fh, wave->miscellaneousStartOffset, SEEK_SET);

		wave->totalMiscellaneousSize = miscellaneousBytes;

		/*
			Write the data.  On the first call to this
			function (from _af_wave_write_init), the
			data won't be available, af_fseek is used to
			reserve space until the data has been provided.
			On subseuent calls to this function (from
			_af_wave_update), the data will really be written.
		*/

		/* Write 'LIST'. */
		af_fwrite("LIST", 4, 1, filehandle->fh);

		/* Write the size of the following chunk. */
		chunkSize = miscellaneousBytes-8;
		af_write_uint32_le(&chunkSize, filehandle->fh);

		/* Write 'INFO'. */
		af_fwrite("INFO", 4, 1, filehandle->fh);

		/* Write each miscellaneous chunk. */
		for (i=0; i<filehandle->miscellaneousCount; i++)
		{
			uint32_t	miscsize = filehandle->miscellaneous[i].size;
			uint32_t 	miscid = 0;

			/* Skip miscellaneous data of an unsupported type. */
			if (misc_type_to_wave(filehandle->miscellaneous[i].type,
				&miscid) == AF_FAIL)
				continue;

			af_fwrite(&miscid, 4, 1, filehandle->fh);
			af_write_uint32_le(&miscsize, filehandle->fh);
			if (filehandle->miscellaneous[i].buffer != NULL)
			{
				uint8_t	zero = 0;

				af_fwrite(filehandle->miscellaneous[i].buffer, filehandle->miscellaneous[i].size, 1, filehandle->fh);

				/* Pad if necessary. */
				if ((filehandle->miscellaneous[i].size%2) != 0)
					af_write_uint8(&zero, filehandle->fh);
			}
			else
			{
				int	size;
				size = filehandle->miscellaneous[i].size;

				/* Pad if necessary. */
				if ((size % 2) != 0)
					size++;
				af_fseek(filehandle->fh, size, SEEK_CUR);
			}
		}
	}

	return AF_SUCCEED;
}
Ejemplo n.º 18
0
status af_write_uint8 (const uint8_t *value, AFvirtualfile *vf)
{
	if (af_fwrite(value, 1, 1, vf) != 1)
		return AF_FAIL;
	return AF_SUCCEED;
}
Ejemplo n.º 19
0
status _af_avr_write_init (AFfilesetup setup, AFfilehandle filehandle)
{
	_Track		*track;
	char		name[8];
	uint16_t	mono, resolution, sign, loop, midi;
	uint32_t	rate, size, loopStart, loopEnd;
	char		reserved[26];
	char		user[64];

	if (_af_filesetup_make_handle(setup, filehandle) == AF_FAIL)
		return AF_FAIL;

	filehandle->formatSpecific = NULL;

	track = _af_filehandle_get_track(filehandle, AF_DEFAULT_TRACK);

	if (af_fseek(filehandle->fh, 0, SEEK_SET) != 0)
	{
		_af_error(AF_BAD_LSEEK, "bad seek");
		return AF_FAIL;
	}

	af_fwrite("2BIT", 4, 1, filehandle->fh);
	memset(name, 0, 8);
	if (filehandle->fileName != NULL)
		strncpy(name, af_basename(filehandle->fileName), 8);
	af_fwrite(name, 8, 1, filehandle->fh);

	if (track->f.channelCount == 1)
		mono = 0x0;
	else
		mono = 0xffff;
	af_write_uint16_be(&mono, filehandle->fh);

	resolution = track->f.sampleWidth;
	af_write_uint16_be(&resolution, filehandle->fh);

	if (track->f.sampleFormat == AF_SAMPFMT_UNSIGNED)
		sign = 0x0;
	else
		sign = 0xffff;
	af_write_uint16_be(&sign, filehandle->fh);

	/* We do not currently support loops. */
	loop = 0;
	af_write_uint16_be(&loop, filehandle->fh);
	midi = 0xffff;
	af_write_uint16_be(&midi, filehandle->fh);

	rate = track->f.sampleRate;
	/* Set the high-order byte of rate to 0xff. */
	rate |= 0xff000000;
	size = track->totalfframes;
	loopStart = 0;
	loopEnd = size;

	af_write_uint32_be(&rate, filehandle->fh);
	af_write_uint32_be(&size, filehandle->fh);
	af_write_uint32_be(&loopStart, filehandle->fh);
	af_write_uint32_be(&loopEnd, filehandle->fh);

	memset(reserved, 0, 26);
	af_fwrite(reserved, 26, 1, filehandle->fh);

	memset(user, 0, 64);
	af_fwrite(user, 64, 1, filehandle->fh);

	if (track->fpos_first_frame == 0)
		track->fpos_first_frame = af_ftell(filehandle->fh);

	return AF_SUCCEED;
}
Ejemplo n.º 20
0
static int do_extract_from_opened_pack_archive_currentfile(
    unzFile uf,
    const int* popt_extract_without_path,
    const char* prefix_extracting_name,
    int transform_path_separator, int quiet)
{
    char filename_inzip[0x200];
    char* filename_withoutpath;
    char* p;
    int err=UNZ_OK;
    ABSTRACTFILE *fout=NULL;
    void* buf;
    uInt size_buf;

    unz_file_info file_info;
    err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);

    if (err!=UNZ_OK)
    {
        error("error %d with zipfile in unzGetCurrentFileInfo\n",err);
        return err;
    }

    size_buf = UNPACK_WRITEBUFFERSIZE;
    buf = (void*)malloc(size_buf);
    if (buf==NULL)
    {
        error("Error allocating memory\n");
        return UNZ_INTERNALERROR;
    }

    p = filename_withoutpath = filename_inzip;
    while ((*p) != '\0')
    {
        if (((*p)=='/') || ((*p)=='\\'))
            filename_withoutpath = p+1;
        p++;
    }

    if ((*filename_withoutpath)=='\0')
    {
        if ((*popt_extract_without_path)==0)
        {
            {
                size_t len_prefix = strlen(prefix_extracting_name);
                size_t len_directory_in_zip = strlen(filename_inzip);
                size_t total_len = len_prefix + len_directory_in_zip;
                char* provis_dir_name = NULL;
                if (len_directory_in_zip > 0)
                    provis_dir_name = (char*)malloc(total_len + 2);
                if (provis_dir_name)
                {
                    strcpy(provis_dir_name, prefix_extracting_name);
                    strcpy(provis_dir_name + len_prefix, filename_inzip);
                    transform_fileName_separator(provis_dir_name + len_prefix, transform_path_separator);
                    if (quiet == 0)
                        u_printf("creating directory: %s\n", provis_dir_name);
                    mkDirPortable(provis_dir_name);
                    free(provis_dir_name);
                }
            }
        }
    }
    else
    {
        char* previousPathCreated = NULL;
        const char* write_filename;
        int skip=0;

        if ((*popt_extract_without_path)==0)
            write_filename = filename_inzip;
        else
            write_filename = filename_withoutpath;

        err = unzOpenCurrentFile(uf);
        if (err!=UNZ_OK)
        {
            error("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
        }



        if ((skip==0) && (err==UNZ_OK))
        {
            char* provis_concat_name = NULL;
            if (prefix_extracting_name == NULL)
                prefix_extracting_name = "";

            if (prefix_extracting_name != NULL)
                {
                    size_t len_prefix = strlen(prefix_extracting_name);
                    size_t total_len = len_prefix + strlen(write_filename);
                    provis_concat_name = (char*)malloc(total_len+2);
                    if (provis_concat_name != NULL)
                    {
                        strcpy(provis_concat_name,prefix_extracting_name);
                        strcat(provis_concat_name,write_filename);
                        transform_fileName_separator(provis_concat_name + len_prefix, transform_path_separator);
                    }
                }

            const char* useFileName = (provis_concat_name != NULL) ? provis_concat_name : write_filename;

            if ((*popt_extract_without_path)==0)
            {
                char* newPathOnlyPortion = (char*)malloc(strlen(useFileName)+1);
                if (newPathOnlyPortion != NULL)
                {
                    strcpy(newPathOnlyPortion,useFileName);
                    removeNoPathPortionInFileName(newPathOnlyPortion);

                    int can_skip_creating = 0;
                    if (previousPathCreated != NULL)
                        if (strcmp(previousPathCreated,newPathOnlyPortion) == 0)
                            can_skip_creating = 1;

                    if (can_skip_creating == 0)
                    {
                        if (quiet == 0)
                            u_printf("Creating directory: %s\n",newPathOnlyPortion);
                        mkDirPortable(newPathOnlyPortion);
                    }

                    if (previousPathCreated != NULL)
                        free(previousPathCreated);
                    previousPathCreated = newPathOnlyPortion;
                }
            }

            fout=af_fopen(useFileName,"wb");

            if (quiet == 0)
                u_printf("extracting %s to %s...",filename_inzip,useFileName);

            if (fout==NULL)
            {
                error("error opening %s\n",useFileName);
            }

            if (provis_concat_name != NULL)
                free(provis_concat_name);

            /* some zipfile don't contain directory alone before file */
            if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
                                (filename_withoutpath!=(char*)filename_inzip))
            {
                char c=*(filename_withoutpath-1);
                *(filename_withoutpath-1)='\0';
                mkDirPortable(write_filename);
                *(filename_withoutpath-1)=c;
                fout=af_fopen(write_filename,"wb");
            }



            if (quiet == 0)
                u_printf(" done\n");
        }

        if (fout!=NULL)
        {
            af_setsizereservation(fout, (long)file_info.uncompressed_size);
            do
            {
                err = unzReadCurrentFile(uf,buf,(unsigned)size_buf);
                if (err<0)
                {
                    error("error %d with zipfile in unzReadCurrentFile\n",err);
                    break;
                }
                if (err>0)
                    if (af_fwrite(buf,err,1,fout)!=1)
                    {
                        error("error in writing extracted file\n");
                        err=UNZ_ERRNO;
                        break;
                    }
            }
            while (err>0);
            if (fout)
                    af_fclose(fout);
            /*
            if (err==0)
                change_file_date(write_filename,file_info.dosDate,
                                 file_info.tmu_date);
            */
        }

        if (err==UNZ_OK)
        {
            err = unzCloseCurrentFile (uf);
            if (err!=UNZ_OK)
            {
                error("error %d with zipfile in unzCloseCurrentFile\n",err);
            }
        }
        else
            unzCloseCurrentFile(uf); /* don't lose the error */


        if (previousPathCreated != NULL)
           free(previousPathCreated);
    }

    free(buf);
    return err;
}
Ejemplo n.º 21
0
static struct ExecutionLogging* BuildAllocInitExecutionLoggingForIncrementedNumber(void* privateLoggerPtr)
{
    struct ExecutionLogging* pEL = NULL;
 
    struct UniLoggerSpace * pULS=(struct UniLoggerSpace *)privateLoggerPtr;
    struct ActivityLoggerPrivateData* pALPD = (struct ActivityLoggerPrivateData*) pULS->privateUnloggerData;

    char szNumFileSuffix[256];


    if (pULS -> auto_increment_logfilename == 0)
    {
        return NULL;
    }
    /* we take a mutex, to be sure two thread don't increment and use same number at same time */
    SyncGetMutex(pALPD->pMutexLog);

    if (pULS->szNameLog == NULL) {


        unsigned int current_number = 0;
        const char* szNumFile = buildDupFileNameWithPrefixDir(pULS->szPathLog,"unitex_logging_parameters_count.txt");

        /* here : we will need protect with a mutex */
        ABSTRACTFILE *af_fin = af_fopen_unlogged(szNumFile,"rb");
        if (af_fin!=NULL)
        {
            size_t size_num_file=0;

            if (af_fseek(af_fin, 0, SEEK_END) == 0)
            {
	            size_num_file = af_ftell(af_fin);
                af_fseek(af_fin, 0, SEEK_SET);
            }

            char* buf_num_file=(char*)malloc(size_num_file+1);
            *(buf_num_file+size_num_file)=0;
            if (af_fread(buf_num_file,1,size_num_file,af_fin) == size_num_file)
            {
                sscanf(buf_num_file,"%u",&current_number);
            }
            af_fclose(af_fin);
            free(buf_num_file);        
        }

        current_number++;


        ABSTRACTFILE *af_fout = af_fopen_unlogged(szNumFile,"wb");
        if (af_fout!=NULL)
        {
            char szNumOut[32];
            sprintf(szNumOut,"%010u",current_number);
            af_fwrite(szNumOut,1,strlen(szNumOut),af_fout);
            af_fclose(af_fout);
        }
        free((void*)szNumFile);
        sprintf(szNumFileSuffix,"unitex_log_%08u.ulp",current_number);
    }
    else {
        sprintf(szNumFileSuffix,"%s",pULS->szNameLog);
    }
    SyncReleaseMutex(pALPD->pMutexLog);
    
    const char* szLogFileName = buildDupFileNameWithPrefixDir(pULS->szPathLog,szNumFileSuffix);

    pEL=BuildAllocInitExecutionLogging(privateLoggerPtr,szLogFileName);
    free((void*)szLogFileName);

    return pEL;
}
Ejemplo n.º 22
0
static status WriteFormat (AFfilehandle file)
{
	_Track		*track = NULL;
	u_int16_t	formatTag, channelCount;
	u_int32_t	sampleRate, averageBytesPerSecond;
	u_int16_t	blockAlign;
	u_int32_t	chunkSize;
	u_int16_t	bitsPerSample;
	_WAVEInfo	*waveinfo = NULL;

	assert(file != NULL);

	track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK);
	waveinfo = (_WAVEInfo *) file->formatSpecific;

	af_fwrite("fmt ", 4, 1, file->fh);

	switch (track->f.compressionType)
	{
		case AF_COMPRESSION_NONE:
			chunkSize = 16;
			formatTag = WAVE_FORMAT_PCM;
			blockAlign = _af_format_frame_size(&track->f, AF_FALSE);
			bitsPerSample = 8 * _af_format_sample_size(&track->f, AF_FALSE);
			break;
		case AF_COMPRESSION_G711_ULAW:
			chunkSize = 18;
			formatTag = IBM_FORMAT_MULAW;
			blockAlign = _af_format_frame_size(&track->f, AF_FALSE);
			bitsPerSample = 8 * _af_format_sample_size(&track->f, AF_FALSE);
			break;
		case AF_COMPRESSION_G711_ALAW:
			chunkSize = 18;
			formatTag = IBM_FORMAT_ALAW;
			blockAlign = _af_format_frame_size(&track->f, AF_FALSE);
			bitsPerSample = 8 * _af_format_sample_size(&track->f, AF_FALSE);
			break;
		default:
			_af_error(AF_BAD_COMPTYPE, "bad compression type");
			return AF_FAIL;
	}

	chunkSize = HOST_TO_LENDIAN_INT32(chunkSize);
	af_fwrite(&chunkSize, 4, 1, file->fh);

	formatTag = HOST_TO_LENDIAN_INT16(formatTag);
	af_fwrite(&formatTag, 2, 1, file->fh);
	formatTag = LENDIAN_TO_HOST_INT16(formatTag);

	channelCount = track->f.channelCount;
	channelCount = HOST_TO_LENDIAN_INT16(channelCount);
	af_fwrite(&channelCount, 2, 1, file->fh);

	sampleRate = track->f.sampleRate;
	sampleRate = HOST_TO_LENDIAN_INT32(sampleRate);
	af_fwrite(&sampleRate, 4, 1, file->fh);

	averageBytesPerSecond =
		track->f.sampleRate * _af_format_frame_size(&track->f, AF_FALSE);
	averageBytesPerSecond = HOST_TO_LENDIAN_INT32(averageBytesPerSecond);
	af_fwrite(&averageBytesPerSecond, 4, 1, file->fh);

	blockAlign = _af_format_frame_size(&track->f, AF_FALSE);
	blockAlign = HOST_TO_LENDIAN_INT16(blockAlign);
	af_fwrite(&blockAlign, 2, 1, file->fh);

	bitsPerSample = HOST_TO_LENDIAN_INT16(bitsPerSample);
	af_fwrite(&bitsPerSample, 2, 1, file->fh);

	/*
		If the data is compressed we have additional
		format-specific information to write as well as the
		'fact' (frame count) chunk.
	*/
	if (track->f.compressionType != AF_COMPRESSION_NONE)
	{
		u_int32_t	factSize = 4;
		u_int32_t	totalFrameCount = 0;

		if (track->f.compressionType == AF_COMPRESSION_G711_ULAW ||
			track->f.compressionType == AF_COMPRESSION_G711_ALAW)
		{
			u_int16_t	zero = 0;
			af_fwrite(&zero, 2, 1, file->fh);
		}

		af_fwrite("fact", 4, 1, file->fh);
		factSize = HOST_TO_LENDIAN_INT32(factSize);
		af_fwrite(&factSize, 4, 1, file->fh);

		waveinfo->fileSizeOffset = af_ftell(file->fh);

		totalFrameCount = HOST_TO_LENDIAN_INT32(totalFrameCount);
		af_fwrite(&totalFrameCount, 4, 1, file->fh);
	}

	return AF_SUCCEED;
}