コード例 #1
0
ファイル: font_family_impl.cpp プロジェクト: cosim/ClanLib
	void FontFamily_Impl::font_face_load(const FontDescription &desc, const std::string &typeface_name, float pixel_ratio)
	{
#if defined(WIN32)
		std::shared_ptr<FontEngine> engine = std::make_shared<FontEngine_Win32>(desc, typeface_name, pixel_ratio);
		font_cache.push_back(Font_Cache(engine));
		font_cache.back().glyph_cache->set_texture_group(texture_group);
		font_cache.back().pixel_ratio = pixel_ratio;
#elif defined(__APPLE__)
		std::shared_ptr<FontEngine> engine = std::make_shared<FontEngine_Cocoa>(desc, typeface_name, pixel_ratio);
		font_cache.push_back(Font_Cache(engine));
		font_cache.back().glyph_cache->set_texture_group(texture_group);
		font_cache.back().pixel_ratio = pixel_ratio;
#elif defined(__ANDROID__)
		throw Exception("automatic typeface to ttf file selection is not supported on android");
#else

		// Obtain the best matching font file from fontconfig.
		FontConfig &fc = FontConfig::instance();
		std::string font_file_path = fc.match_font(typeface_name, desc);
		std::string path = PathHelp::get_fullpath(font_file_path, PathHelp::path_type_file);
		auto filename = PathHelp::get_filename(font_file_path, PathHelp::path_type_file);
		auto fs = FileSystem(path);
		IODevice file = fs.open_file(filename);
		DataBuffer font_databuffer;
		font_databuffer.set_size(file.get_size());
		file.read(font_databuffer.get_data(), font_databuffer.get_size());
		font_face_load(desc, font_databuffer, pixel_ratio);
#endif
	}
コード例 #2
0
ファイル: xml_tokenizer.cpp プロジェクト: Cassie90/ClanLib
XMLTokenizer::XMLTokenizer(IODevice &input) : impl(new XMLTokenizer_Impl)
{
	impl->input = input;
	impl->size = input.get_size();
	impl->pos = 0;

	DataBuffer buffer(impl->size);
	input.receive(buffer.get_data(), buffer.get_size(), true);

	StringHelp::BOMType bom_type = StringHelp::detect_bom(buffer.get_data(), buffer.get_size());
	switch (bom_type)
	{
	default:
	case StringHelp::bom_none:
		impl->data = StringHelp::utf8_to_text(std::string(buffer.get_data(), buffer.get_size()));
		break;
	case StringHelp::bom_utf32_be:
	case StringHelp::bom_utf32_le:
		throw Exception("UTF-16 XML files not supported yet");
		break;
	case StringHelp::bom_utf16_be:
	case StringHelp::bom_utf16_le:
		throw Exception("UTF-32 XML files not supported yet");
		break;
	case StringHelp::bom_utf8:
		impl->data = StringHelp::utf8_to_text(std::string(buffer.get_data()+3, buffer.get_size()-3));
		break;
	}

}
コード例 #3
0
ファイル: Commander.cpp プロジェクト: simophin/remotevision
Error Commander::
readCommand(Command &cmd) {
	Error rc;
	IODevice *dd = d->device;
	assert(dd != 0);

	CommandHeader hdr;
	char *buf = 0;
	// Read header
	{
		size_t read_size;
		rc = dd->read((char *)&hdr,sizeof(hdr), &read_size);
		if (!rc.isSuccess()) {
			return rc;
		}
		if( read_size != sizeof(hdr)) {
			rc.setErrorType(Error::ERR_INVALID, "header corrupted");
			return rc;
		}
	}

	// Read command
	{
		buf = (char *)::malloc(hdr.length);
		int tried_times = 0;
		size_t offset = 0;
		size_t read_size;
		do {
			rc = dd->read( (char *)(buf + offset), hdr.length-offset, &read_size);
			if (!rc.isSuccess()) {
				goto out;
			}
			offset += read_size;
		}while(tried_times++ < 20 && offset < hdr.length);
	}
	{
		String name;
		std::vector<String> args;
		size_t offset = 0;

		name = buf;
		offset += ::strlen(buf)+1;


		while( (unsigned int)offset < (unsigned )hdr.length) {
			args.push_back(buf+offset);
			offset += ::strlen(buf+offset)+1;
		}

		cmd.setName(name);
		cmd.setArguments(args);
	}

	if (buf) ::free(buf);
	return rc;

	out:
	if (buf) ::free(buf);
	return rc;
}
コード例 #4
0
	void Zip64EndOfCentralDirectoryLocator::save(IODevice &output)
	{
		output.write_int32(signature);
		output.write_int32(number_of_disk_with_zip64_end_of_central_directory);
		output.write_int64(relative_offset_of_zip64_end_of_central_directory);
		output.write_int32(total_number_of_disks);
	}
コード例 #5
0
void SoundProvider_Vorbis_Impl::load(IODevice &input)
{
    int size = input.get_size();
    buffer = DataBuffer(size);
    int bytes_read = input.read(buffer.get_data(), buffer.get_size());
    buffer.set_size(bytes_read);
}
コード例 #6
0
ファイル: shader_object.cpp プロジェクト: ARMCoderCHS/ClanLib
ShaderObject ShaderObject::load(GraphicContext &gc, ShaderType shader_type, IODevice &file)
{
	int size = file.get_size();
	std::string source(size, 0);
	file.read(&source[0], size);

	return ShaderObject(gc, shader_type, StringHelp::local8_to_text(source));
}
コード例 #7
0
	void LocalDeviceManager::setBackgroundImage(string uri) {
		IODevice* dev;
		map<unsigned int, IODevice*>::iterator i;

		i = devices->find(0);
		if (i != devices->end()) {
			dev = i->second;
			dev->setBackgroundImage(uri);
		}
	}
コード例 #8
0
	void* LocalDeviceManager::getGfxRoot(unsigned int deviceNumber) {
		IODevice* dev;

		if (devices->count(deviceNumber) == 0) {
			return 0;
		}

		dev = (*devices)[deviceNumber];
		return dev->getGfxRoot();
	}
コード例 #9
0
ファイル: IOBuffer.cpp プロジェクト: 3Nigma/frayon
void IOBuffer::attach(IODevice& ioDevice)
{ 
    if( ioDevice.isReading() || ioDevice.isWriting() )
        throw IOPending("IODevice in use");

    this->detach();

    _ioDevice = &ioDevice;
    ioDevice.inputReady() += slot(*this, &IOBuffer::onRead);
    ioDevice.outputReady() += slot(*this, &IOBuffer::onWrite);
}
コード例 #10
0
	void LocalDeviceManager::releaseWindow(
			void* win, unsigned int deviceNumber, unsigned int screenNumber) {

		IODevice* dev;

		if (devices->count(deviceNumber) == 0) {
			return;
		}

		dev = (*devices)[deviceNumber];
                return dev->releaseWindow(win, screenNumber);
	}
コード例 #11
0
	void Zip64EndOfCentralDirectoryLocator::load(IODevice &input)
	{
		signature = input.read_int32();
		if (signature != 0x07064b50)
		{
			throw Exception("Incorrect Zip64 End of central directory locator signature!");
		}

		number_of_disk_with_zip64_end_of_central_directory = input.read_int32();
		relative_offset_of_zip64_end_of_central_directory = input.read_int64();
		total_number_of_disks = input.read_int32();
	}
コード例 #12
0
	int LocalDeviceManager::getDeviceHeight(
			unsigned int deviceNumber, unsigned int screenNumber) {

		IODevice* dev;

		if (devices->count(deviceNumber) == 0) {
			return 0;
		}

		dev = (*devices)[deviceNumber];
		return dev->getScreenHeightRes(screenNumber);
	}
コード例 #13
0
        void* LocalDeviceManager::createSurface(
			void* surfaceDesc,
			unsigned int deviceNumber, unsigned int screenNumber) {

		IODevice* dev;

		if (devices->count(deviceNumber) == 0) {
			return NULL;
		}

		dev = (*devices)[deviceNumber];
                return dev->createSurface(surfaceDesc, screenNumber);
	}
コード例 #14
0
	void LocalDeviceManager::releaseSurface(
			void* sur,
			unsigned int deviceNumber, unsigned int screenNumber) {

		IODevice* dev;

		if (devices->count(deviceNumber) == 0) {
			return;
		}

		dev = (*devices)[deviceNumber];
                return dev->releaseSurface(sur, screenNumber);
	}
コード例 #15
0
ファイル: zip_reader.cpp プロジェクト: keigen-shu/ClanLib
	int64_t ZipReader_Impl::deflate_read(void *data, int64_t size, bool read_all)
	{
		zs.next_out = (unsigned char *)data;
		zs.avail_out = size;
		// Continue feeding zlib data until we get our data:
		while (zs.avail_out > 0)
		{
			// zlib needs more data:
			if (zs.avail_in == 0 && compressed_pos < local_header.compressed_size)
			{
				// Read some compressed data:
				int received_input = 0;
				while (received_input < 16 * 1024)
				{
					received_input += input.receive(zbuffer, int(min((int64_t)16 * 1024, local_header.compressed_size - compressed_pos)), true);
					if (compressed_pos + received_input == local_header.compressed_size) break;
				}
				compressed_pos += received_input;

				zs.next_in = (unsigned char *)zbuffer;
				zs.avail_in = received_input;
			}

			// Decompress data:
			int result = mz_inflate(&zs, (compressed_pos == local_header.compressed_size) ? MZ_FINISH : MZ_NO_FLUSH);
			if (result == MZ_STREAM_END) break;
			if (result == MZ_NEED_DICT) throw Exception("Zlib inflate wants a dictionary!");
			if (result == MZ_DATA_ERROR) throw Exception("Zip data stream is corrupted");
			if (result == MZ_STREAM_ERROR) throw Exception("Zip stream structure was inconsistent!");
			if (result == MZ_MEM_ERROR) throw Exception("Zlib did not have enough memory to decompress file!");
			if (result == MZ_BUF_ERROR) throw Exception("Not enough data in buffer when Z_FINISH was used");
			if (result != MZ_OK) throw Exception("Zlib inflate failed while decompressing zip file!");
		}
		return size - zs.avail_out;
	}
コード例 #16
0
void OutlineProviderFile_Impl::load(IODevice &input_source)
{
	// file type & version identifiers
	int type = input_source.read_uint32();
	unsigned char version = input_source.read_uint8();

	if( type != 0x16082004  )
		throw Exception("File is not a collision outline file" );
	if( version != 1 )
		throw Exception(string_format("Unsupported version of outline format: %1. Supported versions: 1.", version) );

	// read in width and height
	width = input_source.read_int32();
	height = input_source.read_int32();

	// x-pos of enclosing disc
	minimum_enclosing_disc.position.x = input_source.read_float();
	// y-pos of enclosing disc
	minimum_enclosing_disc.position.y = input_source.read_float();
	// radius of enclosing disc
	minimum_enclosing_disc.radius = input_source.read_float();
	
	// num contours
	int num_contours = input_source.read_uint32();

	for( int cc=0; cc < num_contours; ++cc )
	{
		Contour contour;

		int num_points = input_source.read_uint32();

		for( int pp=0; pp < num_points; ++pp )
		{
			Pointf point(0,0);
			point.x = input_source.read_float();
			point.y = input_source.read_float();

			contour.get_points().push_back(point);
		}
		
		contours.push_back(contour);
	}
}
コード例 #17
0
	unsigned int SoundProvider_Wave_Impl::find_subchunk(const char *chunk, IODevice &source, unsigned int file_offset, unsigned int max_offset)
	{
		char subchunk1_id[4];

		max_offset -= 8;	// Each subchunk must contains at least name and size
		while (file_offset < max_offset)
		{
			source.seek(file_offset);
			source.read(subchunk1_id, 4);
			uint32_t subchunk1_size = source.read_uint32();
			if (!memcmp(subchunk1_id, chunk, 4))
			{
				// Found chunk
				return subchunk1_size;
			}
			file_offset += subchunk1_size + 8;
		}

		throw Exception("Block not found!");
	}
