<commit_before>#include "in_mad.h"
#include <assert.h>

#define NOISE_SHAPING

namespace {
unsigned long prng(unsigned long state)
{
	return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL;
}
}

mad_input::mad_input() :
	m_file(NULL), m_bitrate(128000)
{
	mad_frame_init(&m_frame);
	mad_stream_init(&m_stream);
	mad_synth_init(&m_synth);
	memset(&m_left_dither, 0, sizeof(m_left_dither));
	memset(&m_right_dither, 0, sizeof(m_right_dither));
}

mad_input::~mad_input()
{
	if (m_file)
		fclose(m_file);
	mad_frame_finish(&m_frame);
	mad_stream_finish(&m_stream);
	mad_synth_finish(&m_synth);
}

int mad_input::open(song *song)
{
	assert(m_file == NULL);
	m_file = fopen(song->fname.c_str(), "rb");
	if (m_file == NULL)
		return -1;
	fill_readbuf();
	return 0;
}

int mad_input::fill_readbuf()
{
	if (m_stream.next_frame) {
		const char *p =
			reinterpret_cast<const char *>(m_stream.next_frame);
		size_t pos = p - m_readbuf.data();
		m_readbuf = m_readbuf.substr(pos);
	}

	size_t toread = 16384 - m_readbuf.size();

	std::string buf(toread, 0);
	ssize_t ret = fread(&buf[0], 1, toread, m_file);
	if (ret <= 0)
		return -1;
	buf.resize(ret);
	m_readbuf += buf;

	mad_stream_buffer(&m_stream,
		reinterpret_cast<const unsigned char *>(m_readbuf.data()),
		m_readbuf.size());

	return 0;
}

#ifdef NOISE_SHAPING
sample_t mad_input::scale(unsigned int bits, mad_fixed_t sample,
			  audio_dither *dither)
{<commit_msg>in_mad: Tiny optimization [PERFECTIVE]<commit_after>#include "in_mad.h"
#include <assert.h>

#define NOISE_SHAPING

namespace {
unsigned long prng(unsigned long state)
{
	return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL;
}
}

mad_input::mad_input() :
	m_file(NULL), m_bitrate(128000)
{
	mad_frame_init(&m_frame);
	mad_stream_init(&m_stream);
	mad_synth_init(&m_synth);
	memset(&m_left_dither, 0, sizeof(m_left_dither));
	memset(&m_right_dither, 0, sizeof(m_right_dither));
}

mad_input::~mad_input()
{
	if (m_file)
		fclose(m_file);
	mad_frame_finish(&m_frame);
	mad_stream_finish(&m_stream);
	mad_synth_finish(&m_synth);
}

int mad_input::open(song *song)
{
	assert(m_file == NULL);
	m_file = fopen(song->fname.c_str(), "rb");
	if (m_file == NULL)
		return -1;
	fill_readbuf();
	return 0;
}

int mad_input::fill_readbuf()
{
	if (m_stream.next_frame) {
		const char *p =
			reinterpret_cast<const char *>(m_stream.next_frame);
		size_t pos = p - m_readbuf.data();
		m_readbuf.erase(m_readbuf.begin(), m_readbuf.begin() + pos);
	}

	size_t toread = 16384 - m_readbuf.size();

	std::string buf(toread, 0);
	ssize_t ret = fread(&buf[0], 1, toread, m_file);
	if (ret <= 0)
		return -1;
	buf.resize(ret);
	m_readbuf += buf;

	mad_stream_buffer(&m_stream,
		reinterpret_cast<const unsigned char *>(m_readbuf.data()),
		m_readbuf.size());

	return 0;
}

#ifdef NOISE_SHAPING
sample_t mad_input::scale(unsigned int bits, mad_fixed_t sample,
			  audio_dither *dither)
{
{'fraction_non_alphanumeric': 0.15240734326290267, 'fraction_numerical': 0.024246622791825424, 'mean_word_length': 5.639080459770115, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3647413', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1237, 'n_words': 218}