<commit_before>/*!
#
# Win-Widget. Windows related software for Audio-Widget/SDR-Widget (http://code.google.com/p/sdr-widget/)
# Copyright (C) 2012 <NAME>
#
# Permission to copy, use, modify, sell and distribute this software 
# is granted provided this copyright notice appears in all copies. 
# This software is provided "as is" without express or implied
# warranty, and with no claim as to its suitability for any purpose.
#
#----------------------------------------------------------------------------
# Contact: <EMAIL>
#----------------------------------------------------------------------------
*/

#pragma once
#include "targetver.h"
#include <windows.h>
#include <string.h>
#include <tchar.h>
#include <math.h>

#ifdef _ENABLE_TRACE
extern void debugPrintf(const _TCHAR *szFormat, ...);
#endif

#define packetPerTransferDAC	16
#define packetPerTransferADC	16
#define packetPerTransferFb		8

class USBAudioDevice;

typedef void (*FillDataCallback)(void* context, UCHAR *buffer, int& len);

class Mutex 
{ 
protected: 
	CRITICAL_SECTION cs;

public:
	inline void Enter() { EnterCriticalSection(&cs); }
	inline void Leave() { LeaveCriticalSection(&cs); }

	Mutex() { InitializeCriticalSection(&cs); }
	~Mutex() { DeleteCriticalSection(&cs); } 
};

class FeedbackInfo
{
	Mutex m_guard;
	float cur_value;

	float playback_value; // BSB 20121222 added default sample rate value for playback

	float max_value;
	float min_value;
#ifdef _ENABLE_TRACE
	float last_value;
#endif
	float interval;
public:
	FeedbackInfo()
	{
		cur_value = 0.f;
#ifdef _ENABLE_TRACE
		last_value = 0.f;
#endif
		interval = 1.f;
	}

	void SetIntervalValue(float value)
	{
		interval = 1000.f * value; //for output real freq
	}

	void SetDefaultValue(float feedbackValue)
	{
		m_guard.Enter();
		cur_value = feedbackValue;
#ifdef _ENABLE_TRACE
		last_value = feedbackValue;
		playback_value = feedbackValue; // BSB 20121222 added
		debugPrintf("ASIOUAC: Set default value: %f\n", interval * feedbackValue);
#endif
		m_guard.Leave();
	}
	void SetValue(int feedbackValue)
	{
		m_guard.Enter();

		float newValue = (float)feedbackValue / 65536.0f;

#ifdef _ENABLE_TRACE
		if(newValue != 0.f && fabs(last_value - newValue)/newValue > 0.5f / interval)
			//(int)(10*last_value) != (int)(10*newValue))
//			debugPrintf("ASIOUAC: Set fb value: %f (raw = %d, curVal = %f)\n", interval * newValue, feedbackValue, interval * cur_value);
			debugPrintf("ASIOUAC: Set fb value: %f (raw = %d, cur_value = %f, playback_value = %f)\n", interval * newValue, feedbackValue, interval * cur_value, interval * playback_value);

		last_value = newValue;
#endif
		cur_value = newValue;

		if(max_value == 0.f || max_value < cur_value)
			max_value = cur_value;
		if(min_value == 0.f || min_value > cur_value)
			min_value = cur_value;
		m_guard.Leave();
	}

	float GetValue()
	{
		float retVal;
		m_guard.Enter();
		retVal = cur_value;
		m_guard.Leave();
		return retVal;
	}