コード例 #18
0
	void Zip64EndOfCentralDirectoryRecord::load(IODevice &input)
	{
		signature = input.read_int32();
		if (signature != 0x06064b50)
		{
			throw Exception("Incorrect Zip64 End of Central Directory Record signature");
		}

		size_of_record = input.read_int64();
		version_made_by = input.read_int16();
		version_needed_to_extract = input.read_int16();
		number_of_this_disk = input.read_int32();
		number_of_disk_with_central_directory_start = input.read_int32();
		number_of_entries_on_this_disk = input.read_int64();
		number_of_entries_in_central_directory = input.read_int64();
		size_of_central_directory = input.read_int64();
		offset_to_start_of_central_directory = input.read_int64();

		// todo: read extensible data sector
	}
コード例 #19
0
ファイル: css_document.cpp プロジェクト: Cassie90/ClanLib
void CSSDocument::add_sheet(CSSSheetOrigin origin, const std::string &filename, const FileSystem &fs)
{
	// Load the css document:
	IODevice file = fs.open_file(filename);
	DataBuffer file_data(file.get_size());
	file.read(file_data.get_data(), file_data.get_size());
	std::string css_text(file_data.get_data(), file_data.get_size());

	// Find the base URI for this css document:
	std::string base_uri = PathHelp::get_fullpath(filename);

	// Find import directives and load those first:
	std::vector<std::string> import_urls = CSSTokenizer(css_text).read_import_urls();
	for (size_t i = 0; i < import_urls.size(); i++)
	{
		add_sheet(origin, PathHelp::combine(base_uri, import_urls[i]), fs);
	}

	// Add the css sheet:
	CSSTokenizer tokenizer(css_text);
	impl->sheets.push_back(std::shared_ptr<CSSDocumentSheet>(new CSSDocumentSheet(origin, tokenizer, base_uri)));
}
コード例 #20
0
	int LocalDeviceManager::createDevice(string description) {
		unsigned int deviceNumber;
		IODevice* dev;
		//DeviceAudio* aud;
		//DeviceCommunication* com;
		

		if (description == "systemScreen(0)") {
			deviceNumber = devices->size();
			dev = new IODevice();
			(*devices)[deviceNumber] = dev;
			(*profiles)[deviceNumber] = description;

			_deviceScreen = new DFBDeviceScreen(0, NULL);

			if (_deviceScreen != NULL) {
				dev->addScreen(_deviceScreen);
			}

			return (int)deviceNumber;
		}
		return -1;
	}
コード例 #21
0
ファイル: Commander.cpp プロジェクト: simophin/remotevision
Error Commander::
writeCommand (const Command & cmd) {
	IODevice *dd = d->device;
	Error rc;
	assert(dd != 0);


	std::stringstream buf;
	buf << cmd.getName() << '\0';

	std::vector<String> args = cmd.getArguments();
	for (unsigned i=0;i<args.size();i++) {
		buf << args[i] << '\0';
	}

	String data = buf.str();
	CommandHeader hdr;
	// Write header
	{
		::memset (&hdr,0,sizeof(hdr));
		hdr.length = data.size();
	}

	// Write data
	{
		rc = dd->write((char *)&hdr, sizeof(hdr));
		if ( !rc.isSuccess()) {
			return rc;
		}
		rc = dd->write(data.c_str(), data.size());
		if ( !rc.isSuccess() ) {
			return rc;
		}
	}

	return rc;
}
コード例 #22
0
void CollisionOutline_Impl::save(IODevice &output_source) const
{
/*	fileformat:

	uint32  type        // file type identifier	
	uint8   version     // file version	
	uint32  width       // width of the outline
	uint32  height      // height of the outline
	float32 x-pos       // of enclosing disc
	float32 y-pos       // of enclosing disc
	float32 radius      // of enclosing disc

	uint32 num_contours
		uint32 num_points contour 1
			float32 px1
			float32 py1
			float32 px2
			float32 py2
			... contour 1 data ...
		uint32 num_points contour 2
			... contour 2 data ...
		uint32 num_points contour N
			... contour N data ...
*/

	// file type identifier
	output_source.write_uint32( 0x16082004 );

	// fileformat version
	output_source.write_uint8(1);

	// width
	output_source.write_int32(width);

	// height
	output_source.write_int32(height);

	// x-pos of enclosing disc
	output_source.write_float((float)minimum_enclosing_disc.position.x);
	
	// y-pos of enclosing disc
	output_source.write_float((float)minimum_enclosing_disc.position.y);

	// radius of enclosing disc
	output_source.write_float((float)minimum_enclosing_disc.radius);
	
	// number of contours
	output_source.write_uint32(contours.size());
	
	std::vector<Contour>::const_iterator it_cont;
	for( it_cont = contours.begin(); it_cont != contours.end(); ++it_cont )
	{
		// number of points in contours
		output_source.write_uint32((*it_cont).get_points().size());
		
		std::vector<Pointf>::const_iterator it;
		for( it = (*it_cont).get_points().begin(); it != (*it_cont).get_points().end(); ++it )
		{
			// x,y of points
			output_source.write_float((float)(*it).x);
			output_source.write_float((float)(*it).y);
		}
	}
}
コード例 #23
0
	void Zip64EndOfCentralDirectoryRecord::save(IODevice &output)
	{
		output.write_int32(signature);
		output.write_int64(size_of_record);
		output.write_int16(version_made_by);
		output.write_int16(version_needed_to_extract);
		output.write_int32(number_of_this_disk);
		output.write_int32(number_of_disk_with_central_directory_start);
		output.write_int64(number_of_entries_on_this_disk);
		output.write_int64(number_of_entries_in_central_directory);
		output.write_int64(size_of_central_directory);
		output.write_int64(offset_to_start_of_central_directory);
		output.write(extensible_data_sector.data(), extensible_data_sector.size());
	}
