I wanna show 3 .jpg pictures on LCD ,but the program crashed, it seems that the pointer did not init,here is the main() function:
#include <stdio.h>
#include <pthread.h>
int main(int argc,char **argv)
{
lcd_open("/dev/fb0");
int i = 0;
char* pic_path[3] = {"./jpeg/1.jpg","./jpeg/2.jpg","./jpeg/3.jpg"};
while(1)
{
if(i == 3)
{
i = 0;
}
lcd_draw_jpg(0, 0, pic_path[i], NULL, 0, 0);
pthread_testcancel();
sleep(1);
i++;
}
return 0;
}
It just show the 1st picture.
here is the display function Lcd.c
int lcd_draw_jpg(unsigned int x,unsigned int y,const char *pjpg_path,char *pjpg_buf,unsigned int jpg_buf_size,unsigned int jpg_half)
{
/*定义解码对象,错误处理对象*/
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
char *pcolor_buf = g_color_buf
char *pjpg
unsigned int i=0;
unsigned int color =0;
unsigned int count =0;
unsigned int x_s = x;
unsigned int x_e ;
unsigned int y_e ;
int jpg_fd;
unsigned int jpg_size;
unsigned int jpg_width;
unsigned int jpg_height;
//lcd_open("/dev/fb0");
if(pjpg_path!=NULL)
{
/* 申请jpg资源,权限可读可写 */
jpg_fd=open(pjpg_path,O_RDWR);
if(jpg_fd == -1)
{
printf("open %s error\n",pjpg_path);
return -1;
}
/* 获取jpg文件的大小 */
jpg_size=file_size_get(pjpg_path);
/* 为jpg文件申请内存空间 */
pjpg = malloc(jpg_size);
/* 读取jpg文件所有内容到内存 */
read(jpg_fd,pjpg,jpg_size);
}
else
{
jpg_size = jpg_buf_size;
pjpg = pjpg_buf;
}
/*注册出错处理*/
cinfo.err = jpeg_std_error(&jerr);
/*创建解码*/
jpeg_create_decompress(&cinfo);
/*直接解码内存数据*/
jpeg_mem_src(&cinfo,pjpg,jpg_size);
/*读文件头*/
jpeg_read_header(&cinfo, TRUE);
/*开始解码*/
jpeg_start_decompress(&cinfo);
if(jpg_half)
{
x_e = x_s+(cinfo.output_width/2);
y_e = y +(cinfo.output_height/2);
/*读解码数据*/
while(cinfo.output_scanline < cinfo.output_height)
{
pcolor_buf = g_color_buf;
/* 读取jpg一行的rgb值 */
jpeg_read_scanlines(&cinfo,(JSAMPARRAY)&pcolor_buf,1);
/* 再读取jpg一行的rgb值 */
jpeg_read_scanlines(&cinfo,(JSAMPARRAY)&pcolor_buf,1);
for(i=0; i<(cinfo.output_width/2); i++)
{
/* 获取rgb值 */
color = *(pcolor_buf+2);
color = color | *(pcolor_buf+1)<<8;
color = color | *(pcolor_buf)<<16;
/* 显示像素点 */
lcd_draw_point(x,y,color);
pcolor_buf +=6;
x++;
}
/* 换行 */
y++;
x = x_s;
}
}
else
{
x_e = x_s+cinfo.output_width;
y_e = y +cinfo.output_height;
/*读解码数据*/
while(cinfo.output_scanline < cinfo.output_height )
{
pcolor_buf = g_color_buf;
/* read one line's of jpg */
jpeg_read_scanlines(&cinfo,(JSAMPARRAY)&pcolor_buf,1);
for(i=0; i<cinfo.output_width; i++)
{
/* get rgb value */
color = *(pcolor_buf+2);
color = color | *(pcolor_buf+1)<<8;
color = color | *(pcolor_buf)<<16;
/* display pixel point */
lcd_draw_point(x,y,color);
pcolor_buf +=3;
x++;
}
/* 换行 */
y++;
x = x_s;
}
}
/*解码完成*/
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
if(pjpg_path!=NULL)
{
/* 关闭jpg文件 */
close(jpg_fd);
/* 释放jpg文件内存空间 */
free(pjpg);
}
close_lcd();
return 0;
}
this looks like Memory Leak or something else......
these seems a little weird,and
Related
This program tries to simulate FIFO and LRU page replacement. I am trying to implement a simple queue using a dynamically allocated array for the FIFO queue. I want the "page" to be stored in the array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
/*
* Some compile-time constants.
*/
#define REPLACE_NONE 0
#define REPLACE_FIFO 1
#define REPLACE_LRU 2
#define REPLACE_SECONDCHANCE 3
#define REPLACE_OPTIMAL 4
#define TRUE 1
#define FALSE 0
#define PROGRESS_BAR_WIDTH 60
#define MAX_LINE_LEN 100
/*
* Some function prototypes to keep the compiler happy.
*/
int setup(void);
int teardown(void);
int output_report(void);
long resolve_address(long, int);
void error_resolve_address(long, int);
/*
* Variables used to keep track of the number of memory-system events
* that are simulated.
*/
int page_faults = 0;
int mem_refs = 0;
int swap_outs = 0;
int swap_ins = 0;
/*
* Page-table information. You may want to modify this in order to
* implement schemes such as SECONDCHANCE. However, you are not required
* to do so.
*/
struct page_table_entry *page_table = NULL;
struct page_table_entry {
long page_num;
int dirty;
int free;
};
/*
* These global variables will be set in the main() function. The default
* values here are non-sensical, but it is safer to zero out a variable
* rather than trust to random data that might be stored in it -- this
* helps with debugging (i.e., eliminates a possible source of randomness
* in misbehaving programs).
*/
int size_of_frame = 0; /* power of 2 */
int size_of_memory = 0; /* number of frames */
int page_replacement_scheme = REPLACE_NONE;
long *queue;
void add_end(long page_num){
for(int i=0; i<size_of_memory; i++){
if(queue[i] == NULL){
queue[i] = page_num;
break;
}
}
}
long peek_front(){
return queue[0];
}
void remove_front(){
for(int i=0; i<size_of_memory; i++){
queue[i] = queue[i+1];
}
}
// typedef struct page_in_queue page_in_queue;
// struct page_in_queue {
// long page_num;
// page_in_queue *next;
// };
// page_in_queue *new_page(){
// page_in_queue *new_page;
// new_page = (page_in_queue *) malloc(sizeof(page_in_queue));
// new_page->next = NULL;
// return new_page;
// }
// page_in_queue *add_end(page_in_queue *queue, page_in_queue *page){
// page_in_queue *curr;
// if (queue == NULL) {
// page->next = NULL;
// return page;
// }
// for (curr = queue; curr->next != NULL; curr = curr->next);
// curr->next = page;
// page->next = NULL;
// return queue;
// }
// page_in_queue *peek_front(page_in_queue *queue) {
// return queue;
// }
// page_in_queue *remove_front(page_in_queue *queue){
// if (queue == NULL) {
// return NULL;
// }
// page_in_queue *new_front_page = queue->next;
// free(queue);
// return new_front_page;
// }
long *list;
void add(long page_num){
int i;
for (i=0; i<size_of_memory; i++){
list[i] = list[i+1];
}
list[i] = page_num;
}
long peek_least_used(){
return list[0];
}
/*
* Function to convert a logical address into its corresponding
* physical address. The value returned by this function is the
* physical address (or -1 if no physical address can exist for
* the logical address given the current page-allocation state.
*/
long resolve_address(long logical, int memwrite)
{
int i;
long page, frame;
long offset;
long mask = 0;
long effective;
/* Get the page and offset */
page = (logical >> size_of_frame);
for (i=0; i<size_of_frame; i++) {
mask = mask << 1;
mask |= 1;
}
offset = logical & mask;
if (page_replacement_scheme == 2){
add(page);
}
/* Find page in the inverted page table. */
frame = -1;
for ( i = 0; i < size_of_memory; i++ ) {
if (!page_table[i].free && page_table[i].page_num == page) {
frame = i;
break;
}
}
/* If frame is not -1, then we can successfully resolve the
* address and return the result. */
if (frame != -1) {
effective = (frame << size_of_frame) | offset;
return effective;
}
/* If we reach this point, there was a page fault. Find
* a free frame. */
page_faults++;
for ( i = 0; i < size_of_memory; i++) {
if (page_table[i].free) {
frame = i;
break;
}
}
// page_in_queue *temp_page;
// page_in_queue *queue;
long rem_page;
/* If we found a free frame, then patch up the
* page table entry and compute the effective
* address. Otherwise return -1.
*/
if (frame != -1) {
page_table[frame].page_num = page;
page_table[i].free = FALSE;
swap_ins++;
if (page_replacement_scheme == 1){
// temp_page = new_page();
// temp_page->page_num = page;
add_end(page);
}
effective = (frame << size_of_frame) | offset;
return effective;
}
else {
if (page_replacement_scheme == 1){
rem_page = peek_front();
for ( i = 0; i < size_of_memory; i++){
if(page_table[i].page_num == rem_page){
page_table[i].page_num = page;
page_table[i].free = FALSE;
page_table[i].dirty = memwrite;
swap_ins++;
if(page_table[i].dirty == 1){
swap_outs++;
}
frame = i;
break;
}
}
remove_front();
effective = (frame << size_of_frame) | offset;
return effective;
}
if (page_replacement_scheme == 2){
long temp = peek_least_used();
for ( i = 0; i < size_of_memory; i++){
if(page_table[i].page_num == temp){
page_table[i].page_num = page;
page_table[i].free = FALSE;
page_table[i].dirty = memwrite;
swap_ins++;
if(page_table[i].dirty == 1){
swap_outs++;
}
frame = i;
break;
}
}
effective = (frame << size_of_frame) | offset;
return effective;
}
if (page_replacement_scheme == 3){
}
}
}
/*
* Super-simple progress bar.
*/
void display_progress(int percent)
{
int to_date = PROGRESS_BAR_WIDTH * percent / 100;
static int last_to_date = 0;
int i;
if (last_to_date < to_date) {
last_to_date = to_date;
} else {
return;
}
printf("Progress [");
for (i=0; i<to_date; i++) {
printf(".");
}
for (; i<PROGRESS_BAR_WIDTH; i++) {
printf(" ");
}
printf("] %3d%%", percent);
printf("\r");
fflush(stdout);
}
int setup()
{
int i;
page_table = (struct page_table_entry *)malloc(
sizeof(struct page_table_entry) * size_of_memory
);
if (page_table == NULL) {
fprintf(stderr,
"Simulator error: cannot allocate memory for page table.\n");
exit(1);
}
for (i=0; i<size_of_memory; i++) {
page_table[i].free = TRUE;
}
return -1;
}
int teardown()
{
return -1;
}
void error_resolve_address(long a, int l)
{
fprintf(stderr, "\n");
fprintf(stderr,
"Simulator error: cannot resolve address 0x%lx at line %d\n",
a, l
);
exit(1);
}
int output_report()
{
printf("\n");
printf("Memory references: %d\n", mem_refs);
printf("Page faults: %d\n", page_faults);
printf("Swap ins: %d\n", swap_ins);
printf("Swap outs: %d\n", swap_outs);
return -1;
}
int main(int argc, char **argv)
{
/* For working with command-line arguments. */
int i;
char *s;
/* For working with input file. */
FILE *infile = NULL;
char *infile_name = NULL;
struct stat infile_stat;
int line_num = 0;
int infile_size = 0;
/* For processing each individual line in the input file. */
char buffer[MAX_LINE_LEN];
long addr;
char addr_type;
int is_write;
/* For making visible the work being done by the simulator. */
int show_progress = FALSE;
/* Process the command-line parameters. Note that the
* REPLACE_OPTIMAL scheme is not required for A#3.
*/
for (i=1; i < argc; i++) {
if (strncmp(argv[i], "--replace=", 9) == 0) {
s = strstr(argv[i], "=") + 1;
if (strcmp(s, "fifo") == 0) {
page_replacement_scheme = REPLACE_FIFO;
} else if (strcmp(s, "lru") == 0) {
page_replacement_scheme = REPLACE_LRU;
} else if (strcmp(s, "secondchance") == 0) {
page_replacement_scheme = REPLACE_SECONDCHANCE;
} else if (strcmp(s, "optimal") == 0) {
page_replacement_scheme = REPLACE_OPTIMAL;
} else {
page_replacement_scheme = REPLACE_NONE;
}
} else if (strncmp(argv[i], "--file=", 7) == 0) {
infile_name = strstr(argv[i], "=") + 1;
} else if (strncmp(argv[i], "--framesize=", 12) == 0) {
s = strstr(argv[i], "=") + 1;
size_of_frame = atoi(s);
} else if (strncmp(argv[i], "--numframes=", 12) == 0) {
s = strstr(argv[i], "=") + 1;
size_of_memory = atoi(s);
if (page_replacement_scheme == 1){
queue = (long *)malloc(sizeof(long)*size_of_memory);
}
if (page_replacement_scheme == 2){
list = (long *)malloc(sizeof(long)*size_of_memory);
}
} else if (strcmp(argv[i], "--progress") == 0) {
show_progress = TRUE;
}
}
if (infile_name == NULL) {
infile = stdin;
} else if (stat(infile_name, &infile_stat) == 0) {
infile_size = (int)(infile_stat.st_size);
/* If this fails, infile will be null */
infile = fopen(infile_name, "r");
}
if (page_replacement_scheme == REPLACE_NONE ||
size_of_frame <= 0 ||
size_of_memory <= 0 ||
infile == NULL)
{
fprintf(stderr,
"usage: %s --framesize=<m> --numframes=<n>", argv[0]);
fprintf(stderr,
" --replace={fifo|lru|optimal} [--file=<filename>]\n");
exit(1);
}
setup();
while (fgets(buffer, MAX_LINE_LEN-1, infile)) {
line_num++;
if (strstr(buffer, ":")) {
sscanf(buffer, "%c: %lx", &addr_type, &addr);
if (addr_type == 'W') {
is_write = TRUE;
} else {
is_write = FALSE;
}
if (resolve_address(addr, is_write) == -1) {
error_resolve_address(addr, line_num);
}
mem_refs++;
}
if (show_progress) {
display_progress(ftell(infile) * 100 / infile_size);
}
}
teardown();
output_report();
fclose(infile);
exit(0);
}
The file is saved as virtmem.c. This is the makefile:
#
# "makefile" for the virtual-memory simulation.
#
CC=gcc
CFLAGS=-c -Wall -g
all: virtmem
virtmem.o: virtmem.c
$(CC) $(CFLAGS) virtmem.c
virtmem: virtmem.o
$(CC) virtmem.o -o virtmem
clean:
rm -rf *.o virtmem
After running the "make" command, I run the executable with these inputs
./virtmem --framesize=12 --numframes=100 --replace=fifo --file=traces/ls_out.txt --progress
But it is giving a segmentation fault at the conditional "if(queue[i] == NULL)", saying the memory location cannot be accessed. The gdb output is as follows:
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554bea in add_end (page_num=34158723704) at virtmem.c:80
80 if(queue[i] == (long)0){
(gdb) print queue[i]
Cannot access memory at address 0x0
(gdb)
My program contains the next structure:
typedef struct user {
char username[LENGTH_USERNAME];
} User
and I have an array inside my main function of struct user
User user_database[NUMBER_USERS];
The problem is that when I pass this database to a function to edit the username field of all the struct user in the array like this:
void initialize_empty_user_database(User database[])
{
int i;
for(i = 0; i < NUMBER_USERS; ++i)
database[i].username[0] = '\0';
}
when trying to compile, I get the next error:
error: incompatible types when assigning to type ‘User’ {aka ‘struct user’} from type ‘char *’
Here is the complete code:
#include <stdio.h>
#include <string.h>
#define LENGTH_USERNAME 21
#define LENGTH_ACTIVITY_NAME 21
#define LENGTH_TASK_DESCR 51
#define NUMBER_USERS 50
#define NUMBER_ACTIVITIES 10
#define NUMBER_TASKS 10000
typedef struct user {
char username[LENGTH_USERNAME];
/* username cannot contain blank characters */
} User;
typedef struct activity {
char name[LENGTH_ACTIVITY_NAME];
/* name cannot contain lowercase letters */
} Activity;
typedef struct task {
int id;
char des[LENGTH_TASK_DESCR]; /* description */
User username[LENGTH_USERNAME];
Activity activity[LENGTH_ACTIVITY_NAME];
int etc; /* Estimated Time of Completion */
int start_time;
} Task;
void initialize_empty_user_database(User database[])
{
int i;
for(i = 0; i < NUMBER_USERS; ++i)
database[i].username[0] = '\0';
}
void initialize_empty_activity_database(Activity database[])
{
int i;
for(i = 0; i < NUMBER_ACTIVITIES; ++i)
database[i].name[0] = '\0';
}
void initialize_empty_task_database(Task database[])
{
int i;
for(i = 0; i < NUMBER_TASKS; ++i) {
database[i].id = -1; /* id == -1 -> task not initialized */
}
}
void read_task_descr(char target[])
{
int i;
char c;
c = getchar();
for(i = 0; i < (LENGTH_TASK_DESCR - 1) || c == '\n'; ++i){
target[i] = c;
c = getchar();
}
target[i] = '\0';
}
int available_tasks(Task database[])
{
int i;
for(i = 0; i < NUMBER_TASKS; ++i) {
if(database[i].id == -1)
return NUMBER_TASKS - i;
}
return 0;
}
int duplicate_descr(Task database[], char descr[])
{
int i;
for(i = 0; i < NUMBER_TASKS; ++i) {
if (strcmp(database[i].des, descr) == 0)
return 1;
}
return 0;
}
int t_exceptions(Task task_database[], char task_descr[])
{
if (available_tasks(task_database) == 0) {
printf("too many tasks");
return 1;
}
else if (duplicate_descr(task_database, task_descr) == 1) {
printf("duplicate description");
return 2;
}
return 0;
}
void new_task(Task database[], int etc, char descr[])
{
static int i = 0;
database[i].id = i + 1; /* id */
strcpy(database[i].des, descr); /* descr */
database[i].username[0] = '\0'; /* username (not attributed) */
strcpy(database[i].activity.name, "TO DO"); /* activity */
database[i].etc = etc; /* estimated time for completion */
database[i].start_time = 0; /* start time (not started) */
++i;
}
int main()
{
/* Database declarations */
User user_database[NUMBER_USERS];
Activity activity_database[NUMBER_ACTIVITIES] = {{"TO DO"}, {"IN PROGRESS"}, {"DONE"}};
Task task_database[NUMBER_TASKS];
/* Variable and array definition */
int time;
int etc; /* estimated time of completion */
char task_descr[LENGTH_TASK_DESCR];
char activity_name[LENGTH_ACTIVITY_NAME];
char username[LENGTH_USERNAME];
/* Auxiliary variables */
char command;
/* Database initilizations */
initialize_empty_user_database(user_database);
initialize_empty_activity_database(activity_database);
initialize_empty_task_database(task_database);
while(1) {
command = getchar();
switch(command) {
case 'q':
return 0;
case 't':
scanf("%d", &etc);
getchar(); /* Consume space separating arguments */
read_task_descr(task_descr);
if(t_exceptions(task_database, task_descr) == 0)
new_task(task_database, etc, task_descr);
break;
default:
printf("Exception: Unknown command\n");
}
getchar(); /* Consume the newline character */
}
return -1;
}
can somebody explain to me what am I doing wrong?
Thanks!
Errors in the original code are:
main.c: In function ‘new_task’:
main.c:103:31: error: incompatible types when assigning to type ‘User’ {aka ‘struct user’} from type ‘int’
103 | database[i].username[0] = '\0'; /* username (not attributed) */
| ^~~~
main.c:104:32: error: ‘(Activity *)&(database + (sizetype)((long unsigned int)i * 948))->activity’ is a pointer; did you mean to use ‘->’?
104 | strcpy(database[i].activity.name, "TO DO"); /* activity */
| ^
| ->
The error is on this line:
database[i].username[0] = '\0';
This is because username is declared to be a User array:
typedef struct task {
int id;
char des[LENGTH_TASK_DESCR]; /* description */
User username[LENGTH_USERNAME];
Activity activity[LENGTH_ACTIVITY_NAME];
int etc; /* Estimated Time of Completion */
int start_time;
} Task;
That looks like an error in the field declaration and should instead be:
char username[LENGTH_USERNAME];
The expression user_database[i].username[0] is of type char, so you need to assign a char and not a string to it.
user_database[i].username[0] = '\0';
Note the single quotes not double quotes '\0'.
Trying to use SetConsoleScreenBufferSize but it fails and puts "The handle is invalid." In the last error. Will post all code, but here are some highlights:
Using this to resize buffer:
int TGHandleResizeEvent(struct TGHandle *tgHandle, INPUT_RECORD record) {
if (record.EventType == WINDOW_BUFFER_SIZE_EVENT) {
WINDOW_BUFFER_SIZE_RECORD size = record.Event.WindowBufferSizeEvent;
sizeTGDrawBuffer(&tgHandle->drawBuffer, size.dwSize.X, size.dwSize.Y);
clearTGDrawBuffer(&tgHandle->drawBuffer);
COORD bufferNewSize = {
size.dwSize.X,
size.dwSize.Y
};
return SetConsoleScreenBufferSize(&tgHandle->screenBufferHandle, bufferNewSize);
}
}
Using this to allocate handle:
struct TGHandle TG() {
struct TGHandle tgHandle;
tgHandle.screenBufferHandle = CreateConsoleScreenBuffer(
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL
);
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(tgHandle.screenBufferHandle, &info);
tgHandle.drawBuffer = createTGDrawBuffer(info.dwSize.X, info.dwSize.Y);
// Create the input buffer
tgHandle.inputBufferSize = 32;
tgHandle.inputBuffer = malloc(sizeof(INPUT_RECORD) * tgHandle.inputBufferSize);
// Hook up the input handle
tgHandle.inputHandle = GetStdHandle(STD_INPUT_HANDLE);
return tgHandle;
}
Here is full code.
tg.h
#ifndef TG_H
#define TG_H
#include <Windows.h>
#include <memory.h>
#define FOREGROUND_WHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
#define BACKGROUND_WHITE BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
// A drawing buffer, for general purposes
struct TGDrawBuffer {
COORD size;
CHAR_INFO *buffer;
};
struct TGDrawBuffer createTGDrawBuffer(int, int); // Function to allocate a drawing buffer
void sizeTGDrawBuffer(struct TGDrawBuffer*, int, int); // Resize a draw buffer
void clearTGDrawBuffer(struct TGDrawBuffer*); // Fill a buffer with blank cells
void TGDrawPixel(struct TGDrawBuffer*, int, int, CHAR_INFO); // Draw to a single cell on the buffer
void TGDrawAttribute(struct TGDrawBuffer*, int, int, int); // Modify a single attribute. X, Y, Attr
void TGDrawCharInfoString(struct TGDrawBuffer*, int, int, CHAR_INFO*, int); // X, Y, string, int. Draws to max X
CHAR_INFO* TGCharToCharInfo(char*, int); // Convert basic characters to CHAR_INFO. String, length.
void TGDrawString(struct TGDrawBuffer*, int, int, char*, int); // X, Y, string, length. Draws to max X
void freeTGDrawBuffer(struct TGDrawBuffer*); // Function to de-allocate a drawing buffer
int CharInfoStrlen(CHAR_INFO*); // Get length of a CHAR_INFO as if it were a string
// Essentially a drawing context to the screen
struct TGHandle {
HANDLE screenBufferHandle, inputHandle;
struct TGDrawBuffer drawBuffer;
INPUT_RECORD *inputBuffer;
int inputBufferSize;
};
struct TGHandle TG(); // Initialization function, which returns a drawing context to the screen
void useTGHandle(struct TGHandle*); // Make a screen drawing context active
void updateTGHandle(struct TGHandle*); // Displays what has been drawn
void setTGHandleCursorVisibility(struct TGHandle*, int); // True / False
int getTGInput(struct TGHandle*, INPUT_RECORD*, int); // Fill input into a buffer
int getTGNextInput(struct TGHandle*, INPUT_RECORD*); // Get a single INPUT_RECORD or return false
int TGHandleResizeEvent(struct TGHandle*, INPUT_RECORD); // Resize is not handled automatically
#endif
tg.c
#include "tg.h"
#include <string.h>
struct TGDrawBuffer createTGDrawBuffer(int width, int height) {
struct TGDrawBuffer tgDrawBuffer;
tgDrawBuffer.buffer = NULL; // Init the buffer to NULL
sizeTGDrawBuffer(&tgDrawBuffer, width, height);
return tgDrawBuffer;
}
void sizeTGDrawBuffer(struct TGDrawBuffer* drawBuffer, int width, int height) {
// Using free/ malloc here because we aren't interested in retaining data
if (drawBuffer->buffer) {
free(drawBuffer->buffer);
}
drawBuffer->buffer = malloc(sizeof(CHAR_INFO) * (width * height));
// Copy the size to the buffer record
drawBuffer->size.X = width;
drawBuffer->size.Y = height;
}
void clearTGDrawBuffer(struct TGDrawBuffer *tgBuffer) {
int i = 0, limit = tgBuffer->size.X * tgBuffer->size.Y;
// Create a blank CHAR_INFO
CHAR_INFO clearChar;
clearChar.Char.AsciiChar = ' ';
clearChar.Char.UnicodeChar = ' ';
clearChar.Attributes = FOREGROUND_WHITE; // Would be confusing without this
// Set everything to that buffer
while (i < limit) {
tgBuffer->buffer[i] = clearChar;
i++;
}
}
void TGDrawPixel(struct TGDrawBuffer *tgBuffer, int x, int y, CHAR_INFO character) {
tgBuffer->buffer[(tgBuffer->size.X * y) + x] = character;
}
void TGDrawAttribute(struct TGDrawBuffer *tgBuffer, int x, int y, int attr) {
tgBuffer->buffer[(tgBuffer->size.X * y) + x].Attributes = attr;
}
void TGDrawCharInfoString(struct TGDrawBuffer *tgDrawBuffer, int x, int y, CHAR_INFO *string, int length) {
int charsToWrite = length;
int distanceToEnd = (tgDrawBuffer->size.Y - 1) - y;
if (distanceToEnd < charsToWrite)
distanceToEnd = charsToWrite;
int startPos = x + (tgDrawBuffer->size.X * y);
int i = 0;
while (i < distanceToEnd) {
tgDrawBuffer->buffer[startPos + x] = string[i];
i++;
}
}
CHAR_INFO* TGCharToCharInfo(char* string, int length) {
if (length == -1)
length = strlen(string);
// TODO
}
void TGDrawString(struct TGDrawBuffer *tgDrawBuffer, int x, int y, char *string, int length) {
int charsToWrite = length;
int distanceToEnd = (tgDrawBuffer->size.Y - 1) - y;
if (distanceToEnd < charsToWrite)
charsToWrite = distanceToEnd;
int startPos = x + (tgDrawBuffer->size.X * y);
int i = 0;
while (i < charsToWrite) {
tgDrawBuffer->buffer[startPos + i].Char.AsciiChar = string[i];
tgDrawBuffer->buffer[startPos + i].Char.UnicodeChar = string[i];
i++;
}
}
void freeTGDrawBuffer(struct TGDrawBuffer *drawBuffer) {
free(drawBuffer->buffer);
}
struct TGHandle TG() {
struct TGHandle tgHandle;
tgHandle.screenBufferHandle = CreateConsoleScreenBuffer(
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL
);
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(tgHandle.screenBufferHandle, &info);
tgHandle.drawBuffer = createTGDrawBuffer(info.dwSize.X, info.dwSize.Y);
// Create the input buffer
tgHandle.inputBufferSize = 32;
tgHandle.inputBuffer = malloc(sizeof(INPUT_RECORD) * tgHandle.inputBufferSize);
// Hook up the input handle
tgHandle.inputHandle = GetStdHandle(STD_INPUT_HANDLE);
return tgHandle;
}
void useTGHandle(struct TGHandle *tgHandle) {
SetConsoleActiveScreenBuffer(tgHandle->screenBufferHandle);
// Update the buffer sizes
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(tgHandle->screenBufferHandle, &info);
sizeTGDrawBuffer(&tgHandle->drawBuffer, info.dwSize.X, info.dwSize.Y);
clearTGDrawBuffer(&tgHandle->drawBuffer);
}
void updateTGHandle(struct TGHandle *tgHandle) {
COORD size = { tgHandle->drawBuffer.size.X, tgHandle->drawBuffer.size.Y }; // Buffer size
COORD pos = { 0, 0 }; // Start of the buffer coord
SMALL_RECT rect = {
.Left = 0,
.Top = 0,
.Right = size.X - 1,
.Bottom = size.Y - 1
}; // Rect to draw to on destination
WriteConsoleOutput(
tgHandle->screenBufferHandle,
tgHandle->drawBuffer.buffer,
size,
pos,
&rect
);
}
void setTGHandleCursorVisibility(struct TGHandle *tgHandle, int visible) {
// Copy the already-available cursor info
CONSOLE_CURSOR_INFO info;
GetConsoleCursorInfo(tgHandle->screenBufferHandle, &info);
// Modify the cursor visibility
info.bVisible = visible;
SetConsoleCursorInfo(tgHandle->screenBufferHandle, &info);
}
// You should be able to use a TGHandle's input buffer rather than creating your own
// for maximum memory conservation
int getTGInput(struct TGHandle *tgHandle, INPUT_RECORD *inputBuffer, int max) {
int availableRecords;
GetNumberOfConsoleInputEvents(tgHandle->inputHandle, &availableRecords);
int amountToRead = max;
if (availableRecords < max) {
amountToRead = availableRecords;
}
int numberRead;
ReadConsoleInput(
tgHandle->inputHandle,
inputBuffer,
amountToRead,
&numberRead
);
return numberRead;
}
// This function should be pretty performant if someone would not like to use
// the above function and mess around with buffers.
// Input record info: https://learn.microsoft.com/en-us/windows/console/input-record-str
int getTGNextInput(struct TGHandle *tgHandle, INPUT_RECORD *record) {
int availableRecords;
GetNumberOfConsoleInputEvents(tgHandle->inputHandle, &availableRecords);
if (availableRecords == 0) {
return 0;
}
ReadConsoleInput(
tgHandle->inputHandle,
tgHandle->inputBuffer,
1,
&availableRecords
);
*record = tgHandle->inputBuffer[0];
return 1;
}
int TGHandleResizeEvent(struct TGHandle *tgHandle, INPUT_RECORD record) {
if (record.EventType == WINDOW_BUFFER_SIZE_EVENT) {
WINDOW_BUFFER_SIZE_RECORD size = record.Event.WindowBufferSizeEvent;
sizeTGDrawBuffer(&tgHandle->drawBuffer, size.dwSize.X, size.dwSize.Y);
clearTGDrawBuffer(&tgHandle->drawBuffer);
COORD bufferNewSize = {
size.dwSize.X,
size.dwSize.Y
};
return SetConsoleScreenBufferSize(&tgHandle->screenBufferHandle, bufferNewSize);
}
}
test.c
#include "tg.h"
#include <time.h>
#include <stdio.h>
void getMessageAsStr(char *buf) {
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), 0,
buf, 256, NULL);
}
int main() {
// Error buffer
char buf[256];
// Create a drawing context to the screen
struct TGHandle context = TG();
useTGHandle(&context);
setTGHandleCursorVisibility(&context, 0); // Hide the cursor of course
struct TGDrawBuffer *buffer = &context.drawBuffer;
// Create a CHAR_INFO to draw with
CHAR_INFO info;
info.Attributes = BACKGROUND_BLUE | FOREGROUND_WHITE;
info.Char.AsciiChar = ' ';
info.Char.UnicodeChar = ' ';
INPUT_RECORD input;
const int STRING_BUF_SIZE = 64;
char *fpsCountBuffer = malloc(sizeof(char) * STRING_BUF_SIZE);
long start, end;
start = QueryPerformanceCounter(&start);
int running = 1;
while (running) {
// Start off with a nice clean slate
//clearTGDrawBuffer(buffer);
// Collect input to react to resize
while (getTGNextInput(&context, &input)) {
if (input.EventType == WINDOW_BUFFER_SIZE_EVENT) {
if (!TGHandleResizeEvent(&context, input)) {
OutputDebugString("Couldn't resize:\n");
getMessageAsStr(buf);
OutputDebugString(buf);
}
}
}
// Draw line along top and bottom
int i = 0;
while (i < buffer->size.X) {
TGDrawPixel(buffer, i, 0, info);
TGDrawPixel(buffer, i, buffer->size.Y - 1, info);
i++;
}
i = 0;
// Draw vertical lines
while (i < buffer->size.Y) {
TGDrawPixel(buffer, 0, i, info);
TGDrawPixel(buffer, buffer->size.X - 1, i, info);
i++;
}
// FPS count!
// Get time elapsed in millis
QueryPerformanceCounter(&end);
long fps = 1000000 / (end - start);
// Put it into the screen buffer
snprintf(fpsCountBuffer, STRING_BUF_SIZE, "Running at %ldhz, %dx%d", fps, buffer->size.X, buffer->size.Y);
TGDrawString(buffer, 1, 1, fpsCountBuffer, strlen(fpsCountBuffer));
start = end;
updateTGHandle(&context);
}
}
Spotted it right after I posted it. You can see that I'm taking the pointer location of the handle in TGHandleResizeEvent:
return SetConsoleScreenBufferSize(&tgHandle->screenBufferHandle, bufferNewSize);
This is, in fact, an invalid handle. Corrected code:
return SetConsoleScreenBufferSize(tgHandle->screenBufferHandle, bufferNewSize);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have to speed up searching of a MAC address in an array (size: 32k). I would like to get better performance out of it, I wrote a little example code to show the problem (mind that the MACs in the array are going to be random numbers (random ports, random vlans) and not nicely ordered (as displayed in the example code).
Now I'm looking for suggestions how I can improve that i.e. speed it up:
#include <stdio.h>
#include <string.h>
#define MAX_MAC 32768
typedef unsigned char l2_mac_t[6];
typedef struct l2_s {
int prt;
int vln;
l2_mac_t mac;
}l2_t;
int find_mac(int port, int vlan, l2_mac_t mac);
void fill_mac(void);
static l2_t arr[MAX_MAC] = {0};
int main (void) {
int i = 0;
int res = 0;
fill_mac();
for (i=0;i<MAX_MAC;i++) {
res = find_mac(arr[i].prt,arr[i].vln,arr[i].mac);
if (res%1000 == 0 )
printf("Got MAC %d\n",res);
}
}
int find_mac(int port, int vlan, l2_mac_t mac) {
int i = 0;
for (int i = 0;i< MAX_MAC; i++) {
if (arr[i].prt == port) {
if (arr[i].vln == vlan) {
if (memcmp(arr[i].mac,mac,sizeof(l2_mac_t)) == 0 ) {
//found
return i;
}
}
}
}
}
void fill_mac(void) {
int i = 0;
for (i=0;i<MAX_MAC; i++) {
arr[i].prt = 4;
arr[i].vln = 10;
arr[i].mac[5] = i%255;
arr[i].mac[4] = i%65025;
}
}
Below is some edited code after getting some comments:
Okay,
I was going to use a hash and came up with the below (which gives me a segfault as it doesn't want to allocate this much memory in init()). Plus, this feels kind of like using a sledge hammer at it, there must be a better way to hash this than than the below MacSum(), any suggestions are welcome!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_MAC 32768
#define MacSum(x) (x)[0]*(2^24) \
+(x)[1]*(2^20) \
+(x)[2]*(2^16) \
+(x)[3]*(2^12)\
+(x)[4]*(2^8)\
+(x)[5]
typedef unsigned char l2_mac_t[6];
typedef struct l2_s {
int prt;
int vln;
l2_mac_t mac;
}l2_t;
static unsigned short *L2Hash=0;
int find_mac(int port, int vlan, l2_mac_t mac);
void fill_mac(void);
void init(void);
static l2_t arr[MAX_MAC] = {0};
int main (void) {
int i = 0;
int res = 0;
init();
fill_mac();
for (i=0;i<MAX_MAC;i++) {
res = find_mac(arr[i].prt,arr[i].vln,arr[i].mac);
/*if (res%1000 == 0 )
printf("Got MAC %d\n",res);*/
}
}
int find_mac(int port, int vlan, l2_mac_t mac) {
int i = 0;
int key = 0;
key = MacSum(mac);
if (memcmp(arr[key].mac,mac,sizeof(l2_mac_t)) == 0 ) {
return key;
} else {
for (int i = 0;i< MAX_MAC; i++) {
if (arr[i].prt == port) {
if (arr[i].vln == vlan) {
if (memcmp(arr[i].mac,mac,sizeof(l2_mac_t)) == 0 ) {
return i;
}
}
}
}
}
}
void fill_mac(void) {
int i = 0;
int key = 0;
for (i=0;i<MAX_MAC; i++) {
arr[i].prt = 4;
arr[i].vln = 10;
arr[i].mac[5] = i%255;
arr[i].mac[4] = i%65025;
key = MacSum(arr[i].mac);
L2Hash[key] = i;
}
}
void init(void) {
static int init = 0;
if (init)
return;
L2Hash = (unsigned short*) malloc(0xffffffffffff*sizeof(unsigned short));
}
For a further update to the question, scroll down to the second answer
stylistic note: nested if()s are hard to read. Some people prefer:
int find_mac(int port, int vlan, l2_mac_t mac) {
int i = 0;
for (int i = 0;i< MAX_MAC; i++) {
if (arr[i].prt != port) continue;
if (arr[i].vln != vlan) continue;
if (memcmp(arr[i].mac,mac,sizeof(l2_mac_t)) continue;
//found
return i;
}
return WHAT; //!!11!!1
}
[this should be a comment, but I needed the formatting.]
I followd some of the above suggestions and came up with the below code. I've now reduced the number of MACs to 1000 but I already get some:Could not find MAC messages.
Anybody able to assist me here?
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_MAC 1000
#define SHORT_INIT 0xFFFF
#define MacSum(x) (x)[0]*(2^24) \
+(x)[1]*(2^20) \
+(x)[2]*(2^16) \
+(x)[3]*(2^12)\
+(x)[4]*(2^8)\
+(x)[5]
typedef unsigned char l2_mac_t[6];
typedef struct l2_s {
int prt;
int vln;
l2_mac_t mac;
}l2_t;
static unsigned short l2hash[MAX_MAC]={0};
int find_mac(int port, int vlan, l2_mac_t mac);
void fill_mac_tab(void);
void init(void);
void mac_hash_add (int idx, l2_mac_t mac);
static l2_t arr[MAX_MAC] = {0};
//---------------------------------------------------------------------
int main (void) {
int i = 0;
int res = 0;
init();
fill_mac_tab();
for (i=0;i<MAX_MAC;i++) {
res = find_mac(arr[i].prt,arr[i].vln,arr[i].mac);
}
}
//---------------------------------------------------------------------
void init(void) {
int i = 0;
for (i=0;i<MAX_MAC;i++)
l2hash[i] = SHORT_INIT;
}
//---------------------------------------------------------------------
int find_mac(int port, int vlan, l2_mac_t mac) {
int i = 0;
int k = 0;
k = (MacSum(mac))%MAX_MAC;
if (memcmp(arr[k].mac,mac,sizeof(l2_mac_t)) == 0 ) {
printf("Found MAC %02X:%02X:%02X:%02X:%02X:%02X at key %d\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5],k);
return k;
} else {
for (int i = k;i< MAX_MAC; i++ ) {
if (arr[i].prt != port ) continue;
if (arr[i].vln != vlan ) continue;
if (memcmp( arr[i].mac,mac,sizeof(l2_mac_t) )) continue;
printf("Found MAC %02X:%02X:%02X:%02X:%02X:%02X\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
return i;
}
}
printf("Could not find MAC %02X:%02X:%02X:%02X:%02X:%02X\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
return -1;
}
//---------------------------------------------------------------------
void fill_mac_tab(void) {
int i = 0;
int o = 0;
int key = 0;
for (i=0;i<MAX_MAC; i++) {
// fill table
arr[i].prt = 4;
arr[i].vln = 10;
arr[i].mac[5] = i%255;
if (i>255)
arr[i].mac[4] = i%65025;
mac_hash_add(i,arr[i].mac);
}
}
void mac_hash_add (int idx, l2_mac_t mac) {
int i = 0;
int o = 0;
int k = 0;
k = (MacSum(arr[idx].mac))%MAX_MAC;
printf("k %d\n",k);
if(l2hash[k] == SHORT_INIT ) {
l2hash[k] = i;
} else {
printf("k %d already used, find next\n",k);
// find next empty spot in hash
for (o=k; o<MAX_MAC; o++) {
if (l2hash[o] != SHORT_INIT ) continue;
printf("using %d\n",o);
l2hash[o] = i;
return;
}
printf("unable to find empty key within range \n");
}
}
a working solution of the above looks like:
It still takes a significant amount of time but it's much better than linear searching through the complete array for every MAC every time.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_MAC 32768
#define SHORT_INIT 0xFFFF
#define OK 0
#define ERROR -1
#define MacSum(x) (x)[0]*(2^24) \
+(x)[1]*(2^20) \
+(x)[2]*(2^16) \
+(x)[3]*(2^12)\
+(x)[4]*(2^8)\
+(x)[5]
typedef unsigned char l2_mac_t[6];
typedef struct l2_s {
int prt;
int vln;
l2_mac_t mac;
}l2_t;
static unsigned short l2hash[MAX_MAC]={0};
int find_mac(int port, int vlan, l2_mac_t mac);
int fill_mac_tab(void);
void init(void);
int mac_hash_add (int idx, l2_mac_t mac);
static l2_t arr[MAX_MAC] = {0};
//---------------------------------------------------------------------
int main (void) {
int i = 0;
int rv = OK;
init();
printf("insert\n");
rv = fill_mac_tab();
if (rv) {
printf("ERROR: fill_mac_tab() returned %d\n",rv);
exit (rv);
}
printf("find\n");
for (i=0;i<MAX_MAC;i++) {
rv = find_mac(arr[i].prt,arr[i].vln,arr[i].mac);
if (rv <0) {
printf("ERROR: find_mac() returned %d\n",rv);
exit(rv);
}
}
}
//---------------------------------------------------------------------
void init(void) {
int i = 0;
for (i=0;i<MAX_MAC;i++)
l2hash[i] = SHORT_INIT;
}
//---------------------------------------------------------------------
int find_mac(int port, int vlan, l2_mac_t mac) {
int i = 0;
int k = 0;
k = (MacSum(mac))%MAX_MAC;
if (memcmp(arr[k].mac,mac,sizeof(l2_mac_t)) == 0 ) {
//printf("Found MAC %02X:%02X:%02X:%02X:%02X:%02X at key %d\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5],k);
return k;
} else {
for (int i = k;i< MAX_MAC; i++ ) {
if (arr[i].prt != port ) continue;
if (arr[i].vln != vlan ) continue;
if (memcmp( arr[i].mac,mac,sizeof(l2_mac_t) )) continue;
//printf("Found MAC %02X:%02X:%02X:%02X:%02X:%02X\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
return i;
}
//printf("continue search from bottom\n");
for (int i = 0;i< k; i++ ) {
if (arr[i].prt != port ) continue;
if (arr[i].vln != vlan ) continue;
if (memcmp( arr[i].mac,mac,sizeof(l2_mac_t) )) continue;
//printf("Found MAC %02X:%02X:%02X:%02X:%02X:%02X\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
return i;
}
}
printf("Could not find MAC %02X:%02X:%02X:%02X:%02X:%02X\n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
return ERROR;
}
//---------------------------------------------------------------------
int fill_mac_tab(void) {
int i = 0;
int o = 0;
int key = 0;
int rv = OK;
for (i=0;i<MAX_MAC; i++) {
// fill table
arr[i].prt = 4;
arr[i].vln = 10;
arr[i].mac[5] = i%255;
if (i>255)
arr[i].mac[4] = i%65025;
rv = mac_hash_add(i,arr[i].mac);
}
return rv;
}
int mac_hash_add (int idx, l2_mac_t mac) {
int i = 0;
int o = 0;
int k = 0;
int rv = OK;
k = (MacSum(arr[idx].mac))%MAX_MAC;
//printf("k %d\n",k);
if(l2hash[k] == SHORT_INIT ) {
l2hash[k] = i;
} else {
//printf("k %d already used, find next\n",k);
// find next empty spot in hash
for (o=k; o<MAX_MAC; o++) {
if (l2hash[o] != SHORT_INIT ) continue;
//printf("using %d\n",o);
l2hash[o] = i;
return OK;
}
//printf("Continue search in bottom half\n");
for (o=0; o<k; o++) {
if (l2hash[o] != SHORT_INIT ) continue;
//printf("using %d\n",o);
l2hash[o] = i;
return OK;
}
//printf("unable to find empty key within range \n");
rv = ERROR;
}
return rv;
}
Running my program, I appear to not be writing the correct amount of frames according to the index.
$ ./test
Now recording!! Please speak into the microphone.
index = 0
Writing to: test.flac
audio.h:
#include <stdint.h>
#include <string.h>
typedef struct
{
uint32_t duration;
uint16_t format_type;
uint16_t number_of_channels;
uint32_t sample_rate;
uint32_t frameIndex; /* Index into sample array. */
uint32_t maxFrameIndex;
char* recordedSamples;
} AudioData;
int recordFLAC(AudioData* data, const char *fileName);
AudioData* initAudioData(uint32_t sample_rate, uint16_t channels, uint32_t duration);
capture.c:
#include <stdio.h>
#include <stdlib.h>
#include <portaudio.h>
#include <sndfile.h>
#include "audio.h"
AudioData* initAudioData(uint32_t sample_rate, uint16_t channels, uint32_t duration)
{
AudioData* data = malloc(sizeof(*data));
if (!data) return NULL;
data->duration = duration;
data->format_type = 1;
data->number_of_channels = channels;
data->sample_rate = sample_rate;
data->frameIndex = 0;
data->maxFrameIndex = sample_rate * duration;
data->recordedSamples = malloc(sizeof(data->maxFrameIndex));
if(!data->maxFrameIndex) return NULL;
return data;
}
static int recordCallback(const void *inputBuffer, void *outputBuffer, unsigned long frameCount, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
{
AudioData* data = (AudioData*)userData;
const char* buffer_ptr = (const char*)inputBuffer;
char* index_ptr = &data->recordedSamples[data->frameIndex];
(void) outputBuffer;
(void) timeInfo;
(void) statusFlags;
long framesToCalc;
long i;
int finished;
unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
if(framesLeft < frameCount){
framesToCalc = framesLeft;
finished = paComplete;
}else{
framesToCalc = frameCount;
finished = paContinue;
}
if(!inputBuffer){
for(i = 0; i < framesToCalc; i++){
*index_ptr++ = 0;
}
}else{
for(i = 0; i < framesToCalc; i++){
*index_ptr++ = *buffer_ptr++;
}
}
data->frameIndex += framesToCalc;
return finished;
}
int recordFLAC(AudioData* data, const char *fileName)
{
PaStreamParameters inputParameters;
PaStream* stream;
int err = 0;
int totalFrames = data->maxFrameIndex;
int numSamples;
int numBytes;
char max, val;
double average;
numSamples = totalFrames * data->number_of_channels;
numBytes = numSamples;
data->recordedSamples = malloc(numBytes);
if(!data->recordedSamples)
{
printf("Could not allocate record array.\n");
goto done;
}
for(int i = 0; i < numSamples; i++) data->recordedSamples[i] = 0;
if((err = Pa_Initialize())) goto done;
inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
if (inputParameters.device == paNoDevice) {
fprintf(stderr,"Error: No default input device.\n");
goto done;
}
inputParameters.channelCount = data->number_of_channels; /* stereo input */
inputParameters.sampleFormat = data->format_type;
inputParameters.suggestedLatency = Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;
/* Record some audio. -------------------------------------------- */
err = Pa_OpenStream(&stream, &inputParameters, NULL, data->sample_rate, paFramesPerBufferUnspecified, paClipOff, recordCallback, &data);
if(err) goto done;
if((err = Pa_StartStream(stream))) goto done;
puts("Now recording!! Please speak into the microphone.");
while((err = Pa_IsStreamActive(stream)) == 1)
{
Pa_Sleep(1000);
printf("index = %d\n", data->frameIndex);
}
if( err < 0 ) goto done;
err = Pa_CloseStream(stream);
if(err) goto done;
/* Measure maximum peak amplitude. */
max = 0;
average = 0.0;
for(int i = 0; i < numSamples; i++)
{
val = data->recordedSamples[i];
val = abs(val);
if( val > max )
{
max = val;
}
average += val;
}
average /= (double)numSamples;
done:
Pa_Terminate();
if(err)
{
fprintf(stderr, "An error occured while using the portaudio stream\n");
fprintf(stderr, "Error number: %d\n", err);
fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(err));
err = 1; /* Always return 0 or 1, but no other return codes. */
}
else
{
SF_INFO sfinfo;
sfinfo.channels = 1;
sfinfo.samplerate = data->sample_rate;
sfinfo.format = SF_FORMAT_FLAC | SF_FORMAT_PCM_16;
// open to file
printf("Writing to: %s\n", fileName);
SNDFILE * outfile = sf_open(fileName, SFM_WRITE, &sfinfo);
if (!outfile) return -1;
// prepare a 3 second long buffer (sine wave)
const int size = data->sample_rate * 3;
// write the entire buffer to the file
sf_write_raw(outfile, data->recordedSamples, size);
// force write to disk
sf_write_sync(outfile);
// don't forget to close the file
sf_close(outfile);
}
return err;
}
I'm not quite sure where I am going wrong, I know I need to be writing more frames. Any suggestions?
There seems to be something wrong with your assumptions about the sample format. In the callback you are using char * (single bytes) for the sample format, but in your libsndfile call you're opening a 16 bit file with SF_FORMAT_PCM_16.
This is not so good:
data->format_type = 1;
I recommend using one of the symbolic constants in the PortAudio library for sample formatting. Maybe you want a 16 bit one? And if so, you want to be using short* not char* in the PA callback.
Finally, if your channel count is not 1, the copy loops are incorrect:
for(i = 0; i < framesToCalc; i++){
*index_ptr++ = 0;
}
A "frame" contains data for all channels, so for example, if it's a stereo input your iteration needs to deal with both left and right channels like this:
for(i = 0; i < framesToCalc; i++){
*index_ptr++ = 0; // left
*index_ptr++ = 0; // right
}
Same for the other loops.