EyeAI
Loading...
Searching...
No Matches
ByteArrayParser.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <sndfile.hh>
4#include <cstddef>
5#include <cstring>
6
7/*
8Handles the conversion of the bytearray, representing
9the .wav file where the audio labels are stored, to
10a state, where it can be processed by the libsndfile
11library
12*/
13
14struct MemoryData {
15 const std::byte* data;
16 sf_count_t size;
17 sf_count_t pos;
18};
19
20inline sf_count_t vio_get_filelen(void* user_data) {
21 auto* mem = static_cast<MemoryData*>(user_data);
22 return mem->size;
23}
24
25inline sf_count_t vio_seek(sf_count_t offset, int whence, void* user_data) {
26 auto* mem = static_cast<MemoryData*>(user_data);
27 sf_count_t newpos = mem->pos;
28
29 switch (whence) {
30 case SEEK_SET: newpos = offset; break;
31 case SEEK_CUR: newpos += offset; break;
32 case SEEK_END: newpos = mem->size + offset; break;
33 default: return -1;
34 }
35
36 if (newpos < 0 || newpos > mem->size)
37 return -1;
38
39 mem->pos = newpos;
40 return mem->pos;
41}
42
43inline sf_count_t vio_read(void* ptr, sf_count_t count, void* user_data) {
44 auto* mem = static_cast<MemoryData*>(user_data);
45 sf_count_t remain = mem->size - mem->pos;
46 sf_count_t to_read = (count < remain ? count : remain);
47
48 if (to_read > 0) {
49 std::memcpy(ptr, mem->data + mem->pos, static_cast<size_t>(to_read));
50 mem->pos += to_read;
51 }
52
53 return to_read;
54}
55
56inline sf_count_t vio_write(const void* /*unused*/, sf_count_t /*unused*/, void* /*unused*/) {
57 return 0;
58}
59
60inline sf_count_t vio_tell(void* user_data) {
61 auto* mem = static_cast<MemoryData*>(user_data);
62 return mem->pos;
63}
sf_count_t vio_read(void *ptr, sf_count_t count, void *user_data)
Definition ByteArrayParser.hpp:43
sf_count_t vio_get_filelen(void *user_data)
Definition ByteArrayParser.hpp:20
sf_count_t vio_tell(void *user_data)
Definition ByteArrayParser.hpp:60
sf_count_t vio_seek(sf_count_t offset, int whence, void *user_data)
Definition ByteArrayParser.hpp:25
sf_count_t vio_write(const void *, sf_count_t, void *)
Definition ByteArrayParser.hpp:56
Definition ByteArrayParser.hpp:14
sf_count_t size
Definition ByteArrayParser.hpp:16
sf_count_t pos
Definition ByteArrayParser.hpp:17
const std::byte * data
Definition ByteArrayParser.hpp:15