コード例 #24
0
void TestApp::test_virtual_directory_part2(void)
{

	Console::write_line("  (Using FileSystem)");

	std::string str;
	std::string cwd(working_dir);
	FileSystem vfs("../../Core/");

//*** testing get_directory_listing()
	Console::write_line("   Function: DirectoryListing get_directory_listing()");
	DirectoryListing listing = vfs.get_directory_listing("");
	do
	{
		if (!listing.next()) fail();
		str = listing.get_filename();
	}while(str != "IOData");

//*** testing open_file()
	Console::write_line("   Function: IODevice Directory::open_file()");
	if(true)
	{
		IODevice device = vfs.open_file("IOData/test_virtual_directory.cpp", File::open_existing, File::access_read, File::share_all, 0);
		char test_data[4];
		if (device.receive(test_data, 4, true) != 4) fail();
		if ( (test_data[0] != '/') || (test_data[1] != '*') ) fail();
	}


//*** testing mount(const std::string &mount_point, const std::string &path)
	Console::write_line("   Function: mount(const std::string &mount_point, const std::string &path)");
	if(true)
	{
		FileSystem vfs("somewhere");
		vfs.mount("ABC", "../../Core/IOData", false);
		IODevice device = vfs.open_file("ABC/test_virtual_directory.cpp", File::open_existing, File::access_read, File::share_all, 0);
	}

//*** testing mount(const std::string &mount_point, FileSystem &fs)
	Console::write_line("   Function: mount(const std::string &mount_point, FileSystem &fs)");
	if(true)
	{
		FileSystem new_vfs(new MyFileSource("Hello/"));
		IODevice device = new_vfs.open_file("ABC/World", File::open_existing, File::access_read, File::share_all, 0);
		if (MyFileSource::fullname != "Hello/ABC/World") fail();
	}

//*** testing unmount(const std::string &mount_point)
	Console::write_line("   Function: unmount(const std::string &mount_point)");
	if(true)
	{
		FileSystem vfs("./");
		vfs.mount("ABC", "../../Core/IOData", false);
		vfs.mount("DEF", "../../Core/IOData", false);
		vfs.mount("GHI", "../../Core/IOData", false);

		vfs.open_file("ABC/test_virtual_directory.cpp", File::open_existing, File::access_read, File::share_all, 0);
		vfs.open_file("DEF/test_virtual_directory.cpp", File::open_existing, File::access_read, File::share_all, 0);
		vfs.open_file("GHI/test_virtual_directory.cpp", File::open_existing, File::access_read, File::share_all, 0);
		vfs.unmount("DEF");
		vfs.unmount("WWW");
		vfs.open_file("ABC/test_virtual_directory.cpp", File::open_existing, File::access_read, File::share_all, 0);
		vfs.open_file("GHI/test_virtual_directory.cpp", File::open_existing, File::access_read, File::share_all, 0);

		try
		{
			vfs.open_file("DEF/test_virtual_directory.cpp", File::open_existing, File::access_read, File::share_all, 0);
			fail();	// Exception should have been called
		}
		catch (Exception errror)
		{
		}
		vfs.unmount("ABC");
		vfs.unmount("GHI");
		try
		{
			vfs.open_file("ABC/test_virtual_directory.cpp", File::open_existing, File::access_read, File::share_all, 0);
			vfs.open_file("GHI/test_virtual_directory.cpp", File::open_existing, File::access_read, File::share_all, 0);

			fail();	// Exception should have been called
		}
		catch (Exception errror)
		{
		}
		vfs.open_file("test_virtual_directory.cpp", File::open_existing, File::access_read, File::share_all, 0);

	}

}
コード例 #25
0
	JPEGFileReader::JPEGFileReader(IODevice iodevice)
		: iodevice(iodevice)
	{
		iodevice.set_big_endian_mode();
	}
