Skip to content

Commit ad13cd8

Browse files
committed
Added sound
The sound was added in a unique manner: instead of just changing the pitch of the sound depending on the value of the element accessed, the sound itself changes depending on whether the access was a read or write. The next commit will fix an issue where you must run the program with the repository folder as your current working directory.
1 parent bc540cc commit ad13cd8

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ Please **read the warnings** at the top. Thank you!
2727

2828
## To-do
2929

30-
- [X] Add indicator for where array accesses are, with different colors for reads, writes, and both
30+
- [X] Add indicator for where array accesses are, with different colors for reads, writes, and both (bc540cc1ceae5de769dd40eda9566bbea58ca124)
3131
- [ ] Make the indicator more visible
32-
- [ ] Add sounds
32+
- [X] Add sounds
3333
- [ ] Add a HUD using a non-obtrusive color
3434
- [ ] Add more algorithms
3535

assets/array_read.wav

10.8 KB
Binary file not shown.

assets/array_write.wav

10.8 KB
Binary file not shown.

src/main.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pthread_t sort_array_write_lock = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER;
2929
size_t sort_array_write_len = 0;
3030
size_t *sort_array_write_indices;
3131

32+
Sound array_read_sound;
33+
Sound array_write_sound;
34+
3235
#define push_array_access(mutex, index_stack, index_len) \
3336
pthread_mutex_lock(mutex); \
3437
size_t old_len = index_len; \
@@ -68,13 +71,17 @@ void draw_array(Array array, int width, int height, int x, int y)
6871

6972
const Color RECTANGLE_COLORS[4] = {WHITE, BLUE, RED, MAGENTA};
7073

71-
#define compile_array_accesses(mutex, index_stack, index_len, bit) \
74+
#define compile_array_accesses(mutex, index_stack, index_len, bit, sound) \
7275
pthread_mutex_lock(mutex); \
7376
for (size_t i = 0; i < index_len; i++) \
7477
{ \
7578
size_t index = index_stack[i]; \
7679
if (index < array->len) \
80+
{ \
7781
sort_array_modifications[index >> 2] |= bit << ((index & 3) << 1); \
82+
SetSoundPitch(sound, (float)array->_arr[index] / array->len); \
83+
PlaySound(sound); \
84+
} \
7885
} \
7986
index_len = 0; \
8087
index_stack = MemRealloc(index_stack, 0); \
@@ -85,8 +92,8 @@ void draw_array(Array array, int width, int height, int x, int y)
8592
{
8693
sort_array_modifications = MemAlloc((array->len + 3 >> 2) * sizeof(unsigned char));
8794

88-
compile_array_accesses(&sort_array_read_lock, sort_array_read_indices, sort_array_read_len, 1);
89-
compile_array_accesses(&sort_array_write_lock, sort_array_write_indices, sort_array_write_len, 2);
95+
compile_array_accesses(&sort_array_read_lock, sort_array_read_indices, sort_array_read_len, 1, array_read_sound);
96+
compile_array_accesses(&sort_array_write_lock, sort_array_write_indices, sort_array_write_len, 2, array_write_sound);
9097
}
9198

9299
for (size_t i = 0; i < array->len; i++)
@@ -166,6 +173,10 @@ int main()
166173
Array_set_set_callback(my_array_write_callback);
167174
sort_array = Array_new_init(256);
168175

176+
InitAudioDevice();
177+
array_read_sound = LoadSound("assets/array_read.wav");
178+
array_write_sound = LoadSound("assets/array_write.wav");
179+
169180
InitWindow(640, 480, "Sorting Visualizer");
170181
SetWindowState(FLAG_WINDOW_RESIZABLE);
171182
SetWindowMinSize(10, 10);

0 commit comments

Comments
 (0)