Python(Jython)从图片中的像素播放音符

发布于 2024-08-23 16:20:18 字数 918 浏览 4 评论 0原文

这是来自课堂作业:

这个程序是关于听颜色的。我们将把图片当作钢琴乐谱。 编写一个名为listenToPicture 的函数,该函数将一张图片作为参数。它首先显示图片。接下来,它将循环遍历每 4 行中的每 4 个像素并执行以下操作。它将计算像素的红色、绿色和蓝色级别的总和,将其除以 9,然后将结果添加到 24。该数字将是 playNote 播放的音符编号。 这意味着像素越暗,音符越低;像素越亮,音符越高。它将以最大音量 (127) 播放该音符十分之一秒(100 毫秒)。每次移动到新行时,它都会在控制台上打印出行号(y 值)。 您的主要功能将要求用户选择带有图片的文件。它将打印要演奏的音符数(即图片中的像素数除以 16;为什么?)。然后它将调用listenToPicture函数。

好吧,我编辑了到目前为止的内容,唯一我还没有弄清楚(我相信)的是如何在主函数中打印音符数量。顺便说一句,感谢所有提供帮助的人。你们太棒了。这个网站有捐款的地方吗?

def main():
   pic=makePicture(pickAFile())
   show (pic)
   listenToPicture(pic)

def listenToPicture(pic):
   w=getWidth(pic)
   h=getHeight(pic)

   for y in range(0,h,4):
       printNow(str(y))
       for x in range (0,w,4):
            px=getPixel(pic,x,y)                            
            r=getRed(px)
            g=getGreen(px)
            b=getBlue(px)
            tot=((r+g+b)/9)+24
            playNote(tot,100,127)

This is from a class assignment:

This program is about listening to colors. We will treat pictures as piano scores.
Write a function called listenToPicture that takes one picture as an argument. It first shows the picture. Next, it will loop through every 4th pixel in every 4th row and do the following. It will compute the total of the red, green and blue levels of the pixel, divide that by 9, then add the result to 24. That number will be the note number played by playNote.
That means that the darker the pixel, the lower the note; the lighter the pixel, the higher the note. It will play that note at full volume (127) for a tenth of a second (100 milliseconds). Every time it moves to a new row, it prints out the row number (y value) on the console.
Your main function will ask the user to select a file with a picture. It will print the number of notes to be played (which is the number of pixels in the picture divided by 16; why?). It will then call the listenToPicture function.

Ok I edited in what I have so far and the only thing I haven't figured out (I believe) is how to print the number of notes in the main function. By the way, thanks to everyone who helped. You guys are amazing. Is there a place to donate to this site?

def main():
   pic=makePicture(pickAFile())
   show (pic)
   listenToPicture(pic)

def listenToPicture(pic):
   w=getWidth(pic)
   h=getHeight(pic)

   for y in range(0,h,4):
       printNow(str(y))
       for x in range (0,w,4):
            px=getPixel(pic,x,y)                            
            r=getRed(px)
            g=getGreen(px)
            b=getBlue(px)
            tot=((r+g+b)/9)+24
            playNote(tot,100,127)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦过后 2024-08-30 16:20:18

罗比对于循环的宽度/高度是正确的。

您用来获取像素并播放音符的循环看起来就像是获取所有像素并在每次获得唯一的 x 和 y 时播放它们。您应该做的是获取 (x,y) 处的像素,然后提取 RGB 值并调用播放注释。你真的甚至不需要第三个 for 循环。你离这个还不算太远。尝试用简单的英语按照逻辑步骤写出问题。我发现这在我开始编码之前有很大帮助。

祝你好运。

Robbie is right for the width/height for loops.

The loop you are using to get the pixels and play the notes looks as if it is getting ALL the pixels and playing them all every time you get a unique x and y. What you should be doing is be getting the pixel at (x,y) then pulling out the rgb values and calling play note on that. You really shouldn't even need the 3rd for loop. You're not too far off. Try writing the problem out in logical steps in plain English. I find that helps a ton before I start coding.

Good Luck.

平生欢 2024-08-30 16:20:18

您之前曾询问过类似的事情。好吧,由于您没有放入任何有关实际检索像素值的代码,所以我假设您仍然无法做到这一点。我知道这远远超出了你的问题,但上次你对你的问题非常含糊,并表示你需要更多的帮助,而不仅仅是你所问的。如果其中任何一个是不必要的,那么就忽略它。我只是想提供一些建议,你可以接受也可以放弃。

如果您还没有弄清楚如何读取像素,我建议使用 PIL。它具有打开此处记录的图像的功能。然后,您可以使用同一页面上记录的 getpixel 通过 x 和 y 值访问图像中的像素。

为了播放音符,我建议查看 PyAudio 模块并制作自己的模块写入开放音频流的各种频率的正弦曲线(取决于像素的大小)。这部分可能有更好的包,但这是我在 Python 音频的小冒险中使用的。

对于音频内容,我会尝试仅以固定频率输出声音,然后再尝试实际发出变化的频率。

编辑:
你的循环现在看起来更好了,所以我拿出了关于你的循环的东西。

You asked about similar things before. Well, since you didn't put any code in about actually retrieving the pixel value, I'll assume that you still aren't able to do that. I know this is going way beyond your question, but last time you were pretty vague about your question and indicated that you needed more help than just what you had asked. If any of this is not necessary then just ignore it. I'm just trying to offer some advice and you can take it or leave it.

In case you haven't figured out how to read a pixel, I recommend using PIL. It has functions for opening images documented here. Then you can access a pixel in the image by its x and y value using getpixel which is documented on the same page.

For playing the note I would recommend looking into the PyAudio module and just making your own sinusoids of various frequencies (depending on the magnitude of the pixel) that you write to an open audio stream. There might be better packages for this part, but this is what I have used in my small adventures in Python audio.

For the audio stuff, I would try just outputting a sound at a fixed frequency before trying to actually emit a varying frequency.

Edit:
Your loops look better now so I took out my stuff about your loops.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文