コード例 #26
0
void ZipFileHeader::load(IODevice &input)
{
	signature = input.read_int32();
	if (signature != 0x02014b50)
	{
		throw Exception("Incorrect File Header signature");
	}
	version_made_by = input.read_int16();
	version_needed_to_extract = input.read_int16();
	general_purpose_bit_flag = input.read_int16();
	compression_method = input.read_int16();
	last_mod_file_time = input.read_int16();
	last_mod_file_date = input.read_int16();
	crc32 = input.read_uint32();
	compressed_size = input.read_int32();
	uncompressed_size = input.read_int32();
	file_name_length = input.read_int16();
	extra_field_length = input.read_int16();
	file_comment_length = input.read_int16();
	disk_number_start = input.read_int16();
	internal_file_attributes = input.read_int16();
	external_file_attributes = input.read_int32();
	relative_offset_of_local_header = input.read_int32();
	filename.resize(file_name_length);

	auto str1 = new char[file_name_length];
	auto str2 = new char[extra_field_length];
	auto str3 = new char[file_comment_length];
	try
	{
		input.read(str1, file_name_length);
		input.read(str2, extra_field_length);
		input.read(str3, file_comment_length);
		if (general_purpose_bit_flag & ZIP_USE_UTF8)
		{
			filename = StringHelp::utf8_to_text(std::string(str1, file_name_length));
			file_comment = StringHelp::utf8_to_text(std::string(str3, file_comment_length));
		}
		else
		{
			filename = StringHelp::cp437_to_text(std::string(str1, file_name_length));
			file_comment = StringHelp::cp437_to_text(std::string(str3, file_comment_length));
		}

		extra_field = DataBuffer(str2, extra_field_length);

		delete[] str1;
		delete[] str2;
		delete[] str3;
	}
	catch (...)
	{
		delete[] str1;
		delete[] str2;
		delete[] str3;
		throw;
	}
}
コード例 #27
0
void ZipFileHeader::save(IODevice &output)
{
	std::string str_filename;
	std::string str_comment;
	if (general_purpose_bit_flag & ZIP_USE_UTF8)
	{
		str_filename = StringHelp::text_to_utf8(filename);
		str_comment = StringHelp::text_to_utf8(file_comment);
	}
	else
	{
		str_filename = StringHelp::text_to_cp437(filename);
		str_comment = StringHelp::text_to_cp437(file_comment);
	}

	file_name_length = str_filename.length();
	file_comment_length = str_comment.length();

	output.write_int32(signature);
	output.write_int16(version_made_by);
	output.write_int16(version_needed_to_extract);
	output.write_int16(general_purpose_bit_flag);
	output.write_int16(compression_method);
	output.write_int16(last_mod_file_time);
	output.write_int16(last_mod_file_date);
	output.write_uint32(crc32);
	output.write_int32(compressed_size);
	output.write_int32(uncompressed_size);
	output.write_int16(file_name_length);
	output.write_int16(extra_field_length);
	output.write_int16(file_comment_length);
	output.write_int16(disk_number_start);
	output.write_int16(internal_file_attributes);
	output.write_int32(external_file_attributes);
	output.write_int32(relative_offset_of_local_header);
	output.write(str_filename.data(), file_name_length);
	output.write(extra_field.get_data(), extra_field_length);
	output.write(file_comment.data(), file_comment_length);
}
コード例 #28
0
	void SoundProvider_Wave_Impl::load(IODevice &source)
	{
		source.set_little_endian_mode();

		char chunk_id[4];
		source.read(chunk_id, 4);
		if (memcmp(chunk_id, "RIFF", 4))
			throw Exception("Expected RIFF header!");
		uint32_t chunk_size = source.read_uint32();

		char format_id[4];
		source.read(format_id, 4);
		if (memcmp(format_id, "WAVE", 4))
			throw Exception("Expected WAVE header!");

		uint32_t subchunk_pos = source.get_position();
		uint32_t subchunk1_size = find_subchunk("fmt ", source, subchunk_pos, chunk_size);

		uint16_t audio_format = source.read_uint16();
		num_channels = source.read_uint16();
		frequency = source.read_uint32();
		uint32_t byte_rate = source.read_uint32();
		uint16_t block_align = source.read_uint16();
		uint16_t bits_per_sample = source.read_uint16();

		if (bits_per_sample == 16)
			format = sf_16bit_signed;
		else if (bits_per_sample == 8)
			format = sf_8bit_unsigned;
		else
			throw Exception("Unsupported wave sample format");

		uint32_t subchunk2_size = find_subchunk("data", source, subchunk_pos, chunk_size);

		data = new char[subchunk2_size];
		source.read(data, subchunk2_size);

		num_samples = subchunk2_size / block_align;
	}
