xref: /qemu/replay/replay-audio.c (revision f8e1a989)
1 /*
2  * replay-audio.c
3  *
4  * Copyright (c) 2010-2017 Institute for System Programming
5  *                         of the Russian Academy of Sciences.
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2 or later.
8  * See the COPYING file in the top-level directory.
9  *
10  */
11 
12 #include "qemu/osdep.h"
13 #include "qemu/error-report.h"
14 #include "sysemu/replay.h"
15 #include "replay-internal.h"
16 #include "sysemu/sysemu.h"
17 #include "audio/audio.h"
18 
19 void replay_audio_out(int *played)
20 {
21     if (replay_mode == REPLAY_MODE_RECORD) {
22         g_assert(replay_mutex_locked());
23         replay_save_instructions();
24         replay_put_event(EVENT_AUDIO_OUT);
25         replay_put_dword(*played);
26     } else if (replay_mode == REPLAY_MODE_PLAY) {
27         g_assert(replay_mutex_locked());
28         replay_account_executed_instructions();
29         if (replay_next_event_is(EVENT_AUDIO_OUT)) {
30             *played = replay_get_dword();
31             replay_finish_event();
32         } else {
33             error_report("Missing audio out event in the replay log");
34             abort();
35         }
36     }
37 }
38 
39 void replay_audio_in(int *recorded, void *samples, int *wpos, int size)
40 {
41     int pos;
42     uint64_t left, right;
43     if (replay_mode == REPLAY_MODE_RECORD) {
44         g_assert(replay_mutex_locked());
45         replay_save_instructions();
46         replay_put_event(EVENT_AUDIO_IN);
47         replay_put_dword(*recorded);
48         replay_put_dword(*wpos);
49         for (pos = (*wpos - *recorded + size) % size ; pos != *wpos
50              ; pos = (pos + 1) % size) {
51             audio_sample_to_uint64(samples, pos, &left, &right);
52             replay_put_qword(left);
53             replay_put_qword(right);
54         }
55     } else if (replay_mode == REPLAY_MODE_PLAY) {
56         g_assert(replay_mutex_locked());
57         replay_account_executed_instructions();
58         if (replay_next_event_is(EVENT_AUDIO_IN)) {
59             *recorded = replay_get_dword();
60             *wpos = replay_get_dword();
61             for (pos = (*wpos - *recorded + size) % size ; pos != *wpos
62                  ; pos = (pos + 1) % size) {
63                 left = replay_get_qword();
64                 right = replay_get_qword();
65                 audio_sample_from_uint64(samples, pos, left, right);
66             }
67             replay_finish_event();
68         } else {
69             error_report("Missing audio in event in the replay log");
70             abort();
71         }
72     }
73 }
74