	float GetFreqValue()
	{
		float retVal;
		m_guard.Enter();
		retVal = interval * cur_value;<commit_msg>Support for both 15.17 and 16.16 feedback<commit_after>/*!
#
# Win-Widget. Windows related software for Audio-Widget/SDR-Widget (http://code.google.com/p/sdr-widget/)
# Copyright (C) 2012 <NAME>
#
# Permission to copy, use, modify, sell and distribute this software 
# is granted provided this copyright notice appears in all copies. 
# This software is provided "as is" without express or implied
# warranty, and with no claim as to its suitability for any purpose.
#
#----------------------------------------------------------------------------
# Contact: <EMAIL>
#----------------------------------------------------------------------------
*/

#pragma once
#include "targetver.h"
#include <windows.h>
#include <string.h>
#include <tchar.h>
#include <math.h>

#ifdef _ENABLE_TRACE
extern void debugPrintf(const _TCHAR *szFormat, ...);
#endif

#define packetPerTransferDAC	16
#define packetPerTransferADC	16
#define packetPerTransferFb		8

class USBAudioDevice;

typedef void (*FillDataCallback)(void* context, UCHAR *buffer, int& len);

class Mutex 
{ 
protected: 
	CRITICAL_SECTION cs;

public:
	inline void Enter() { EnterCriticalSection(&cs); }
	inline void Leave() { LeaveCriticalSection(&cs); }

	Mutex() { InitializeCriticalSection(&cs); }
	~Mutex() { DeleteCriticalSection(&cs); } 
};

class FeedbackInfo
{
	Mutex m_guard;
	float cur_value;

	float playback_value; // BSB 20121222 added default sample rate value for playback

	float max_value;
	float min_value;
#ifdef _ENABLE_TRACE
	float last_value;
#endif
	float interval;
public:
	FeedbackInfo()
	{
		cur_value = 0.f;
#ifdef _ENABLE_TRACE
		last_value = 0.f;
#endif
		interval = 1.f;
	}

	void SetIntervalValue(float value)
	{
		interval = 1000.f * value; //for output real freq
	}

	void SetDefaultValue(float feedbackValue)
	{
		m_guard.Enter();
		cur_value = feedbackValue;
#ifdef _ENABLE_TRACE
		last_value = feedbackValue;
		playback_value = feedbackValue; // BSB 20121222 added
		debugPrintf("ASIOUAC: Set default value: %f\n", interval * feedbackValue);
#endif
		m_guard.Leave();
	}
	void SetValue(int feedbackValue)
	{
		m_guard.Enter();

		// BSB 20121222 revised to support both 16.16 and 15.17 feedback
		float newValue = (float)feedbackValue / 32768.0f;	// Must divide by 32768.0f when FW uses 16.16 samples / 125s microframe

		if (newValue > playback_value * 1.5) {				// Must divide by 65536.0f when FW uses 15.17 samples / 125s microframe
			newValue = newValue / 2;						// Inspired by https://git.kernel.org/?p=linux/kernel/git/tiwai/sound.git;a=blob;f=sound/usb/endpoint.c l 1081
		}

#ifdef _ENABLE_TRACE
		if(newValue != 0.f && fabs(last_value - newValue)/newValue > 0.5f / interval)
			//(int)(10*last_value) != (int)(10*newValue))
//			debugPrintf("ASIOUAC: Set fb value: %f (raw = %d, curVal = %f)\n", interval * newValue, feedbackValue, interval * cur_value);
			debugPrintf("ASIOUAC: Set fb value: %f (raw=%d, cur_value=%f, playback_value=%f)\n", interval * newValue, feedbackValue, interval * cur_value, interval * playback_value);

		last_value = newValue;
#endif
		cur_value = newValue;

		if(max_value == 0.f || max_value < cur_value)
			max_value = cur_value;
		if(min_value == 0.f || min_value > cur_value)
			min_value = cur_value;
		m_guard.Leave();
	}

	float GetValue()
	{
		float retVal;
		m_guard.Enter();
		retVal = cur_value;
		m_guard.Leave();
		return retVal;
	}

	float GetFreqValue()
	{
		float retVal;
		m_guard.Enter();
		retVal = interval * cur_value;
{'fraction_non_alphanumeric': 0.1636923076923077, 'fraction_numerical': 0.023230769230769232, 'mean_word_length': 3.4224489795918367, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 20, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '7417713', 'n_tokens_mistral': 2406, 'n_tokens_neox': 2154, 'n_words': 590}