mcapibridge 0.1.4__tar.gz → 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mcapibridge
3
- Version: 0.1.4
3
+ Version: 0.2.1
4
4
  Summary: Python libary for MCAPIBridge
5
5
  Author: TaotianZhufang
6
6
  License: MIT License
@@ -39,6 +39,8 @@ MCAPIBridge is a mod for Minecraft loaded with Fabric.This libary offers some wa
39
39
  ### Install
40
40
  Ensure your mod loaded.
41
41
 
42
+ **Please ensure you are using the latest libary.Now version is 0.2.1.**
43
+
42
44
  Use pip to install this.
43
45
  ```
44
46
  pip install mcapibridge
@@ -145,6 +147,12 @@ Modifies the NBT data of a block entity (Tile Entity).
145
147
  * **nbt_string**: Valid SNBT string.
146
148
 
147
149
 
150
+ ---
151
+
152
+ ### Audio
153
+
154
+ #### [Audio Doc](https://github.com/TaotianZhufang/MCAPIBridge/blob/main/Bridges/Python/PyAudio.MD)
155
+
148
156
  ---
149
157
 
150
158
  ### Info
@@ -7,6 +7,8 @@ MCAPIBridge is a mod for Minecraft loaded with Fabric.This libary offers some wa
7
7
  ### Install
8
8
  Ensure your mod loaded.
9
9
 
10
+ **Please ensure you are using the latest libary.Now version is 0.2.1.**
11
+
10
12
  Use pip to install this.
11
13
  ```
12
14
  pip install mcapibridge
@@ -113,6 +115,12 @@ Modifies the NBT data of a block entity (Tile Entity).
113
115
  * **nbt_string**: Valid SNBT string.
114
116
 
115
117
 
118
+ ---
119
+
120
+ ### Audio
121
+
122
+ #### [Audio Doc](https://github.com/TaotianZhufang/MCAPIBridge/blob/main/Bridges/Python/PyAudio.MD)
123
+
116
124
  ---
117
125
 
118
126
  ### Info
@@ -4,8 +4,8 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "mcapibridge"
7
- version = "0.1.4"
8
- description = "Python libary for MCAPIBridge"
7
+ version = "0.2.1"
8
+ description ="Python libary for MCAPIBridge"
9
9
  requires-python = ">=3.7"
10
10
  license = { file = "LICENSE" }
11
11
  readme = "README.MD"
@@ -1,6 +1,104 @@
1
1
  import socket
2
2
  import math
3
3
  import time
