Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <ctype.h>
- #include "wav.h"
- struct header
- {
- int X, Y, Z;
- long frequency;
- };
- struct partition_info
- {
- int back_part_num, front_part_num, size_x;
- int left_part_num, right_part_num, size_y;
- int down_part_num, up_part_num, size_z;
- };
- typedef struct Plant
- {
- unsigned char node_type;
- double reflection_coeficient;
- double p; /* pressão acústica no nó */
- double pinFront; /* componente de pressão enviada para o vizinho a N */
- double poutFront; /* componente de pressão recebida do vizinho a N */
- double pinBack; /* componente de pressão enviada para o vizinho a S */
- double poutBack; /* etc.. */
- double pinLeft;
- double poutLeft;
- double pinRight;
- double poutRight;
- double pinUp;
- double poutUp;
- double pinDown;
- double poutDown;
- } pl;
- void loadDataRoom();
- void createDataRoom();
- void insert_receiver_or_source(char node_type);
- void insert_cuboid();
- void insert_sphere();
- void room_division();
- void partitioning(int part_counter, int x_block, int y_block, int z_block, int start_x, int start_y, int start_z, int end_x, int end_y, int end_z, int x_blocks, int y_blocks, int z_blocks);
- void loadRoomPartition(int partitionId);
- void loadRoomPlant();
- void loadPartitionPlant(int part_counter);
- void plantMemoryAllocationAndDataReading(FILE *infile);
- void scattering();
- void delay();
- void run();
- double getCoeficient(unsigned char node_type);
- //GLOBAL VARS
- unsigned char ***data;
- pl ***plant;
- float node_spacing;
- struct header input;
- struct partition_info partition;
- int number_samples;
- int16_t *samples = NULL;
- int16_t *result = NULL;
- //Border buffers
- double **backInBuffer;
- double **backOutBuffer;
- double **frontInBuffer;
- double **frontOutBuffer;
- double **leftInBuffer;
- double **leftOutBuffer;
- double **rightInBuffer;
- double **rightOutBuffer;
- double **downInBuffer;
- double **downOutBuffer;
- double **upInBuffer;
- double **upOutBuffer;
- //Sources and receivers - falta permitir múltiplos
- pl *source;
- pl *receiver;
- int main()
- {
- number_samples = wavread("file.wav", &samples);
- int ch = 0, ch1 = 0, partitionId = 0;
- printf("1 - Create Room\n");
- printf("2 - Load data Room\n");
- printf("3 - Sound Simulation\n");
- printf("Option: ");
- scanf("%i", &ch);
- switch (ch)
- {
- case 1:
- createDataRoom();
- break;
- case 2:
- loadDataRoom();
- break;
- case 3:
- loadRoomPlant();
- run();
- break;
- default:
- printf("Invalid option\n");
- }
- printf("\n");
- printf("1 - Room Division\n");
- printf("Option: ");
- scanf("%i", &ch1);
- switch (ch1)
- {
- case 1:
- room_division();
- break;
- case 2:
- printf("Merda\n");
- break;
- default:
- printf("Invalid option\n");
- }
- printf("\n");
- return 0;
- }
- void createDataRoom()
- {
- float width, height, depth;
- char ch;
- FILE *outfile;
- int op = 0;
- outfile = fopen("config.dwm", "w");
- if (outfile == NULL)
- {
- fprintf(stderr, "\nError opening config.dwm\n\n");
- return;
- }
- printf("Dimensions of Room\n");
- do
- {
- printf("Width : ");
- scanf("%f", &width);
- if (width < 0 && !isdigit(width))
- {
- printf("Invalid input\n");
- }
- } while (width < 0);
- do
- {
- printf("Height : ");
- scanf("%f", &height);
- if (height < 0 && !isdigit(height))
- {
- printf("Invalid input\n");
- }
- } while (height < 0);
- do
- {
- printf("Depth : ");
- scanf("%f", &depth);
- if (depth < 0 && !isdigit(depth))
- {
- printf("Invalid input\n");
- }
- } while (depth < 0);
- do
- {
- printf("Frequency\n");
- printf("1-48000Hz\t2-44100Hz\t3-22050Hz\t4-16000Hz\t5-11025Hz\t6-8000Hz\n");
- printf("Frequency: ");
- scanf("%i", &op);
- switch (op)
- {
- case 1:
- input.frequency = 48000;
- break;
- case 2:
- input.frequency = 44100;
- break;
- case 3:
- input.frequency = 22050;
- break;
- case 4:
- input.frequency = 16000;
- break;
- case 5:
- input.frequency = 11025;
- break;
- case 6:
- input.frequency = 8000;
- break;
- default:
- printf("Invalid option\n");
- }
- } while (op < 1 || op > 6);
- node_spacing = ((344 * sqrt(3)) / input.frequency);
- input.X = width / node_spacing;
- input.Y = depth / node_spacing;
- input.Z = height / node_spacing;
- float _w, _h, _d;
- _w = input.X * node_spacing;
- _d = input.Y * node_spacing;
- _h = input.Z * node_spacing;
- printf("Necessary Ajustments: ");
- printf("Width: %.2f Height: %.2f Depth: %.2f Frequency: %ld\n\n", _w, _h, _d, input.frequency);
- fflush(stdin);
- data = (unsigned char ***)malloc(input.X * sizeof(unsigned char **));
- for (int i = 0; i < input.X; i++)
- {
- data[i] = (unsigned char **)malloc(input.Y * sizeof(unsigned char *));
- for (int j = 0; j < input.Y; j++)
- {
- data[i][j] = (unsigned char *)malloc(input.Z * sizeof(unsigned char));
- }
- }
- unsigned char node_type;
- double r;
- do
- {
- printf("Node type:\n");
- scanf("%c", &node_type);
- r = getCoeficient(node_type);
- printf("Absorption coefficient (ρ) = %f\n", r);
- if (r == -1)
- {
- printf("Invalid input\n");
- }
- } while (r == -1);
- for (int i = 0; i < input.X; i++)
- {
- for (int j = 0; j < input.Y; j++)
- {
- for (int k = 0; k < input.Z; k++)
- {
- if (i == 0 || j == 0 || k == 0 || i == (input.X - 1) || j == (input.Y - 1) || k == (input.Z - 1))
- {
- data[i][j][k] = node_type;
- }
- else
- {
- data[i][j][k] = ' ';
- }
- }
- }
- }
- int has_receiver, has_source;
- do
- {
- printf("Node type in Room\n");
- printf("Receiver - R || Source - S || C - Cuboid || E - Sphere\n");
- printf("0 - Exit: \n");
- scanf(" %c", &ch);
- switch (ch)
- {
- case 'R':
- has_receiver = 1;
- insert_receiver_or_source('R');
- break;
- case 'S':
- has_source = 1;
- insert_receiver_or_source('S');
- break;
- case 'C':
- insert_cuboid();
- break;
- case 'E':
- insert_sphere();
- break;
- case '0':
- printf("Room Created");
- break;
- default:
- printf("Invalid option\n");
- }
- } while (ch != '0' || has_source != 1 || has_receiver != 1);
- fprintf(outfile, "%d %d %d %ld\n", input.X, input.Y, input.Z, input.frequency);
- for (int i = 0; i < input.X; i++)
- {
- for (int j = 0; j < input.Y; j++)
- {
- for (int k = 0; k < input.Z; k++)
- {
- fprintf(outfile, "%c", data[i][j][k]);
- }
- }
- }
- }
- void loadDataRoom()
- {
- FILE *infile;
- infile = fopen("config.dwm", "r");
- if (infile == NULL)
- {
- fprintf(stderr, "\nError opening config.dwm\n\n");
- exit(1);
- }
- fscanf(infile, "%d %d %d %ld\n", &input.X, &input.Y, &input.Z, &input.frequency);
- data = (unsigned char ***)malloc(input.X * sizeof(unsigned char **));
- for (int i = 0; i < input.X; i++)
- {
- data[i] = (unsigned char **)malloc(input.Y * sizeof(unsigned char *));
- for (int j = 0; j < input.Y; j++)
- {
- data[i][j] = (unsigned char *)malloc(input.Z * sizeof(unsigned char));
- for (int k = 0; k < input.Z; k++)
- {
- fscanf(infile, "%c", &data[i][j][k]);
- }
- }
- }
- }
- void insert_receiver_or_source(char node_type)
- {
- float _w, _h, _d;
- int x = 0, y = 0, z = 0;
- float i, j, k;
- do
- {
- printf("I -> Position : ");
- scanf("%f", &i);
- if (i < 0 && !isdigit(i))
- {
- printf("Invalid input\n");
- }
- } while (i < 0);
- x = i / node_spacing;
- do
- {
- printf("J -> Position : ");
- scanf("%f", &j);
- if (j < 0 && !isdigit(j))
- {
- printf("Invalid input\n");
- }
- } while (j < 0);
- y = j / node_spacing;
- do
- {
- printf("K -> Position : ");
- scanf("%f", &k);
- if (k < 0 && !isdigit(k))
- {
- printf("Invalid input\n");
- }
- } while (k < 0);
- z = k / node_spacing;
- _w = x * node_spacing;
- _d = y * node_spacing;
- _h = z * node_spacing;
- printf("Necessarily Ajusts: Center: ");
- printf("Position X: %.2f Position Y: %.2f Position Z: %.2f\n\n", _w, _h, _d);
- data[x][y][z] = node_type;
- }
- void insert_cuboid()
- {
- int x_min = 0, x_max = 0, y_min = 0, y_max = 0, z_min = 0, z_max = 0;
- float i_min = 0, i_max = 0, j_min = 0, j_max = 0, k_min = 0, k_max = 0;
- unsigned char node_type;
- double r;
- do
- {
- printf("Node type in Room\n");
- scanf("%c", &node_type);
- r = getCoeficient(node_type);
- printf("Absorption coefficient (ρ) = %f\n", r);
- if (r == -1)
- {
- printf("Invalid input\n");
- }
- } while (r == -1);
- do
- {
- printf("X - MIN: ");
- scanf("%f", &i_min);
- if (i_min < 0 && !isdigit(i_min))
- {
- printf("Invalid input\n");
- }
- } while (i_min < 0);
- x_min = i_min / node_spacing;
- do
- {
- printf("X - MAX : ");
- scanf("%f", &i_max);
- if (i_max < 0 && !isdigit(i_max))
- {
- printf("Invalid input\n");
- }
- } while (i_max < 0);
- x_max = i_max / node_spacing;
- do
- {
- printf("Y - MIN: ");
- scanf("%f", &j_min);
- if (j_min < 0 && !isdigit(j_min))
- {
- printf("Invalid input\n");
- }
- } while (j_min < 0);
- y_min = j_min / node_spacing;
- do
- {
- printf("Y - MAX: ");
- scanf("%f", &j_max);
- if (j_max < 0 && !isdigit(j_max))
- {
- printf("Invalid input\n");
- }
- } while (j_max < 0);
- y_max = j_max / node_spacing;
- do
- {
- printf("Z - MIN : ");
- scanf("%f", &k_min);
- if (k_min < 0 && !isdigit(k_min))
- {
- printf("Invalid input\n");
- }
- } while (k_min < 0);
- z_min = k_min / node_spacing;
- do
- {
- printf("Z - MAX : ");
- scanf("%f", &k_max);
- if (k_max < 0 && !isdigit(k_max))
- {
- printf("Invalid input\n");
- }
- } while (k_max < 0);
- z_max = k_max / node_spacing;
- for (int x = x_min; x <= x_max; x++)
- {
- for (int y = y_min; y <= y_max; y++)
- {
- for (int z = z_min; z <= z_max; z++)
- {
- data[x][y][z] = node_type;
- }
- }
- }
- }
- void insert_sphere()
- {/*
- int x = 0, y = 0, z = 0;
- float i, j, k, radius_, radius;
- unsigned char node_type;
- double r;
- do
- {
- printf("Node type in Room\n");
- scanf("%c", &node_type);
- r = getCoeficient(node_type);
- printf("Absorption coefficient (ρ) = %f\n", r);
- if (r == -1)
- {
- printf("Invalid input\n");
- }
- } while (r == -1);
- do
- {
- printf("I : ");
- scanf("%f", &i);
- if (i < 0 && !isdigit(i))
- {
- printf("Invalid input\n");
- }
- } while (i < 0);
- x = i / node_spacing;
- do
- {
- printf("J : ");
- scanf("%f", &j);
- if (j < 0 && !isdigit(j))
- {
- printf("Invalid input\n");
- }
- } while (j < 0);
- y = j / node_spacing;
- do
- {
- printf("K : ");
- scanf("%f", &k);
- if (k < 0 && !isdigit(k))
- {
- printf("Invalid input\n");
- }
- } while (k < 0);
- z = k / node_spacing;
- do
- {
- printf("Radius : ");
- scanf("%f", &radius_);
- if (radius_ < 0 && !isdigit(radius_))
- {
- printf("Invalid input\n");
- }
- } while (radius_ < 0);
- radius = radius_ / node_spacing;
- for (int x_ = 0; x_ <= radius; x_++)
- {
- for (int y_ = 0; y_ <= radius; y_++)
- {
- for (int z_ = 0; z <= radius; z_++)
- {
- if (sqrt(pow(x_, 2) + pow(y_, 2) + pow(z_, 2)) <= radius)
- {
- data[x + x_][y + y_][z + z_] = node_type;
- data[x - x_][y - y_][z - z_] = node_type;
- data[x - x_][y - y_][z + z_] = node_type;
- data[x + x_][y - y_][z - z_] = node_type;
- data[x - x_][y + y_][z - z_] = node_type;
- data[x - x_][y + y_][z + z_] = node_type;
- data[x + x_][y - y_][z + z_] = node_type;
- data[x + x_][y + y_][z - z_] = node_type;
- }
- }
- }
- }*/
- }
- void room_division()
- {
- int x_blocks = 0, y_blocks = 0, z_blocks = 0;
- do
- {
- printf("Blocks on X axis : ");
- scanf("%d", &x_blocks);
- if (x_blocks < 0 && !isdigit(x_blocks))
- {
- printf("Invalid input\n");
- }
- } while (x_blocks < 0);
- do
- {
- printf("Blocks on Y axis : ");
- scanf("%d", &y_blocks);
- if (y_blocks < 0 && !isdigit(y_blocks))
- {
- printf("Invalid input\n");
- }
- } while (y_blocks < 0);
- do
- {
- printf("Blocks on Z axis : ");
- scanf("%d", &z_blocks);
- if (z_blocks < 0 && !isdigit(z_blocks))
- {
- printf("Invalid input\n");
- }
- } while (z_blocks < 0);
- int x_block_size, y_block_size, z_block_size;
- int start_x, start_y, start_z;
- int end_x, end_y, end_z;
- int part_counter = 0;
- for (int x_block = 0; x_block < x_blocks; x_block++)
- {
- for (int y_block = 0; y_block < y_blocks; y_block++)
- {
- for (int z_block = 0; z_block < x_blocks; z_block++)
- {
- start_x = x_block * input.X / x_blocks;
- start_y = y_block * input.Y / y_blocks;
- start_z = z_block * input.Z / z_blocks;
- end_x = ((x_block+1) * input.X / x_blocks) - 1;
- end_y = ((y_block+1) * input.Y / y_blocks) - 1;
- end_z = ((z_block+1) * input.Z / z_blocks) - 1;
- partitioning(part_counter, x_block, y_block, z_block, start_x, start_y, start_z, end_x, end_y, end_z, x_blocks, y_blocks, z_blocks);
- part_counter++;
- }
- }
- }
- }
- void partitioning(int part_counter, int x_block, int y_block, int z_block, int start_x, int start_y, int start_z, int end_x, int end_y, int end_z, int x_blocks, int y_blocks, int z_blocks)
- {
- char c_name[150];
- char c_index[50];
- char c_ext[50];
- FILE *outfile;
- strcpy(c_name, "config_");
- sprintf(c_index, "%d", part_counter);
- strcpy(c_ext, ".dwm");
- strcat(c_name, c_index);
- strcat(c_name, c_ext);
- outfile = fopen(c_name, "w");
- if (outfile == NULL)
- {
- fprintf(stderr, "\nError opening %s\n\n", c_name);
- return;
- }
- int size_x = (end_x - start_x) + 1;
- int size_y = (end_y - start_y) + 1;
- int size_z = (end_z - start_z) + 1;
- int back_part_num, front_part_num;
- int left_part_num, right_part_num;
- int down_part_num, up_part_num;
- if (x_block == 0)
- {
- back_part_num = -1;
- front_part_num = part_counter + (y_blocks * z_blocks);
- }
- else if (x_block == x_blocks - 1)
- {
- back_part_num = part_counter - (y_blocks * z_blocks);
- front_part_num = -1;
- }
- else
- {
- back_part_num = part_counter - (y_blocks * z_blocks);
- front_part_num = part_counter + (y_blocks * z_blocks);
- }
- if (y_block == 0)
- {
- left_part_num = -1;
- right_part_num = part_counter + z_blocks;
- }
- else if (y_block == y_blocks - 1)
- {
- left_part_num = part_counter - z_blocks;
- right_part_num = -1;
- }
- else
- {
- left_part_num = part_counter - z_blocks;
- right_part_num = part_counter + z_blocks;
- }
- if (z_block == 0)
- {
- down_part_num = -1;
- up_part_num = part_counter + 1;
- }
- else if (z_block == z_blocks - 1)
- {
- up_part_num = -1;
- down_part_num = part_counter - 1;
- }
- else
- {
- down_part_num = part_counter -1;
- up_part_num = part_counter + 1;
- }
- fprintf(outfile, "%d %d %d %d %d %d %d %d %d %ld\n", size_x, size_y, size_z, back_part_num, front_part_num, left_part_num, right_part_num, down_part_num, up_part_num, input.frequency);
- int contador = 0;
- for (int i = start_x; i <= end_x; i++)
- {
- for (int j = start_y; j <= end_y; j++)
- {
- for (int k = start_z; k <= end_z; k++)
- {
- fprintf(outfile, "%c", data[i][j][k]);
- contador++;
- }
- }
- }
- }
- void loadRoomPartition(int partitionId)
- {
- char c_name[150];
- char c_index[50];
- char c_ext[50];
- strcpy(c_name, "config_");
- sprintf(c_index, "%d", partitionId);
- strcpy(c_ext, ".dwm");
- strcat(c_name, c_index);
- strcat(c_name, c_ext);
- FILE *infile;
- infile = fopen(c_name, "r");
- if (infile == NULL)
- {
- fprintf(stderr, "\nError opening %s\n\n", c_name);
- exit(1);
- }
- fscanf(infile, "%d %d %d %d %d %d %d %d %d %ld\n", &partition.size_x, &partition.size_y, &partition.size_z, &partition.back_part_num, &partition.front_part_num, &partition.left_part_num, &partition.right_part_num, &partition.down_part_num, &partition.up_part_num, &input.frequency);
- data = (unsigned char ***)malloc(partition.size_x * sizeof(unsigned char **));
- for (int i = 0; i <= partition.size_x; i++)
- {
- data[i] = (unsigned char **)malloc(partition.size_y * sizeof(unsigned char *));
- for (int j = 0; j < partition.size_y; j++)
- {
- data[i][j] = (unsigned char *)malloc(partition.size_z * sizeof(unsigned char));
- for (int k = 0; k < partition.size_z; k++)
- {
- fscanf(infile, "%c", &data[i][j][k]);
- }
- }
- }
- }
- void loadRoomPlant()
- {
- /*source = malloc(sizeof(pl));
- receiver = malloc(sizeof(pl));*/
- FILE *infile;
- infile = fopen("config.dwm", "r");
- unsigned char *node_type;
- double r;
- if (infile == NULL)
- {
- fprintf(stderr, "\nError opening config.dwm\n\n");
- exit(1);
- }
- fscanf(infile, "%d %d %d %ld\n", &input.X, &input.Y, &input.Z, &input.frequency);
- plantMemoryAllocationAndDataReading(infile);
- }
- void loadPartitionPlant(int partitionId)
- {
- /*source = malloc(sizeof(pl));
- receiver = malloc(sizeof(pl));*/
- char c_name[150];
- char c_index[50];
- char c_ext[50];
- strcpy(c_name, "config_");
- sprintf(c_index, "%d", partitionId);
- strcpy(c_ext, ".dwm");
- strcat(c_name, c_index);
- strcat(c_name, c_ext);
- FILE *infile;
- infile = fopen(c_name, "r");
- if (infile == NULL)
- {
- fprintf(stderr, "\nError opening %s\n\n", c_name);
- exit(1);
- }
- fscanf(infile, "%d %d %d %d %d %d %d %d %d %ld\n", &partition.size_x, &partition.size_y, &partition.size_z, &partition.back_part_num, &partition.front_part_num, &partition.left_part_num, &partition.right_part_num, &partition.down_part_num, &partition.up_part_num, &input.frequency);
- input.X = partition.size_x;
- input.Y = partition.size_y;
- input.Z = partition.size_z;
- plantMemoryAllocationAndDataReading(infile);
- }
- void plantMemoryAllocationAndDataReading(FILE *infile)
- {
- //make buffers single dimensional
- /* X Axix Buffers memory alocations */
- backInBuffer = (double **)malloc(input.Y * sizeof(double *));
- for (int i = 0; i < input.Y; i++)
- backInBuffer[i] = (double *)malloc(input.Z * sizeof(double));
- backOutBuffer = (double **)malloc(input.Y * sizeof(double *));
- for (int i = 0; i < input.Y; i++)
- backOutBuffer[i] = (double *)malloc(input.Z * sizeof(double));
- frontInBuffer = (double **)malloc(input.Y * sizeof(double *));
- for (int i = 0; i < input.Y; i++)
- frontInBuffer[i] = (double *)malloc(input.Z * sizeof(double));
- frontOutBuffer = (double **)malloc(input.Y * sizeof(double *));
- for (int i = 0; i < input.Y; i++)
- frontOutBuffer[i] = (double *)malloc(input.Z * sizeof(double));
- /* Y Axix Buffers memory alocations */
- leftInBuffer = (double **)malloc(input.X * sizeof(double *));
- for (int i = 0; i < input.X; i++)
- leftInBuffer[i] = (double *)malloc(input.Z * sizeof(double));
- leftOutBuffer = (double **)malloc(input.X * sizeof(double *));
- for (int i = 0; i < input.X; i++)
- leftOutBuffer[i] = (double *)malloc(input.Z * sizeof(double));
- rightInBuffer = (double **)malloc(input.X * sizeof(double *));
- for (int i = 0; i < input.X; i++)
- rightInBuffer[i] = (double *)malloc(input.Z * sizeof(double));
- rightOutBuffer = (double **)malloc(input.X * sizeof(double *));
- for (int i = 0; i < input.X; i++)
- rightOutBuffer[i] = (double *)malloc(input.Z * sizeof(double));
- /* Z Axix Buffers memory alocations */
- downInBuffer = (double **)malloc(input.X * sizeof(double *));
- for (int i = 0; i < input.Y; i++)
- downInBuffer[i] = (double *)malloc(input.Y * sizeof(double));
- downOutBuffer = (double **)malloc(input.X * sizeof(double *));
- for (int i = 0; i < input.Y; i++)
- downOutBuffer[i] = (double *)malloc(input.Y * sizeof(double));
- upInBuffer = (double **)malloc(input.X * sizeof(double *));
- for (int i = 0; i < input.Y; i++)
- upInBuffer[i] = (double *)malloc(input.Y * sizeof(double));
- upOutBuffer = (double **)malloc(input.X * sizeof(double *));
- for (int i = 0; i < input.Y; i++)
- upOutBuffer[i] = (double *)malloc(input.Y * sizeof(double));
- /* Room plant memory alocation*/
- plant = (pl ***)malloc(input.X * sizeof(pl **));
- for (int i = 0; i < input.X; i++)
- {
- plant[i] = (pl **)malloc(input.Y * sizeof(pl *));
- for (int j = 0; j < input.Y; j++)
- {
- plant[i][j] = (pl *)malloc(input.Z * sizeof(pl));
- for (int k = 0; k < input.Z; k++)
- {
- fscanf(infile, "%c", &plant[i][j][k].node_type);
- plant[i][j][k].reflection_coeficient = getCoeficient(plant[i][j][k].node_type);
- if (plant[i][j][k].node_type == 'S')
- {
- source = &plant[i][j][k];
- printf("source coordinate %d %d %d\n", i, j, k);
- }
- else if (plant[i][j][k].node_type == 'R')
- {
- receiver = &plant[i][j][k];
- printf("receiver coordinate %d %d %d\n", i, j, k);
- }
- }
- }
- }
- }
- void scattering()
- {
- int i, j, k;
- for (i = 0; i < input.X; i++)
- {
- for (j = 0; j < input.Y; j++)
- {
- for (k = 0; k < input.Z; k++)
- {
- if (plant[i][j][k].reflection_coeficient == -1)
- {
- plant[i][j][k].p = (plant[i][j][k].pinFront + plant[i][j][k].pinBack + plant[i][j][k].pinRight + plant[i][j][k].pinLeft + plant[i][j][k].pinDown + plant[i][j][k].pinUp) / 3;
- plant[i][j][k].poutFront = plant[i][j][k].p - plant[i][j][k].pinFront;
- plant[i][j][k].poutBack = plant[i][j][k].p - plant[i][j][k].pinBack;
- plant[i][j][k].poutRight = plant[i][j][k].p - plant[i][j][k].pinRight;
- plant[i][j][k].poutLeft = plant[i][j][k].p - plant[i][j][k].pinLeft;
- plant[i][j][k].poutDown = plant[i][j][k].p - plant[i][j][k].pinDown;
- plant[i][j][k].poutUp = plant[i][j][k].p - plant[i][j][k].pinUp;
- }
- else
- {
- plant[i][j][k].poutFront = plant[i][j][k].reflection_coeficient * plant[i][j][k].poutFront;
- plant[i][j][k].poutBack = plant[i][j][k].reflection_coeficient * plant[i][j][k].poutBack;
- plant[i][j][k].poutRight = plant[i][j][k].reflection_coeficient * plant[i][j][k].poutRight;
- plant[i][j][k].poutLeft = plant[i][j][k].reflection_coeficient * plant[i][j][k].poutLeft;
- plant[i][j][k].poutDown = plant[i][j][k].reflection_coeficient * plant[i][j][k].poutDown;
- plant[i][j][k].poutUp = plant[i][j][k].reflection_coeficient * plant[i][j][k].poutUp;
- }
- }
- }
- }
- }
- void delay()
- {
- int i, j, k;
- //-----------------Buffers e pontos fronteira----------------------
- /* X Axix Buffers */
- for (j = 0; j < input.Y; j++)
- {
- for (k = 0; k < input.Z; k++)
- {
- plant[0][j][k].pinBack = backInBuffer[j][k];
- plant[input.X - 1][j][k].pinFront = frontInBuffer[j][k];
- backOutBuffer[j][k] = plant[0][j][k].poutBack;
- frontOutBuffer[j][k] = plant[input.X - 1][j][k].poutFront;
- }
- }
- /* Y Axix Buffers */
- for (i = 0; i < input.X; i++)
- {
- for (k = 0; k < input.Z; k++)
- {
- plant[i][0][k].pinLeft = leftInBuffer[i][k];
- plant[i][input.Y - 1][k].pinRight = rightInBuffer[i][k];
- leftOutBuffer[i][k] = plant[i][0][k].poutLeft;
- rightOutBuffer[i][k] = plant[i][input.Y - 1][k].poutRight;
- }
- }
- /* Z Axix Buffers */
- for (i = 0; i < input.X; i++)
- {
- for (j = 0; j < input.Y; j++)
- {
- plant[i][j][0].pinDown = downInBuffer[i][j];
- plant[i][j][input.Z - 1].pinUp = upInBuffer[i][j];
- downOutBuffer[i][j] = plant[i][j][0].poutDown;
- upOutBuffer[i][j] = plant[i][j][input.Z - 1].poutUp;
- }
- }
- //---------------Interior Nodes---------------------------
- for (i = 1; i < input.X - 1; i++)
- {
- for (j = 1; j < input.Y - 1; j++)
- {
- for (k = 1; k < input.Z - 1; k++)
- {
- plant[i][j][k].pinBack = plant[i - 1][j][k].poutFront;
- plant[i][j][k].pinLeft = plant[i][j - 1][k].poutRight;
- plant[i][j][k].pinDown = plant[i][j][k - 1].poutUp;
- plant[i][j][k].pinFront = plant[i + 1][j][k].poutBack;
- plant[i][j][k].pinRight = plant[i][j + 1][k].poutLeft;
- plant[i][j][k].pinUp = plant[i][j][k + 1].poutDown;
- }
- }
- }
- }
- void run()
- {
- int iterations = number_samples + 2 * (input.frequency);
- result = (int16_t *)malloc((size_t)(iterations * sizeof(int16_t)));
- for (int iter = 0; iter < iterations; iter++)
- {
- if (iter < number_samples){
- source->pinFront = (double)samples[iter]/2;
- source->pinBack = (double)samples[iter]/2;
- source->pinUp = (double)samples[iter]/2;
- source->pinDown = (double)samples[iter]/2;
- source->pinRight = (double)samples[iter]/2;
- source->pinLeft = (double)samples[iter]/2;
- }
- scattering();
- result[iter] = (int16_t)receiver->p;
- delay();
- }
- wavwrite("result.wav", result);
- }
- double getCoeficient(unsigned char c)
- {
- double reflex_coeficient;
- switch (c)
- {
- case ('A'):
- reflex_coeficient = 0;
- break;
- case ('B'):
- reflex_coeficient = 0.1;
- break;
- case ('C'):
- reflex_coeficient = 0.2;
- break;
- case ('D'):
- reflex_coeficient = 0.3;
- break;
- case ('E'):
- reflex_coeficient = 0.4;
- break;
- case ('F'):
- reflex_coeficient = 0.5;
- break;
- case ('G'):
- reflex_coeficient = 0.6;
- break;
- case ('H'):
- reflex_coeficient = 0.7;
- break;
- case ('I'):
- reflex_coeficient = 0.8;
- break;
- case ('J'):
- reflex_coeficient = 0.9;
- break;
- case ('Z'):
- reflex_coeficient = 1;
- break;
- case ('1'):
- reflex_coeficient = 0.91;
- break;
- case ('2'):
- reflex_coeficient = 0.92;
- break;
- case ('3'):
- reflex_coeficient = 0.93;
- break;
- case ('4'):
- reflex_coeficient = 0.94;
- break;
- case ('5'):
- reflex_coeficient = 0.95;
- break;
- case ('6'):
- reflex_coeficient = 0.96;
- break;
- case ('7'):
- reflex_coeficient = 0.97;
- break;
- case ('8'):
- reflex_coeficient = 0.98;
- break;
- case ('9'):
- reflex_coeficient = 0.99;
- break;
- default:
- reflex_coeficient = -1;
- break;
- }
- return reflex_coeficient;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement