<commit_before>/*

	thunderWave
	
	Basic spectrum analyser for wav files

	T.Lloyd 2016
	

*/
#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <complex.h>
#include "portaudio.h"
#include "wavio.h"
#include "eFFT.h"

#define M_PI 3.141592653589793

typedef struct{
	short int *sin;
	int fs;
	int p;
	int l;
}audioData;
typedef struct{
	GObject *text;
	PaStream *stream;
	audioData a;
	audioSignal as;
	FILE *f;//Audio fileStream
}guiStuff;
typedef struct{
	FILE *f;
	char * path;
	int start;
}graphStuff;
//GObject *label;

static int realTimeCallback(const void *input,void *output,unsigned long frameCount,const PaStreamCallbackTimeInfo* timeInfo,PaStreamCallbackFlags statusFlags,void *callData);
static int bufferedCallback(const void *input,void *output,unsigned long frameCount,const PaStreamCallbackTimeInfo* timeInfo,PaStreamCallbackFlags statusFlags,void *callData);
short int * gensin(double fs,int l,double f);

char * fp="wavs/session.wav";
//char * fp="wavs/5k.wav";

GObject * draw;
GObject * fDraw;

static void playAudio(GtkWidget *widget, gpointer data){
	guiStuff * s = (guiStuff*)data;	
	g_print ("Play Audio!\n");
	//gchar * m = ("MeowMEowMEow");
	//gtk_label_set_text(GTK_LABEL(s->text),m);
	
        //a.sin=gensin(48000.0,48000*2,2000);
	//a.fs=48000;
        //a.p=0; 

	Pa_Initialize();
	/*
	s->as.f=fopen(fp,"r");
	for(int i=0;i<s->as.start;i++){
		fgetc(s->as.f);
	}
	*/
	s->as.p=s->as.start;//realtime
	s->as.p=0;//buffered;
//	readBuffer16(&s->as,fp,s->as.start,s->as.datasize);
	printf("AudioLength=%d\n",s->as.datasize);
	printf("Opening Stream\n");
	//Pa_OpenDefaultStream(&s->stream,0,1,paInt16,s->as.fs,256,realTimeCallback,&s->as);
	Pa_OpenDefaultStream(&s->stream,0,1,paInt16,s->as.fs,256,bufferedCallback,&s->as);
	Pa_StartStream(s->stream);
//	while(Pa_IsStreamActive(s->stream));
	//Pa_StopStream(s->stream);
	//Pa_CloseStream(s->stream);
	//Pa_Terminate();
	
}

static void stopAudio(GtkWidget *widget, gpointer data){
	guiStuff * s = (guiStuff*)data;
	
	//gtk_widget_queue_draw(s->draw);
	
	printf("STOP!");
	Pa_StopStream(s->stream);
	Pa_CloseStream(s->stream);
	Pa_Terminate();
	
}

static void quitProgram(GtkWidget *widget, gpointer data){
	guiStuff *s = (guiStuff*)data;
	Pa_StopStream(s->stream);
	Pa_CloseStream(s->stream);
	Pa_Terminate();
	gtk_main_quit();
}