4
+ import base64
5
+ import wave
6
+ import struct
7
+
8
+ class AudioManager:
9
+ def __init__(self, mc):
10
+ self.mc = mc
11
+
12
+ def load_wav(self, target, audio_id, filepath):
13
+ with wave.open(filepath, 'rb') as wav:
14
+ channels = wav.getnchannels()
15
+ sample_width = wav.getsampwidth()
16
+ sample_rate = wav.getframerate()
17
+ frames = wav.readframes(wav.getnframes())
18
+
19
+ if channels == 2:
20
+ samples = struct.unpack(f'<{len(frames)//2}h', frames)
21
+ mono_samples = []
22
+ for i in range(0, len(samples), 2):
23
+ mono_samples.append((samples[i] + samples[i+1]) // 2)
24
+ frames = struct.pack(f'<{len(mono_samples)}h', *mono_samples)
25
+
26
+ if sample_width == 1:
27
+ samples = struct.unpack(f'{len(frames)}B', frames)
28
+ samples = [(s - 128) * 256 for s in samples]
29
+ frames = struct.pack(f'<{len(samples)}h', *samples)
30
+
31
+ b64_data = base64.b64encode(frames).decode('ascii')
32
+
33
+ chunk_size = 40000
34
+ chunks = [b64_data[i:i + chunk_size] for i in range(0, len(b64_data), chunk_size)]
35
+
36
+ for i, chunk in enumerate(chunks):
37
+ if i == 0:
38
+ self.mc._send(f"audio.load({target},{audio_id},{sample_rate},{chunk})")
39
+ else:
40
+ self.mc._send(f"audio.stream({target},{audio_id},{sample_rate},{chunk})")
41
+ time.sleep(0.02)
42
+
43
+ self.mc._send(f"audio.finishLoad({target},{audio_id})")
44
+
45
+ print(f"[Audio] Loaded {filepath}: {len(frames)} bytes, {sample_rate}Hz")
46
+
47
+ def load_raw(self, target, audio_id, pcm_data, sample_rate=44100):
48
+ b64_data = base64.b64encode(pcm_data).decode('ascii')
49
+
50
+ chunk_size = 40000
51
+ chunks = [b64_data[i:i + chunk_size] for i in range(0, len(b64_data), chunk_size)]
52
+
53
+ for i, chunk in enumerate(chunks):
54
+ if i == 0:
55
+ self.mc._send(f"audio.load({target},{audio_id},{sample_rate},{chunk})")
56
+ else:
57
+ self.mc._send(f"audio.stream({target},{audio_id},{sample_rate},{chunk})")
58
+ time.sleep(0.02)
59
+
60
+ self.mc._send(f"audio.finishLoad({target},{audio_id})")
61
+
62
+ def play(self, target, audio_id, volume=1.0, loop=False):
63
+ loop_str = "true" if loop else "false"
64
+ self.mc._send(f"audio.play({target},{audio_id},{volume},{loop_str})")
65
+
66
+ def play_at(self, audio_id, x, y, z, radius=32, volume=1.0):
67
+ self.mc._send(f"audio.playAt({audio_id},{x},{y},{z},{radius},{volume})")
68
+
69
+ def play_3d(self, target, audio_id, x, y, z, volume=1.0, rolloff=1.0):
70
+ self.mc._send(f"audio.play3d({target},{audio_id},{x},{y},{z},{volume},{rolloff})")
71
+
72
+ def pause(self, target, audio_id):
73
+ self.mc._send(f"audio.pause({target},{audio_id})")
74
+
75
+ def stop(self, target, audio_id):
76
+ self.mc._send(f"audio.stop({target},{audio_id})")
77
+
78
+ def unload(self, target, audio_id):
79
+ self.mc._send(f"audio.unload({target},{audio_id})")
80
+
81
+ def set_volume(self, target, audio_id, volume):
82
+ """(0.0 - 1.0)"""
83
+ self.mc._send(f"audio.volume({target},{audio_id},{volume})")
84
+
85
+ def generate_tone(self, target, audio_id, frequency=440, duration=1.0, sample_rate=44100):
86
+ import math
87
+
88
+ num_samples = int(sample_rate * duration)
89
+ samples = []
90
+
91
+ for i in range(num_samples):
92
+ t = i / sample_rate
93
+ value = int(32767 * math.sin(2 * math.pi * frequency * t))
94
+ samples.append(value)
95
+
96
+ pcm_data = struct.pack(f'<{len(samples)}h', *samples)
97
+ self.load_raw(target, audio_id, pcm_data, sample_rate)
98
+
99
+ def set_position(self, target, audio_id, x, y, z):
100
+ self.mc._send(f"audio.position({target},{audio_id},{x},{y},{z})")
101
+
4
102
 
5
103
  class Vec3:
6
104
  def __init__(self, x, y, z):
@@ -41,7 +139,7 @@ class BlockHit:
41
139
  self.pos = Vec3(x, y, z)
42
140
  self.face = face
43
141
  self.entityId = entityId
44
- self.action = action # 1=Left,2=Right, 101-105:Keyboard Action
142
+ self.action = action # 1=Left,2=Right,101-105:Keyboard Action
45
143
  if action == 1: self.type = "LEFT_CLICK"
46
144
  elif action == 2: self.type = "RIGHT_CLICK"
47
145
  elif action > 100: self.type = f"KEY_MACRO_{action - 100}"
@@ -54,6 +152,7 @@ class Minecraft:
54
152
  self.file_reader = None
55
153
  self.connected = False
56
154
  self._connect()
155
+ self.audio = AudioManager(self)
57
156
 
58
157
  def _connect(self):
59
158
  try:
@@ -353,3 +452,4 @@ class Minecraft:
353
452
  cmd = f"block.setNbt({int(x)},{int(y)},{int(z)},{nbt_string})"
354
453
  if dimension: cmd += f",{dimension}"
355
454
  self._send(cmd)
455
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mcapibridge
3
- Version: 0.1.4
3
+ Version: 0.2.1
4
4
  Summary: Python libary for MCAPIBridge
5
5
  Author: TaotianZhufang
6
6
  License: MIT License
@@ -39,6 +39,8 @@ MCAPIBridge is a mod for Minecraft loaded with Fabric.This libary offers some wa
39
39
  ### Install
40
40
  Ensure your mod loaded.
41
41
 
42
+ **Please ensure you are using the latest libary.Now version is 0.2.1.**
43
+
42
44
  Use pip to install this.
43
45
  ```
44
46
  pip install mcapibridge
@@ -145,6 +147,12 @@ Modifies the NBT data of a block entity (Tile Entity).
145
147
  * **nbt_string**: Valid SNBT string.
146
148
 
147
149
 
150
+ ---
151
+
152
+ ### Audio
153
+
154
+ #### [Audio Doc](https://github.com/TaotianZhufang/MCAPIBridge/blob/main/Bridges/Python/PyAudio.MD)
155
+
148
156
  ---
149
157
 
150
158
  ### Info
File without changes
File without changes