コード例 #29
0
ファイル: GenPeaks.cpp プロジェクト: eroux/transcriber-ag
// ---------------------
// --- GenPeaks Main ---
// ---------------------
int main(int argc, char**argv)
{
	int c;
	string outdir("");
	//
	// parse program options
	while ((c = getopt(argc , argv, "d:")) != -1) {
		switch (c) {
		case 'd': // service configuration file
			outdir= optarg;
			break;
		case '?':
			USAGE();
		}
	}

	// -- Args Check --
	if (optind >= argc )
	{
		USAGE();
	}


	// -- File Open --
	string		in_path	= argv[optind];
	IODevice*	device	= Guesser::open( in_path.c_str() );

	if (device == NULL)
	{
		Log::err() << "Error --> Unable to open file " << in_path << endl;
		return 1;
	}

	// -- Settings --
	MediumInfo*	info = device->m_info();

	string			peak_file;
	if ( outdir.empty() ) {
		unsigned long pos = in_path.rfind(".");
		if ( pos > 0 && pos != string::npos )
			peak_file = in_path.substr( 0, pos );
		else peak_file = in_path;
	} else {
		unsigned long pos1 = in_path.rfind("/");
		unsigned long pos2 = in_path.rfind(".");
		if ( pos1 > pos2 ) pos2 = string::npos;
		peak_file = outdir + in_path.substr(pos1, pos2 - pos1);
	}
	// -- Peaks processing (per channel) --
	for(int i=0; i<info->audio_channels; i++)
	{
		ostringstream	oss;

		oss << peak_file << "_" << i << ".pks";

		cout << "Processing & saving peaks : " << oss.str() <<  "  SZ = " << sizeof(float) << endl;

		float* tab	= computeStreamPeaks(device, i);
		int size	= (int)(info->audio_duration  / AUDIO_ZOOM_MAX);

		FILE* fd = fopen(oss.str().c_str(), "wb");

		if (fd != NULL)
		{
			int cur = 0;

			while (cur < size)
			{
				int toRead = BUFFER_SIZE;

				if (size-cur < BUFFER_SIZE)
					toRead = size-cur;

				int n = fwrite(tab + cur, sizeof(float), toRead, fd);
				cur += n;
			}
			fflush(fd);
		}
	}

	return 0;
}