static gboolean fftCallback(GtkWidget *widget,cairo_t *cr,gpointer data){
	guiStuff * s = (guiStuff*)data;
	//printf("FFT\n");
	int gl=512*4; //FFT/graph length
	double complex *X;
	double * Y=calloc(gl/2,sizeof(double));
	double * f=calloc(gl/2,sizeof(double));
	//short complex x[512];
	complex double *y=calloc(gl,sizeof(complex double));
	//	memcpy(x,s->as.audio16+s->as.p,512*sizeof(short));
	for(int i=0;i<gl;i++){
		y[i]=((complex double)(s->as.audio16[s->as.p+i])/32767.0)+(I*0);
	}
	X=fft(y,gl);
	//printf("%f+i%f",creal(X[0]),cimag(X[0]));
	for(int i=0;i<gl/2;i++){
		f[i]=(((double)i)/((double)gl/2.0))*((double)s->as.fs);
		Y[i]=cabs(X[i]);
		//Y[i]=(double)20*log10(cabs(X[i]));
		if(Y[i]!=0){
//			printf("(%f,%fHz)",Y[i],f[i]);
		}
	}
//	printf("\n");
	//GRAPH PLOTTING
	//printf("%d,%d\n,",f[0],Y[0]);
	graphStuff *b=(graphStuff*)data;
	guint width,height;
	GdkRGBA color;
	GtkStyleContext *context;

	context=gtk_widget_get_style_context(widget);
	width=gtk_widget_get_allocated_width(widget);
	height=gtk_widget_get_allocated_height(widget);

	gtk_render_background(context,cr,0,0,width,height);

	//cairo_new_path(cr);
	cairo_move_to(cr,0,height/2);  
	int diff =(gl/2)/width;

	//printf("%d\n",diff);
	for(int i=0;i<width;i++){
		//cairo_line_to(cr,i,((Y[(i+diff)*width]/(gl/4))*-1*(height-2)/2)+(height/2));
		cairo_line_to(cr,i,((Y[i+diff]))+(height/2));
	}

  	gtk_style_context_get_color (context,
                               gtk_style_context_get_state (context),<commit_msg>spectrum the right way up this time<commit_after>/*

	thunderWave
	
	Basic spectrum analyser for wav files

	T.Lloyd 2016
	

*/
#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <complex.h>
#include "portaudio.h"
#include "wavio.h"
#include "eFFT.h"

#define M_PI 3.141592653589793

typedef struct{
	short int *sin;
	int fs;
	int p;
	int l;
}audioData;
typedef struct{
	GObject *text;
	PaStream *stream;
	audioData a;
	audioSignal as;
	FILE *f;//Audio fileStream
}guiStuff;
typedef struct{
	FILE *f;
	char * path;
	int start;
}graphStuff;
//GObject *label;

static int realTimeCallback(const void *input,void *output,unsigned long frameCount,const PaStreamCallbackTimeInfo* timeInfo,PaStreamCallbackFlags statusFlags,void *callData);
static int bufferedCallback(const void *input,void *output,unsigned long frameCount,const PaStreamCallbackTimeInfo* timeInfo,PaStreamCallbackFlags statusFlags,void *callData);
short int * gensin(double fs,int l,double f);

char * fp="wavs/session.wav";
//char * fp="wavs/5k.wav";

GObject * draw;
GObject * fDraw;

static void playAudio(GtkWidget *widget, gpointer data){
	guiStuff * s = (guiStuff*)data;	
	g_print ("Play Audio!\n");
	//gchar * m = ("MeowMEowMEow");
	//gtk_label_set_text(GTK_LABEL(s->text),m);
	
        //a.sin=gensin(48000.0,48000*2,2000);
	//a.fs=48000;
        //a.p=0; 

	Pa_Initialize();
	/*
	s->as.f=fopen(fp,"r");
	for(int i=0;i<s->as.start;i++){
		fgetc(s->as.f);
	}
	*/
	s->as.p=s->as.start;//realtime
	s->as.p=0;//buffered;
//	readBuffer16(&s->as,fp,s->as.start,s->as.datasize);
	printf("AudioLength=%d\n",s->as.datasize);
	printf("Opening Stream\n");
	//Pa_OpenDefaultStream(&s->stream,0,1,paInt16,s->as.fs,256,realTimeCallback,&s->as);
	Pa_OpenDefaultStream(&s->stream,0,1,paInt16,s->as.fs,256,bufferedCallback,&s->as);
	Pa_StartStream(s->stream);
//	while(Pa_IsStreamActive(s->stream));
	//Pa_StopStream(s->stream);
	//Pa_CloseStream(s->stream);
	//Pa_Terminate();
	
}

static void stopAudio(GtkWidget *widget, gpointer data){
	guiStuff * s = (guiStuff*)data;
	
	//gtk_widget_queue_draw(s->draw);
	
	printf("STOP!");
	Pa_StopStream(s->stream);
	Pa_CloseStream(s->stream);
	Pa_Terminate();
	
}

static void quitProgram(GtkWidget *widget, gpointer data){
	guiStuff *s = (guiStuff*)data;
	Pa_StopStream(s->stream);
	Pa_CloseStream(s->stream);
	Pa_Terminate();
	gtk_main_quit();
}

static gboolean fftCallback(GtkWidget *widget,cairo_t *cr,gpointer data){
	guiStuff * s = (guiStuff*)data;
	//printf("FFT\n");
	int gl=512*4; //FFT/graph length
	double complex *X;
	double * Y=calloc(gl/2,sizeof(double));
	double * f=calloc(gl/2,sizeof(double));
	//short complex x[512];
	complex double *y=calloc(gl,sizeof(complex double));
	//	memcpy(x,s->as.audio16+s->as.p,512*sizeof(short));
	for(int i=0;i<gl;i++){
		y[i]=((complex double)(s->as.audio16[s->as.p+i])/32767.0)+(I*0);
	}
	X=fft(y,gl);
	//printf("%f+i%f",creal(X[0]),cimag(X[0]));
	for(int i=0;i<gl/2;i++){
		f[i]=(((double)i)/((double)gl/2.0))*((double)s->as.fs);
		Y[i]=cabs(X[i]);
		Y[i]=Y[i]/1.0;
		//Y[i]=(double)20*log10(cabs(X[i]));
		if(Y[i]!=0){
//			printf("(%f,%fHz)",Y[i],f[i]);
		}
	}
//	printf("\n");
	//GRAPH PLOTTING
	//printf("%d,%d\n,",f[0],Y[0]);
	graphStuff *b=(graphStuff*)data;
	guint width,height;
	GdkRGBA color;
	GtkStyleContext *context;

	context=gtk_widget_get_style_context(widget);
	width=gtk_widget_get_allocated_width(widget);
	height=gtk_widget_get_allocated_height(widget);

	gtk_render_background(context,cr,0,0,width,height);

	//cairo_new_path(cr);
	cairo_move_to(cr,0,height/2);  
	int diff =(gl/2)/width;

	//printf("%d\n",diff);
	for(int i=0;i<width;i++){
		//cairo_line_to(cr,i,((Y[(i+diff)*width]/(gl/4))*-1*(height-2)/2)+(height/2));
		cairo_line_to(cr,i,(height-((Y[i+diff]))-(height/2)));
	}

  	gtk_style_context_get_color (context,
                               gtk_style_context_get_state (context),
{'fraction_non_alphanumeric': 0.20377503852080123, 'fraction_numerical': 0.028505392912172575, 'mean_word_length': 5.261254019292605, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 77, 'https://': 0, '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': '5953128', 'n_tokens_mistral': 3525, 'n_tokens_neox': 3266, 'n_words': 490}